/
TankOperation.cs
244 lines (214 loc) · 7.67 KB
/
TankOperation.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TurboTank
{
public abstract class TankOperation
{
public abstract TankAction GetAction();
public abstract EvalState GetScore(EvalState state, SignalWeights weights);
protected static double CalculateBatteryNeedMultiplier(Grid grid)
{
double needMultiplier = 1;
if (grid.Health < 30 || grid.Energy < 2)
{
needMultiplier = 2;
}
else if (grid.Health > 280 && grid.Energy > 9)
{
needMultiplier = .8;
}
return needMultiplier;
}
}
public class TankOperationLeft : TankOperation
{
public override TankAction GetAction() { return TankAction.Left; }
public override EvalState GetScore(EvalState state, SignalWeights weights)
{
Grid grid = new Grid(state.Grid);
int score = 0;
if (state.Action == TankAction.Right)
{
score = -100;
}
else
{
Position leftPosition = grid.GetLeft();
char item = grid.GetItem(leftPosition);
if (item == '_')
{
score = 1;
}
else if (item == 'B')
{
score = 50;
}
else if (item == 'O')
{
score = (grid.Energy > 0 ? 60 : -100);
}
grid.Position = new Position(grid.Position.X, grid.Position.Y, leftPosition.Orientation);
}
return new EvalState(GetAction(), state, score, grid);
}
}
public class TankOperationRight : TankOperation
{
public override TankAction GetAction() { return TankAction.Right; }
public override EvalState GetScore(EvalState state, SignalWeights weights)
{
Grid grid = new Grid(state.Grid);
int score = 0;
if (state.Action == TankAction.Left)
{
score = -100;
}
else
{
Position rightPosition = grid.GetRight();
char item = grid.GetItem(rightPosition);
if (item == '_')
{
score = 1;
}
else if (item == 'B')
{
score = 50;
}
else if (item == 'O')
{
score = (grid.Energy > 0 ? 60 : -100);
}
grid.Position = new Position(grid.Position.X, grid.Position.Y, rightPosition.Orientation);
}
return new EvalState(GetAction(), state, score, grid);
}
}
public class TankOperationFire : TankOperation
{
public override TankAction GetAction() { return TankAction.Fire; }
public override EvalState GetScore(EvalState state, SignalWeights weights)
{
Grid grid = new Grid(state.Grid);
int score = -1000;
if (grid.Energy > 0 && !grid.ItemBehind('L', 3) && !grid.ItemBehind('O', 8))
{
int maxDistance = 10;
int distance = 1;
foreach (Position aheadPosition in grid.LookAhead(maxDistance))
{
char item = grid.GetItem(aheadPosition);
if (item == 'W')
{
score = -100;
break;
}
else if ((item == 'L') && (grid.Energy >= 10))
{
score = 100;
grid.SetItem(aheadPosition, '_');
break;
}
else if (item == 'O')
{
score = (maxDistance - distance) * 100;
grid.SetItem(aheadPosition, '_');
grid.EnemyHit = true;
break;
}
else if (item == 'B')
{
// TODO: if opponent is going to get the battery first then shoot it!
// TODO: One each evaluation move the lasers so they are in the right place.
// TODO: I should also influance going left or right based upon where the batteries and opponents are.
// TODO: Opponent behind what do we do? Shoot or turn around?
if (CalculateBatteryNeedMultiplier(grid) < 1)
{
score = (maxDistance - distance) * 10;
}
grid.SetItem(aheadPosition, '_');
break;
}
distance++;
}
}
grid.Energy -= 1;
EvalState fireState = new EvalState(GetAction(), state, score, grid);
return fireState;
}
}
public class TankOperationMove : TankOperation
{
public override TankAction GetAction() { return TankAction.Move; }
public override EvalState GetScore(EvalState state, SignalWeights weights)
{
Grid grid = new Grid(state.Grid);
int score = 0;
if (grid.ItemBehind('L', 3) || grid.ItemBehind('O', 3))
{
score = -1000;
}
else
{
int maxDistance = 15;
int distance = 1;
foreach (Position aheadPosition in grid.LookAhead(maxDistance))
{
char item = grid.GetItem(aheadPosition);
if (item == 'W')
{
if (distance == 1)
{
score = -1000;
}
break;
}
if (item == 'O')
{
if (grid.Energy == 0 && distance < 5)
{
score = -1000;
}
break;
}
else if (item == 'L')
{
score = -200;
break;
}
else if (item == 'B')
{
int distanceMultiplier = (maxDistance - distance);
score = (int)(((double)45) * distanceMultiplier * CalculateBatteryNeedMultiplier(grid));
if (distance == 1)
{
grid.Energy += 5;
grid.Health += 20;
grid.BatteryFound = true;
}
grid.SetItem(aheadPosition, '_');
break;
}
else if (item == '_')
{
score = 2;
}
distance++;
}
}
grid.Position = grid.GetAhead();
return new EvalState(GetAction(), state, score, grid);
}
}
public class TankOperationNoop : TankOperation
{
public override TankAction GetAction() { return TankAction.Noop; }
public override EvalState GetScore(EvalState state, SignalWeights weights)
{
return new EvalState(GetAction(), state, 0, state.Grid);
}
}
}