forked from goel-shashank/Cannon-AI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
285 lines (243 loc) · 7.13 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
from __future__ import print_function
import os
import sys
import time
import copy
import json
import math
import numpy as np
from multiset import Multiset
from selenium import webdriver
from jinja2 import Environment, FileSystemLoader
from selenium.webdriver.chrome.options import Options
display = {8: 600, 10: 750}
PATH = os.path.dirname(os.path.abspath(__file__))
TEMPLATE_ENVIRONMENT = Environment(autoescape = False, loader = FileSystemLoader(os.path.join(PATH, 'templates')), trim_blocks = False)
def render_template(template_filename, context):
return TEMPLATE_ENVIRONMENT.get_template(template_filename).render(context)
def create_index_html(rows, cols, height, width):
fname = "Cannon.html"
context = {
'rows': rows,
'cols': cols,
'height': height,
'width': width
}
with open(fname, 'w') as f:
html = render_template('index.html', context)
f.write(html)
class Game:
def __init__(self, n, m, mode = 'CUI', time = 120):
if(n in display and m in display):
self.rows = int(n)
self.cols = int(m)
self.height = display[n]
self.width = display[m]
else:
raise AssertionError("Board dimensions should be 8 or 10")
# setup Driver
create_index_html(self.rows, self.cols, self.height, self.width)
options = Options()
options.add_argument("--disable-infobars")
if(mode != 'GUI'):
options.add_argument('headless')
self.driver = webdriver.Chrome(options = options)
abs_path = os.path.abspath('Cannon.html')
self.driver.get("file:" + abs_path)
self.driver.set_window_size(width = self.width + 10, height = self.height + 132.5)
self.timer = time
self.townhalls = self.cols // 2
self.spacing = float(self.height) / self.rows
def click_at(self, x, y) :
e = self.driver.find_elements_by_id('PieceLayer')
action = webdriver.common.action_chains.ActionChains(self.driver)
action.move_to_element_with_offset(e[0], x * self.spacing + self.spacing / 2, y * self.spacing + self.spacing / 2)
action.click()
action.perform()
def check_move_validity(self):
return self.driver.execute_script('return is_valid;')
def check_player_state(self):
return self.driver.execute_script('return required_move;')
def get_current_player(self):
return self.driver.execute_script('return current_player;')
def getValidSoldiers(self):
valid_soldiers = list(self.driver.execute_script('return player[current_player].soldiers;'))
return valid_soldiers
def getValidMoves(self):
valid_moves = list(self.driver.execute_script('return guides_move;'))
return valid_moves
def getValidTargets(self):
valid_targets = list(self.driver.execute_script('return guides_bomb;'))
return valid_targets
def calculate_score(self, tA, tB, sA, sB, error_state):
if(error_state == '1'):
tA = 2
elif(error_state == '2'):
tB = 2
if(tA == 4 and tB == 2):
scoreA = 10
scoreB = 0
elif(tA == 3 and tB == 2):
scoreA = 8
scoreB = 2
elif(tA == 2 and tB == 3):
scoreA = 2
scoreB = 8
elif(tA == 2 and tB == 4):
scoreA = 0
scoreB = 10
else:
if(self.get_current_player() == 1):
if(self.driver.execute_script('return player[current_player].soldiers.length;') == 0):
if(tA == 4 and tB == 3):
scoreA = 10
scoreB = 0
elif(tA == 4 and tB == 4):
scoreA = 8
scoreB = 2
elif(tA == 3 and tB == 3):
scoreA = 8
scoreB = 2
elif(tA == 3 and tB == 4):
scoreA = 6
scoreB = 4
else:
if(tA == 4 and tB == 3):
scoreA = 8
scoreB = 2
elif(tA == 4 and tB == 4):
scoreA = 6
scoreB = 4
elif(tA == 3 and tB == 3):
scoreA = 6
scoreB = 4
elif(tA == 3 and tB == 4):
scoreA = 4
scoreB = 6
else:
if(self.driver.execute_script('return player[current_player].soldiers.length;') == 0):
if(tA == 4 and tB == 3):
scoreA = 4
scoreB = 6
elif(tA == 4 and tB == 4):
scoreA = 2
scoreB = 8
elif(tA == 3 and tB == 3):
scoreA = 2
scoreB = 8
elif(tA == 3 and tB == 4):
scoreA = 0
scoreB = 10
else:
if(tA == 4 and tB == 3):
scoreA = 6
scoreB = 4
elif(tA == 4 and tB == 4):
scoreA = 4
scoreB = 6
elif(tA == 3 and tB == 3):
scoreA = 4
scoreB = 6
elif(tA == 3 and tB == 4):
scoreA = 2
scoreB = 8
scoreA = scoreA + float(sA) / 100.0
scoreB = scoreB + float(sB) / 100.0
return [scoreA, scoreB]
def get_score(self, id, error_state = 0):
soldiersA = 0
soldiersB = 0
townhallsA = 0
townhallsB = 0
positions = list(self.driver.execute_script('return positions;'))
for i in range(self.cols):
for j in range(self.rows):
piece = dict(positions[i][j])['piece']
if(piece == 2):
townhallsA += 1
elif(piece == 1):
soldiersA += 1
elif(piece == -1):
soldiersB += 1
elif(piece == -2):
townhallsB += 1
townhallsA, townhallsB = self.cols // 2 - townhallsB, self.cols // 2 - townhallsA
return self.calculate_score(townhallsA, townhallsB, soldiersA, soldiersB, error_state)[int(id) - 1]
def check_finished(self):
required_move = self.driver.execute_script('return required_move;')
return (required_move == 2)
def check_stagnant(self):
is_stagnant = self.driver.execute_script('return is_stagnant;')
return (is_stagnant == 1)
def sign(self, x):
if(x == 0):
return 0
else:
return x / abs(x)
def execute_sequence(self, moves):
success = 1
move = []
id = self.get_current_player()
for i, j in enumerate(moves):
if(i % 3 == 2):
move += [j]
move_success = self.execute_move(' '.join(move))
if(move_success == 0):
return 0
success = success and move_success
move = []
else:
move += [j]
player = self.get_current_player()
if(id == player):
return 0
return success
"""
## Move types
# S - Select a Soldier
# M - Move a soldier
# B - Bombard a shot
"""
def execute_move(self, cmd) :
moves = cmd.split()
if(len(moves) > 3):
return self.execute_sequence(moves)
type = moves[0]
x = int(moves[1])
y = int(moves[2])
success = 1
string_valid = 1
positions = list(self.driver.execute_script('return positions;'))
if(type == 'S'):
self.click_at(x, y)
elif(type == 'M' and (dict(positions[x][y])['guide'] == 1 or dict(positions[x][y])['guide'] == 3)):
self.driver.execute_script('setAction(0);')
self.click_at(x, y)
elif(type == 'B' and (dict(positions[x][y])['guide'] == 2 or dict(positions[x][y])['guide'] == 3)):
self.driver.execute_script('setAction(1);')
self.click_at(x, y)
else:
string_valid = 0
move_valid = self.check_move_validity()
finished = self.check_finished()
stagnant = self.check_stagnant()
if(not (string_valid and move_valid)):
success = 0
elif(finished):
success = 2
elif(stagnant):
success = 3
return success
def simulate(self, filename):
with open(filename) as f:
lines = [line for line in f.readlines()]
for line in lines[:-1]:
parts = line.split('}')
part = parts[0] + '}'
out = json.loads(part)
exec("self.execute_move(\"" + out['data'] + "\")")
print(self.get_score(0, error_state = 0))
print(self.get_score(1, error_state = 0))
if __name__ == "__main__":
game = Game(8, 8, 'GUI')
game.simulate(sys.argv[1])