-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
351 lines (272 loc) · 11.7 KB
/
game.py
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
from typing import Tuple, List
import numpy as np
from numpy import ndarray
from numpy.random import Generator
from game_player import GamePlayer
class Game:
MapWidth = 6
MapHeight = 6
MapSize = MapWidth * MapHeight
NaturePlayer = 0
BluePlayer = 1
RedPlayer = 2
TileTroopMin = 1
TileTroopMax = 20
IdleMove = -1
ProductionMove = 0
AttackMove = 1
TransportMove = 2
def __init__(self):
self.state_parser = None
self.game_map = None
self.nature_player = None
self.blue_player = None
self.red_player = None
self.map_owners = None
self.map_troops = None
self.player_id = None
self.rounds = None
self.states = None
self.random = None # type: Generator
self.strategy = None
self.reset_game()
def reset_game(self, create_game_map: bool = True) -> None:
from state_parser import StateParser
from game_map import GameMap
self.state_parser = StateParser()
self.state_parser.game = self
self.game_map = GameMap() if create_game_map else self.game_map
self.game_map.game = self
self.game_map.state_parser = self.state_parser
self.nature_player = GamePlayer('nature', '#D4D4D8')
self.blue_player = GamePlayer('blue', '#2563EB')
self.red_player = GamePlayer('red', '#DC2626')
self.player_id = Game.RedPlayer
self.states = []
self.random = np.random.default_rng(2) # type: Generator
self.rounds = 0
self.map_owners, self.map_troops = self.get_map()
self.strategy = Game.ProductionMove
@staticmethod
def copy_of(other_game: 'Game') -> 'Game':
game = Game()
game.map_owners = other_game.map_owners.copy()
game.map_troops = other_game.map_troops.copy()
game.player_id = other_game.player_id
game.rounds = other_game.rounds
return game
def get_map(self) -> Tuple[ndarray, ndarray]:
map_owners = np.array([
[1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 2],
])
map_troops = np.array([
[1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1],
])
return map_owners, map_troops
def get_fitness(self) -> float:
return 0.0
def get_max_rounds(self) -> int:
return 500
def is_red_simulated(self) -> bool:
return True
def has_ended(self) -> bool:
return self.rounds >= self.get_max_rounds() \
or self.get_tile_count(Game.BluePlayer) == 0 \
or self.get_tile_count(Game.RedPlayer) == 0 \
or self.is_state_repeated()
def get_winner(self) -> int:
blue_tiles = self.get_tile_count(Game.BluePlayer)
red_tiles = self.get_tile_count(Game.RedPlayer)
if blue_tiles > 0 and red_tiles == 0:
return Game.BluePlayer
elif red_tiles > 0 and blue_tiles == 0:
return Game.RedPlayer
return Game.NaturePlayer
def get_player(self, player_id: int) -> GamePlayer:
if player_id == Game.BluePlayer:
return self.blue_player
elif player_id == Game.RedPlayer:
return self.red_player
else:
return self.nature_player
def create_state(self):
state = []
for i in range(Game.MapWidth):
for j in range(Game.MapHeight):
tile = (i, j)
sign = 1 if self.get_tile_owner(tile) == Game.BluePlayer else -1
troops = self.get_tile_troops(tile)
state.append(sign * troops)
return state
def save_state(self) -> None:
self.states.append(self.create_state())
def is_state_repeated(self) -> bool:
state = self.create_state()
repeats = len(list(filter(lambda past_state: past_state == state, self.states))) - 1
return repeats >= 3
def increase_round(self) -> None:
self.rounds += 1
def production_move(self, source_tile: Tuple[int, int]) -> None:
player = self.get_player(self.player_id)
s_i, s_j = source_tile
self.map_troops[s_i, s_j] += 1
player.production_moves += 1
def is_production_move_valid(self, source_tile: Tuple[int, int]) -> bool:
my_tiles = self.get_tiles(self.player_id)
if source_tile not in my_tiles:
return False
s_i, s_j = source_tile
if self.map_troops[s_i, s_j] >= Game.TileTroopMax:
return False
return True
def attack_move(self, source_tile: Tuple[int, int], target_tile: Tuple[int, int], attackers: int) -> None:
player = self.get_player(self.player_id)
s_i, s_j = source_tile
t_i, t_j = target_tile
self.map_troops[s_i, s_j] -= attackers
if self.map_troops[s_i, s_j] < Game.TileTroopMin:
self.map_owners[s_i, s_j] = Game.NaturePlayer
defenders = self.map_troops[t_i, t_j]
if defenders < attackers:
self.map_owners[t_i, t_j] = self.player_id
self.map_troops[t_i, t_j] = abs(defenders - attackers)
if self.map_troops[t_i, t_j] < Game.TileTroopMin:
self.map_owners[t_i, t_j] = Game.NaturePlayer
player.attack_moves += 1
def is_attack_move_valid(self, source_tile: Tuple[int, int], target_tile: Tuple[int, int], attackers: int) -> bool:
my_tiles = self.get_tiles(self.player_id)
if source_tile not in my_tiles:
return False
if target_tile in my_tiles:
return False
adjacent = self.get_tile_adj(source_tile)
if target_tile not in adjacent:
return False
s_i, s_j = source_tile
if attackers < Game.TileTroopMin or attackers > Game.TileTroopMax:
return False
if self.map_troops[s_i, s_j] < attackers:
return False
return True
def transport_move(self, source_tile: Tuple[int, int], target_tile: Tuple[int, int], transport: int) -> None:
player = self.get_player(self.player_id)
s_i, s_j = source_tile
t_i, t_j = target_tile
self.map_troops[s_i, s_j] -= transport
if self.map_troops[s_i, s_j] < Game.TileTroopMin:
self.map_owners[s_i, s_j] = Game.NaturePlayer
self.map_troops[t_i, t_j] += transport
player.transport_moves += 1
def is_transport_move_valid(self, source_tile: Tuple[int, int], target_tile: Tuple[int, int],
transport: int) -> bool:
my_tiles = self.get_tiles(self.player_id)
if source_tile not in my_tiles:
return False
if target_tile not in my_tiles:
return False
if source_tile == target_tile:
return False
s_i, s_j = source_tile
t_i, t_j = target_tile
if abs(s_i - t_i) > 1 or abs(s_j - t_j) > 1:
return False
if transport < Game.TileTroopMin or transport > Game.TileTroopMax:
return False
if self.map_troops[s_i, s_j] < transport:
return False
if self.map_troops[t_i, t_j] + transport > Game.TileTroopMax:
return False
return True
def get_tile_owner(self, tile: Tuple[int, int]) -> int:
return self.map_owners[tile[0], tile[1]]
def get_tile_count(self, player_id: int) -> int:
return len(self.get_tiles(player_id))
def get_troop_count(self, player_id: int) -> int:
tiles = self.get_tiles(player_id)
return int(np.sum([self.get_tile_troops(tile) for tile in tiles]).item())
def get_tile_troops(self, tile: Tuple[int, int]) -> int:
return self.map_troops[tile[0], tile[1]].item()
@staticmethod
def get_tile_coords(index: int) -> Tuple[int, int]:
return [(i, j) for i in range(Game.MapWidth) for j in range(Game.MapHeight)][index]
def get_tiles(self, player_id: int) -> List[Tuple[int, int]]:
results = np.argwhere(self.map_owners == player_id)
return [tuple(result) for result in results]
@staticmethod
def get_tile_number(coords: Tuple[int, int]):
lookup = {
(0, 0): 0, (0, 1): 1, (0, 2): 2, (0, 3): 3, (0, 4): 4, (0, 5): 5,
(1, 0): 6, (1, 1): 7, (1, 2): 8, (1, 3): 9, (1, 4): 10, (1, 5): 11,
(2, 0): 12, (2, 1): 13, (2, 2): 14, (2, 3): 15, (2, 4): 16, (2, 5): 17,
(3, 0): 18, (3, 1): 19, (3, 2): 20, (3, 3): 21, (3, 4): 22, (3, 5): 23,
(4, 0): 24, (4, 1): 25, (4, 2): 26, (4, 3): 27, (4, 4): 28, (4, 5): 29,
(5, 0): 30, (5, 1): 31, (5, 2): 32, (5, 3): 33, (5, 4): 34, (5, 5): 35,
}
return lookup[coords]
@staticmethod
def get_tile_adj(tile: Tuple[int, int]) -> List[Tuple[int, int]]:
lookup = {
(0, 0): [(0, 1), (1, 0)],
(0, 1): [(0, 0), (0, 2), (1, 0), (1, 1)],
(0, 2): [(0, 1), (1, 1), (1, 2), (0, 3)],
(0, 3): [(0, 2), (1, 2), (1, 3), (0, 4)],
(0, 4): [(0, 3), (1, 3), (1, 4), (0, 5)],
(0, 5): [(0, 4), (1, 4), (1, 5)],
(1, 0): [(0, 0), (0, 1), (1, 1), (2, 0), (2, 1)],
(1, 1): [(0, 1), (0, 2), (1, 0), (1, 2), (2, 1), (2, 2)],
(1, 2): [(0, 2), (0, 3), (1, 1), (1, 3), (2, 2), (2, 3)],
(1, 3): [(0, 3), (0, 4), (1, 2), (1, 4), (2, 3), (2, 4)],
(1, 4): [(0, 4), (0, 5), (1, 3), (1, 5), (2, 4), (2, 5)],
(1, 5): [(0, 5), (1, 4), (2, 5)],
(2, 0): [(1, 0), (2, 1), (3, 0)],
(2, 1): [(1, 0), (1, 1), (2, 0), (2, 2), (3, 0), (3, 1)],
(2, 2): [(1, 1), (1, 2), (2, 1), (2, 3), (3, 1), (3, 2)],
(2, 3): [(1, 2), (1, 3), (2, 2), (2, 4), (3, 2), (3, 3)],
(2, 4): [(1, 3), (1, 4), (2, 3), (2, 5), (3, 3), (3, 4)],
(2, 5): [(1, 4), (1, 5), (2, 4), (3, 4), (3, 5)],
(3, 0): [(2, 0), (2, 1), (3, 1), (4, 0), (4, 1)],
(3, 1): [(2, 1), (2, 2), (3, 0), (3, 2), (4, 1), (4, 2)],
(3, 2): [(2, 2), (2, 3), (3, 1), (3, 3), (4, 2), (4, 3)],
(3, 3): [(2, 3), (2, 4), (3, 2), (3, 4), (4, 3), (4, 4)],
(3, 4): [(2, 4), (2, 5), (3, 3), (3, 5), (4, 4), (4, 5)],
(3, 5): [(2, 5), (3, 4), (4, 5)],
(4, 0): [(3, 0), (4, 1), (5, 0)],
(4, 1): [(3, 0), (3, 1), (4, 0), (4, 2), (5, 0), (5, 1)],
(4, 2): [(3, 1), (3, 2), (4, 1), (4, 3), (5, 1), (5, 2)],
(4, 3): [(3, 2), (3, 3), (4, 2), (4, 4), (5, 2), (5, 3)],
(4, 4): [(3, 3), (3, 4), (4, 3), (4, 5), (5, 3), (5, 4)],
(4, 5): [(3, 4), (3, 5), (4, 4), (5, 4), (5, 5)],
(5, 0): [(4, 0), (4, 1), (5, 1)],
(5, 1): [(4, 1), (4, 2), (5, 0), (5, 2)],
(5, 2): [(4, 2), (4, 3), (5, 1), (5, 3)],
(5, 3): [(4, 3), (4, 4), (5, 2), (5, 4)],
(5, 4): [(4, 4), (4, 5), (5, 3), (5, 5)],
(5, 5): [(4, 5), (5, 4)],
}
return lookup[tile]
def __str__(self):
blue_tiles = self.get_tile_count(Game.BluePlayer)
red_tiles = self.get_tile_count(Game.RedPlayer)
blue_troops = self.get_troop_count(Game.BluePlayer)
red_troops = self.get_troop_count(Game.RedPlayer)
fitness = self.get_fitness()
winner = self.get_winner()
return f'Rounds: {self.rounds:>4}' \
f'{"":2}' \
f'Tiles: {blue_tiles:>2} / {red_tiles:<2}' \
f'{"":2}' \
f'Troops: {blue_troops:>3} / {red_troops:>3}' \
f'{"":2}' \
f'Fitness: {fitness:>8.1f}' \
f'{"":2}' \
f'Winner: {winner:>4}'