-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMonteCarlo.pas
86 lines (80 loc) · 1.7 KB
/
MonteCarlo.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
unit MonteCarlo;
interface
uses
BoardControls,DataTypes,Windows;
function MonteOnetime(ABoard:PBoard;out WasMoved:Integer):Boolean;
function MonteWholeGame(ABoard:PBoard):Integer;
implementation
function MonteWholeGame(ABoard:PBoard):Integer;
var
finished:Boolean;
LMoved:Integer;
begin
finished:=False;
Result:=0;
while not finished do
begin
finished:= not MonteOneTime(ABoard,Lmoved);
inc(Result,Lmoved);
end;
end;
function MonteOnetime(ABoard:PBoard;out WasMoved:Integer):Boolean;
var
lx,ly:smallint;
newVal:TPOint;
SimBoard:PBoard;
i:Integer;
LChance:array[0..3]of Integer;
LMoved:Boolean;
LPropRange:array[0..3] of Integer;
LMoveVal:Integer;
begin
SimBoard:=new(PBoard);
for i := 0 to 3 do
begin
CopyMemory(SimBoard,ABoard,sizeof(TBoard));
case i of
0: begin lx:=1;ly:=0; end;
1: begin lx:=-1;ly:=0; end;
2: begin lx:=0;ly:=1; end;
3: begin lx:=0;ly:=-1; end;
end;
LMoved:= DoMove(SimBoard,lx,ly,newVal);
if LMoved then
LChance[i]:=(RatePosition(SimBoard))
else
LChance[i]:=0;
LProprange[i]:=LChance[i];
if i>0 then LPropRange[i]:=LChance[i]+LPropRange[i-1];
end;
Dispose(SimBoard);
LMoveVal:=random(LPropRange[3]); //random in full space
if LMoveVal<LPropRange[0] then
begin
lx:=1;ly:=0;
end else
begin
if LMoveVal<LPropRange[1] then
begin
lx:=-1;ly:=0;
end else
begin
if LMoveVal<LPropRange[2] then
begin
lx:=0;ly:=1;
end else
begin
lx:=0;ly:=-1;
end;
end;
end;
if DoMove(ABoard,lx,ly,newVal) then
begin
WasMoved:=1;
// if IsHighestPotenceInCorner(ABoard) and IsHighestPotencySingle(ABOard) then
// inc(WasMoved,getHighestPotence(ABoard));
end else
WasMoved:=0;
Result:=IsLost(ABoard);
end;
end.