-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMonteCarloThinker.pas
56 lines (48 loc) · 1.14 KB
/
MonteCarloThinker.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
unit MonteCarloThinker;
interface
uses
Classes,BoardControls,MonteCarlo,DataTypes,Windows;
type
TMonteCarloThinker = class(TThread)
private
{ Private declarations }
FBoard:PBoard;
FMoves:Int64;
FGames:Int64;
FKilled:Boolean;
function GetMoves:Int64;
protected
procedure Execute; override;
public
Constructor Create(ABoard:PBoard);
property Moves:Int64 read GetMoves;
property Games:Int64 read FGames;
property Killed:Boolean read FKilled write FKilled;
end;
implementation
uses Math;
function TMonteCarloThinker.GetMoves:Int64;
begin
Result:= trunc(sqrt(FMoves)*sqrt(FGames));
end;
Constructor TMonteCarloThinker.Create(ABoard:PBoard);
begin
inherited Create(False); //immediately start the thread, no need to wait for data
FBoard:=new(PBoard);
CopyMemory(FBoard,ABoard,sizeof(TBoard));
end;
procedure TMonteCarloThinker.Execute;
var
LSimBoard:PBoard;
begin
LSimBoard:=new(PBoard);
while true do
begin
CopyMemory(LSimBoard,FBoard,sizeof(TBoard));
FMoves:=FMoves +sqr(MonteWholeGame(LSimBoard));
inc(FGames);
If FKilled then Break;
end;
Dispose(LSimBoard);
end;
end.