-
Notifications
You must be signed in to change notification settings - Fork 3
/
aicontest.py
405 lines (339 loc) · 11.9 KB
/
aicontest.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
import math
import numpy as np
import subprocess
import sys
vertices = (
(1.5, -1.5, -1.5),
(1.5, 1.5, -1.5),
(-1.5, 1.5, -1.5),
(-1.5, -1.5, -1.5),
(1.5, -1.5, 1.5),
(1.5, 1.5, 1.5),
(-1.5, -1.5, 1.5),
(-1.5, 1.5, 1.5)
)
edges = (
(0, 1),
(0, 3),
(0, 4),
(2, 1),
(2, 3),
(2, 7),
(6, 3),
(6, 4),
(6, 7),
(5, 1),
(5, 4),
(5, 7)
)
def draw_text(position, text_string, size, color_):
font = pygame.font.Font(None, size)
text_surface = font.render(text_string, True, color_, (0, 0, 0, 255))
text_data = pygame.image.tostring(text_surface, "RGBA", True)
glRasterPos3d(*position)
glDrawPixels(text_surface.get_width(), text_surface.get_height(), GL_RGBA, GL_UNSIGNED_BYTE, text_data)
def draw_cube():
glBegin(GL_LINES)
for edge in edges:
for vertex in edge:
glVertex3fv(vertices[vertex])
glEnd()
def draw_grid_y():
draw_cube()
glPushMatrix()
for i in range(3):
glTranslatef(0.0, 3, 0.0)
draw_cube()
glPopMatrix()
glPushMatrix()
for i in range(4):
glTranslatef(0.0, -3, 0.0)
draw_cube()
glPopMatrix()
def draw_grid():
draw_grid_y()
glPushMatrix()
for i in range(3):
glTranslatef(3.0, 0.0, 0.0)
draw_grid_y()
glPopMatrix()
glPushMatrix()
for i in range(4):
glTranslatef(-3.0, 0.0, 0.0)
draw_grid_y()
glPopMatrix()
def draw_move(selected_cube):
glPushMatrix()
glTranslatef((selected_cube[0] - 4) * 3, (selected_cube[1] - 4) * 3, 0)
glBegin(GL_QUADS)
glVertex3f(1.5, 1.5, -1.5)
glVertex3f(-1.5, 1.5, -1.5)
glVertex3f(-1.5, -1.5, -1.5)
glVertex3f(1.5, -1.5, -1.5)
glEnd()
glPopMatrix()
def draw_reaction(cubes):
glColor3f(1, .8, .1)
for cube in cubes:
draw_move(cube)
def draw_sphere(radius, is_red):
points = np.zeros((105, 105, 3))
stack = 20
slices = 20
for i in range(stack+1):
h = radius*math.sin(float((i/stack))*(math.pi/2))
r = radius*math.cos(float((i/stack))*(math.pi/2))
for j in range(slices+1):
points[i][j][0] = r*math.cos(float((j/slices))*2*math.pi)
points[i][j][1] = r*math.sin(float((j/slices))*2*math.pi)
points[i][j][2] = h
for i in range(stack):
glColor3f(is_red*(i+stack)/(2*stack), (1-is_red)*i/stack, 0)
for j in range(slices):
glBegin(GL_QUADS)
glVertex3f(points[i][j][0], points[i][j][1], points[i][j][2])
glVertex3f(points[i][j+1][0], points[i][j+1][1], points[i][j+1][2])
glVertex3f(points[i+1][j+1][0], points[i+1][j+1][1], points[i+1][j+1][2])
glVertex3f(points[i+1][j][0], points[i+1][j][1], points[i+1][j][2])
glVertex3f(points[i][j][0], points[i][j][1], -points[i][j][2])
glVertex3f(points[i][j+1][0], points[i][j+1][1], -points[i][j+1][2])
glVertex3f(points[i+1][j+1][0], points[i+1][j+1][1], -points[i+1][j+1][2])
glVertex3f(points[i+1][j][0], points[i+1][j][1], -points[i+1][j][2])
glEnd()
def draw_spheres():
global grid, angles
sphere_color = {'R': 1, 'G': 0}
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] != 'No':
glPushMatrix()
glTranslatef((i - 4)*3, (j - 4)*3, 0)
glRotatef(angles[i][j], 0, 0, 1)
angles[i][j] = (angles[i][j] + 5) % 360
if grid[i][j][1] == '1':
draw_sphere(.7, sphere_color[grid[i][j][0]])
elif grid[i][j][1] == '2':
glPushMatrix()
glTranslatef(-.3, 0, 0)
draw_sphere(.6, sphere_color[grid[i][j][0]])
glPopMatrix()
glPushMatrix()
glTranslatef(.3, 0, 0)
draw_sphere(.6, sphere_color[grid[i][j][0]])
glPopMatrix()
elif grid[i][j][1] == '3':
glPushMatrix()
glTranslatef(-.3, -.2, 0)
draw_sphere(.6, sphere_color[grid[i][j][0]])
glPopMatrix()
glPushMatrix()
glTranslatef(.3, -.2, 0)
draw_sphere(.6, sphere_color[grid[i][j][0]])
glPopMatrix()
glPushMatrix()
glTranslatef(0, .2, 0)
draw_sphere(.6, sphere_color[grid[i][j][0]])
glPopMatrix()
elif grid[i][j][1] >= '4':
glPushMatrix()
glTranslatef(-.3, -.2, 0)
draw_sphere(.6, sphere_color[grid[i][j][0]])
glPopMatrix()
glPushMatrix()
glTranslatef(.3, -.2, 0)
draw_sphere(.6, sphere_color[grid[i][j][0]])
glPopMatrix()
glPushMatrix()
glTranslatef(.3, .2, 0)
draw_sphere(.6, sphere_color[grid[i][j][0]])
glPopMatrix()
glPushMatrix()
glTranslatef(-.3, .2, 0)
draw_sphere(.6, sphere_color[grid[i][j][0]])
glPopMatrix()
glPopMatrix()
grid = None
angles = None
def check_validity(values):
global cur_player, players, invalid_move
if len(values) != 2:
invalid_move = True
return False
if not values[0].isdigit() or not values[1].isdigit():
invalid_move = True
return False
values = [int(value) for value in values]
if values[0] >= 8 or values[1] >= 8:
invalid_move = True
return False
if grid[values[0]][values[1]][0] == players[1 - cur_player]:
invalid_move = True
return False
return True
def check_reaction(selected_cube):
dirs = [(1, 0), (-1, 0), (0, 1), (0, -1)]
count = 0
for dir in dirs:
temp = np.array(selected_cube) + np.array(dir)
if temp[0] > -1 and temp[0] < 8 and temp[1] > -1 and temp[1] < 8:
count += 1
return count
def update_grid(selected_cube):
global grid, cur_player, players, grid_updated, cubes_to_update
x = selected_cube[0]
y = selected_cube[1]
if grid[x][y] == 'No':
grid[x][y] = players[cur_player] + '1'
else:
atom_count = int(grid[x][y][1])
if check_reaction(selected_cube) == atom_count + 1:
cubes_to_update.append(selected_cube)
grid[x][y] = players[cur_player] + str(int(grid[x][y][1])+1)
grid_updated = True
def reaction(selected_cube):
global grid, players, cur_player, cubes_to_update
dirs = [(1, 0), (-1, 0), (0, 1), (0, -1)]
x = selected_cube[0]
y = selected_cube[1]
atom_count = int(grid[x][y][1])
react_count = check_reaction(selected_cube)
if react_count == atom_count:
grid[x][y] = "No"
else:
count = atom_count - react_count
if count >= react_count:
cubes_to_update.append(selected_cube)
grid[x][y] = players[cur_player] + str(count)
for dir in dirs:
temp = np.array(selected_cube) + np.array(dir)
if temp[0] > -1 and temp[0] < 8 and temp[1] > -1 and temp[1] < 8:
update_grid(temp)
def write_grid_2():
global cur_player, grid, p1, p2
# str_to_write = players[cur_player] + '\n'
str_to_write = ""
for i in grid:
for j in i:
str_to_write += j + " "
str_to_write += '\n'
if cur_player == 0:
print(str_to_write, file=p1.stdin, flush=True, end="")
else:
print(str_to_write, file=p2.stdin, flush=True, end="")
def check_winner():
global move_count, grid, invalid_move, cur_player
if invalid_move:
return 1 - cur_player
if move_count <= 2:
return -1
green = False
red = False
if 'G1' in grid or 'G2' in grid or 'G3' in grid or 'G4' in grid or 'G5' in grid:
green = True
if 'R1' in grid or 'R2' in grid or 'R3' in grid or 'R4' in grid or 'R5' in grid:
red = True
if green and not red:
return 1
if red and not green:
return 0
return -1
cur_player = None
players = ['R', 'G']
cubes_to_update = None
grid_updated = None
move_count = None
move_read = None
invalid_move = None
move_speed = None
is_over = False
p1 = None
p2 = None
def init():
global grid, angles, cur_player, cubes_to_update, grid_updated, move_count, move_read, invalid_move, move_speed, p1, \
p2
pygame.init()
display = (800, 800)
pygame.display.set_mode(display, DOUBLEBUF | OPENGL)
gluPerspective(45, (display[0] / display[1]), 0.1, 80.0)
grid = np.full((8, 8), 'No')
cubes_to_update = []
angles = np.zeros((8, 8))
cur_player = 0
grid_updated = False
move_count = 0
move_read = False
invalid_move = False
move_speed = int(sys.argv[1])
p1 = subprocess.Popen(['python3', 'player_code.py', 'R'], stdout=subprocess.PIPE, stdin=subprocess.PIPE,
universal_newlines=True, bufsize=1)
p2 = subprocess.Popen(['python3', 'player_code.py', 'G'], stdout=subprocess.PIPE, stdin=subprocess.PIPE,
universal_newlines=True, bufsize=1)
print('start', file=p1.stdin, flush=True)
print('start', file=p2.stdin, flush=True)
write_grid_2()
def display_grid():
global is_over, grid, cur_player, players, cubes_to_update, grid_updated, move_count, move_read, invalid_move, \
move_count
glColor3f(1, 0, 0)
glTranslatef(0.0, 0.0, -45)
# cur_player = 0
while True:
if not is_over and len(cubes_to_update) > 0:
draw_reaction(cubes_to_update)
temp = cubes_to_update.copy()
cubes_to_update.clear()
for update in temp:
reaction(update)
pygame.time.wait(move_speed)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
if not is_over and not move_read:
if cur_player == 0:
selected_cube = p1.stdout.readline().strip('\n').split(" ")
else:
selected_cube = p2.stdout.readline().strip('\n').split(" ")
move_read = True
move_count += 1
if check_validity(selected_cube):
selected_cube = [int(i) for i in selected_cube]
update_grid(selected_cube)
pygame.time.wait(move_speed)
glColor3f(.6, .6, .6)
draw_move(selected_cube)
else:
invalid_move = True
if not is_over:
draw_reaction(cubes_to_update)
if not is_over and grid_updated and len(cubes_to_update) == 0:
cur_player = 1 - cur_player
write_grid_2()
grid_updated = False
move_read = False
draw_text((-5, 5.0, 30.0), "CHAIN REACTION", 32, (120, 120, 220, 255))
draw_text((-5, 5.5, 30.0), "CSE Fest 2019 - AI Contest", 24, (120, 120, 220, 255))
if cur_player == 0:
draw_text((-5, 4, 30.0), "Player 1's move", 24, (250, 10, 10, 255))
else:
draw_text((-5, 4, 30.0), "Player 2's move", 24, (10, 250, 10, 255))
glColor3f(1-cur_player, cur_player, 0)
draw_grid()
draw_spheres()
if invalid_move:
draw_text((-4, 1, 30.0), "Invalid Move by Player" + str(cur_player+1), 64, (120, 120, 220, 255))
is_over = True
if check_winner() != -1:
draw_text((-2.5, 0, 30.0), "Player " + str(check_winner()+1)+" Wins", 64, (120, 120, 220, 255))
is_over = True
pygame.display.flip()
pygame.time.wait(10)
if __name__ == "__main__":
init()
display_grid()