-
Notifications
You must be signed in to change notification settings - Fork 1
/
Runner.dpr
101 lines (79 loc) · 2.15 KB
/
Runner.dpr
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
uses
SysUtils, RemoteProcessClient, StrategyControl, MyStrategy,
PlayerControl, PlayerContextControl, MoveControl, GameControl;
type
TRunner = class
private
FRemoteProcessClient: TRemoteProcessClient;
FToken: String;
public
constructor Create(arguments: array of String);
procedure Run;
destructor Destroy; override;
end;
constructor TRunner.Create(arguments: array of String);
begin
if Length(arguments) = 4 then begin
FRemoteProcessClient := TRemoteProcessClient.Create(arguments[1], StrToInt(arguments[2]));
FToken := arguments[3];
end else begin
FRemoteProcessClient := TRemoteProcessClient.Create('127.0.0.1', 31001);
FToken := '0000000000000000';
end;
end;
procedure TRunner.Run;
var
game: TGame;
move: TMove;
player: TPlayer;
playerContext: TPlayerContext;
strategy: TStrategy;
begin
FRemoteProcessClient.WriteTokenMessage(FToken);
FRemoteProcessClient.WriteProtocolVersionMessage;
FRemoteProcessClient.ReadTeamSizeMessage;
game := FRemoteProcessClient.ReadGameContextMessage;
strategy := TMyStrategy.Create;
while true do begin
playerContext := FRemoteProcessClient.ReadPlayerContextMessage;
if playerContext = nil then begin
break;
end;
player := playerContext.Player;
if player = nil then begin
break;
end;
move := TMove.Create;
strategy.Move(player, playerContext.World, game, move);
FRemoteProcessClient.WriteMoveMessage(move);
move.Free;
playerContext.Free;
end;
strategy.Free;
end;
destructor TRunner.Destroy;
begin
FRemoteProcessClient.Free;
inherited;
end;
var
i: LongInt;
runner: TRunner;
arguments: array of String;
begin
try
SetLength(arguments, paramCount + 1);
for i := 0 to paramCount do begin
arguments[i] := ParamStr(i);
end;
runner := TRunner.Create(arguments);
runner.Run;
runner.Free;
SetLength(arguments, 0);
except
on E: Exception do begin
WriteLn('Exception occured: type=', E.ClassName, ', message="', E.Message, '".');
HALT(239);
end;
end;
end.