/
Game.cs
221 lines (172 loc) · 6.1 KB
/
Game.cs
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
using Agilix.Shared;
using Shared;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace TurboTank
{
[Flags]
public enum TankAction
{
Left = 1,
Right = 2,
Fire = 4,
Move = 8,
Noop = 16
}
public enum Orientation
{
North,
East,
South,
West
}
public enum Status
{
Running,
Won,
Lost,
Draw
}
public class Position
{
public Orientation Orientation;
public int X;
public int Y;
public Position(int x, int y, Orientation orientation)
{
this.Orientation = orientation;
this.X = x;
this.Y = y;
}
public override string ToString()
{
return string.Format("{0} {1},{2}", Orientation, X, Y);
}
}
public class Game : IDisposable
{
private TankClient client;
private long turnTimeout;
private int numberOfTurns = 0;
private TextWriter gameOutput;
public class GameConstants
{
//max_energy;
//laser_distance
//health_loss
//battery_power
//battery_health
//laser_energy
//connect_back_timeout
//max_health
//laser_damage
//turn_timeout
}
public Game(TankClient client, bool saveGame = false)
{
ThreadPool.SetMinThreads(operations.Length, 4);
this.client = client;
if (saveGame)
{
string filename = string.Format("TankGame-{0}.txt", DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss"));
gameOutput = new StreamWriter(new FileStream(filename, FileMode.Create));
}
else
{
gameOutput = Console.Out;
}
}
public void Dispose()
{
if (gameOutput != null)
{
gameOutput.Close();
}
}
public void Run(SignalWeights weights)
{
dynamic joinResponse = client.Start();
turnTimeout = (joinResponse.config.turn_timeout / 1000) - 5000;
Grid grid = new Grid();
Status status = ParseStatus(joinResponse, grid);
while (status == Status.Running)
{
numberOfTurns++;
TankAction action = GetBestAction(grid, weights);
dynamic moveResponse = client.TakeAction(action);
status = ParseStatus(moveResponse, grid);
}
}
static TankOperation[] operations = new TankOperation[] { new TankOperationLeft(), new TankOperationRight(), new TankOperationMove(), new TankOperationFire() };
public TankAction GetBestAction(Grid grid, SignalWeights weights)
{
long turnStartTime = Stopwatch.GetTimestamp();
bool successfulActionFound = false;
int depth = 1;
List<Beam> beams = new List<Beam>();
{
foreach (var operation in operations)
//TankOperation operation = operations[1];
{
Beam beam = new Beam(operation, grid, weights);
beams.Add(beam);
successfulActionFound = (successfulActionFound || beam.WasSuccessful());
}
}
int beamCompletedCount = 0;
while (!successfulActionFound && (beamCompletedCount < beams.Count) && !IsTimeout(turnTimeout, turnStartTime))
{
depth++;
Parallel.ForEach(beams, (beam) =>
{
if (beam.StillSearching())
{
foreach (EvalState state in beam.Iterate())
{
state.Grid.Health--;
foreach (TankOperation operation in operations)
{
EvalState opState = operation.GetScore(state, weights);
beam.Add(opState);
}
}
beam.Evaluate();
beamCompletedCount += (beam.StillSearching() ? 0 : 1);
successfulActionFound = (successfulActionFound || beam.WasSuccessful());
}
});
}
EvalState bestState = new EvalState(TankAction.Noop, grid);
foreach (var beam in beams)
{
EvalState operationBest = beam.GetBest();
gameOutput.WriteLine(" Beam {0} ({3} pts) - depth: {1}, candidates: {2} - {4}", beam.StartAction, depth, beam.CandidateCount, operationBest.Score, operationBest);
if (bestState.Score < operationBest.Score)
{
bestState = operationBest;
}
}
gameOutput.WriteLine("TURN {0} - Best: ({1}) {2}", numberOfTurns, bestState.Score, bestState);
return bestState.GetRootAction();
}
private Status ParseStatus(dynamic moveResponse, Grid grid)
{
Status status;
Enum.TryParse(moveResponse.status, true, out status);
gameOutput.WriteLine("\r\n\r\n=====================================\r\nTURN {0} \r\n{1}", numberOfTurns + 1, moveResponse.grid.ToString().Replace("\n", "\r\n"));
grid.Update(moveResponse);
return status;
}
public static bool IsTimeout(long turnTimeout, long turnStartTime)
{
long ticksUsed = Stopwatch.GetTimestamp() - turnStartTime;
return (ticksUsed > turnTimeout);
}
}
}