-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.py
346 lines (300 loc) · 15 KB
/
main.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
from binascii import b2a_uu
import pygame, os, sys
from pygame.locals import *
from src.board import *
from src.constants import *
from src.button import *
from src.home import *
from src.config import *
from src.qnim import *
import tkinter
from tkinter import ttk
from time import sleep
from threading import Thread
from queue import Queue
import time
# from multiprocessing import Pool
# TODO
# create threads for main function as well as quantum_function to run im parallel
# undo button
# show circuit state on ibm backend
def show_qc_animation(board,screen):
nim_sum = 0
for b in board.curr_board_state:
nim_sum = nim_sum^b
counts_ones = board.curr_board_state.count(1)
counts_zeros = board.curr_board_state.count(0)
if counts_ones == 1 and counts_zeros == 3:
nim_sum = 0
# odd 1s heaps left
if counts_zeros + counts_ones == 4 and counts_ones%2 == 1:
nim_sum = 0
# even 1s left then win for QC so change nim sum
if counts_zeros + counts_ones == 4 and counts_ones%2 == 0:
nim_sum = 1
x_pos = board.qc_turn.rect.topleft[0]
y_pos = board.qc_turn.rect.topleft[1] - 25 - animation_h
start_time, end_time = time.time(), time.time()
delta = round(end_time-start_time,1)
blank_bg = pygame.Surface((animation_h,animation_w))
while delta <= 3:
sleep(0.5)
end_time = time.time()
delta = round(end_time-start_time,1)
if delta == 0.5:
screen.blit(blank_bg,(x_pos,y_pos))
pygame.display.flip()
if nim_sum != 0:
screen.blit(happy_sprite[0],(x_pos,y_pos))
else:
screen.blit(frowning_sprite[0],(x_pos,y_pos))
pygame.display.flip()
if delta == 1:
screen.blit(blank_bg,(x_pos,y_pos))
pygame.display.flip()
if nim_sum != 0:
screen.blit(happy_sprite[1],(x_pos,y_pos))
else:
screen.blit(frowning_sprite[1],(x_pos,y_pos))
pygame.display.flip()
if delta == 1.5:
screen.blit(blank_bg,(x_pos,y_pos))
pygame.display.flip()
if nim_sum != 0:
screen.blit(happy_sprite[2],(x_pos,y_pos))
else:
screen.blit(frowning_sprite[2],(x_pos,y_pos))
pygame.display.flip()
if delta == 2:
screen.blit(blank_bg,(x_pos,y_pos))
pygame.display.flip()
if nim_sum != 0:
screen.blit(happy_sprite[3],(x_pos,y_pos))
else:
screen.blit(frowning_sprite[3],(x_pos,y_pos))
pygame.display.flip()
if delta == 2.5:
screen.blit(blank_bg,(x_pos,y_pos))
pygame.display.flip()
if nim_sum != 0:
screen.blit(happy_sprite[4],(x_pos,y_pos))
else:
screen.blit(frowning_sprite[4],(x_pos,y_pos))
pygame.display.flip()
if delta > 2.8:
screen.blit(blank_bg,(x_pos,y_pos))
pygame.display.flip()
os.environ['SDL_VIDEO_CENTERED'] = '1' # center the window
pygame.init()
screen = pygame.display.set_mode((W, H))
pygame.display.set_caption('QNim')
board = Board() # the snake
home_page = Home()
config_page = Config()
clock = pygame.time.Clock() # for timing and snake's speed
seconds = 3 # seconds before start
def main():
global start, game_over
# pool = Pool()
saved_snapshot = None
home_page.draw(screen, saved_snapshot)
running = True
in_config = False
in_home = True
in_game = False
qc_lost, player_lost = False, False
while running: # main loop
for event in pygame.event.get():
if event.type == pygame.QUIT or running == False:
pygame.quit()
sys.exit()
break
if home_page.continue_saved_game_button.on_button_press(screen) and in_home == True and saved_snapshot is not None:
in_game = True
in_home = False
screen.blit(saved_snapshot,(0,0))
saved_snapshot = None
pygame.display.flip()
if home_page.start_button.on_button_press(screen) and in_home == True:
board.nim_board.clear()
board.quantum_computer_plays_first = False
board.curr_board_state.clear()
board.map_switch_state = {}
board.allowed_lights_to_flip.clear()
board.player_selected_bulb.clear()
in_home = False
saved_snapshot = None
screen.fill('black')
in_config = True
config_page.draw(screen)
if home_page.quit_button.on_button_press(screen) and in_home == True:
running = False
if in_config == True:
for i in range(len(config_page.plus_buttons)):
plus_button = config_page.plus_buttons[i]
plus_button.on_plus_button_press(screen, i, config_page.bulb_counts, config_page.bulb_counts_display)
minus_button = config_page.minus_buttons[i]
minus_button.on_minus_button_press(screen, i, config_page.bulb_counts, config_page.bulb_counts_display)
if config_page.player_first.on_select_turn(screen,config_page.player_first,config_page.qc_first, config_page.player_first_selected_image, config_page.qc_first_image):
config_page.quantum_computer_plays_first = False
if config_page.qc_first.on_select_turn(screen,config_page.player_first,config_page.qc_first, config_page.player_first_image, config_page.qc_first_selected_image):
config_page.quantum_computer_plays_first = True
if config_page.start_button.on_button_press(screen):
in_game = True
screen.fill('black')
in_config = False
board.draw(screen, config_page.bulb_counts,config_page.quantum_computer_plays_first)
if in_game == True:
for i in range(len(board.nim_board)):
for j in range(len(board.nim_board[i])):
button = board.nim_board[i][j]
button.on_light_press(screen, board.map_switch_state, board.light_off, board.light_on, board.allowed_lights_to_flip, board.nim_board, board.player_selected_bulb, board.curr_board_state)
if board.okay_button.on_button_press(screen) and (qc_lost or player_lost):
qc_lost = False
player_lost = False
saved_snapshot = None
config_page.bulb_counts_display.clear()
config_page.plus_buttons.clear()
config_page.minus_buttons.clear()
config_page.bulb_counts = [1,3,5,7]
config_page.quantum_computer_plays_first = False
board.nim_board.clear()
board.quantum_computer_plays_first = False
board.curr_board_state.clear()
board.map_switch_state = {}
board.allowed_lights_to_flip.clear()
board.player_selected_bulb.clear()
in_game = False
in_home = True
home_page.draw(screen, saved_snapshot)
if board.quantum_computer_plays_first == True or board.qc_turn.on_qc_turn_button_press(screen) == True:
if len(board.player_selected_bulb) == 0 and board.quantum_computer_plays_first == False:
original_surface = pygame.Surface.copy(screen)
x_pos = (W-notification_W)/2
y_pos = (H-notification_H)/2
transaprent_bg = pygame.Surface((W, H))
transaprent_bg.set_alpha(200)
screen.blit(transaprent_bg,(0,0))
screen.blit(select_at_least_one_pbject, (x_pos,y_pos))
pygame.display.flip()
sleep(1.5)
screen.blit(original_surface, (0,0))
pygame.display.flip()
else:
# player lost
counts_zeros = board.curr_board_state.count(0)
if counts_zeros == 4:
player_lost = True
board.quantum_computer_plays_first = False
x_pos = (W-notification_bg_W)/2
y_pos = (H-notification_bg_H)/2
transaprent_bg = pygame.Surface((W, H))
transaprent_bg.set_alpha(200)
screen.blit(transaprent_bg,(0,0))
screen.blit(notification_bg, (x_pos,y_pos))
x_pos = x_pos + (notification_bg_W-notification_W)/2
y_pos = y_pos+50
screen.blit(you_lost, (x_pos,y_pos))
board.okay_button.draw(screen)
pygame.display.flip()
continue
screen.blit(board.qc_turn_active_image, board.qc_turn.rect.topleft)
screen.blit(board.my_turn_image, board.my_turn_active.rect.topleft)
for i in range(len(board.allowed_lights_to_flip)):
button = board.allowed_lights_to_flip[i][0]
screen.blit(board.light_on, button.rect.topleft)
pygame.display.flip()
board.allowed_lights_to_flip.clear()
board.player_selected_bulb.clear()
que = Queue()
thr = Thread(target = lambda q, arg : q.put(get_quantum_move(arg)), args = (que, board.curr_board_state))
thr.setDaemon(True)
thr.start()
thr.join()
while not que.empty():
# pygame.time.delay(1000)
# pygame.event.pump()
row, objects_to_pick = (que.get())
break
# row, objects_to_pick = get_quantum_move(board.curr_board_state)
print(row, objects_to_pick)
# insert animation
show_qc_animation(board,screen)
# qc made silly move
if row<0 or row>3 or objects_to_pick <= 0 or objects_to_pick > board.curr_board_state[row]:
qc_lost = True
board.quantum_computer_plays_first = False
x_pos = (W-notification_bg_W)/2
y_pos = (H-notification_bg_H)/2
transaprent_bg = pygame.Surface((W, H))
transaprent_bg.set_alpha(200)
screen.blit(transaprent_bg,(0,0))
screen.blit(notification_bg, (x_pos,y_pos))
x_pos = x_pos + (notification_bg_W-notification_W)/2
y_pos = y_pos+50
screen.blit(qc_silly_move, (x_pos,y_pos))
board.okay_button.draw(screen)
pygame.display.flip()
else:
print("Before:",board.curr_board_state)
board.quantum_computer_plays_first = False
last_index = board.curr_board_state[row] - 1
board.curr_board_state[row] -= objects_to_pick
for count in range(objects_to_pick):
button = board.nim_board[row][last_index]
last_index -= 1
screen.blit(board.light_off, (button.rect.x, button.rect.y))
pygame.display.flip()
board.map_switch_state[button] = False
print("AfterL:",board.curr_board_state)
# sleep(5)
screen.blit(board.my_turn_active_image, board.my_turn.rect.topleft)
screen.blit(board.qc_turn_image, board.qc_turn_active.rect.topleft)
pygame.display.flip()
# allowed lights to flip
for i in range(len(board.curr_board_state)):
if board.curr_board_state[i] > 0:
last_bulb = board.curr_board_state[i]-1
board.allowed_lights_to_flip.append((board.nim_board[i][last_bulb],i,last_bulb))
button = board.nim_board[i][last_bulb]
image = board.light_on.copy()
image.fill(GREEN_TINT, special_flags=pygame.BLEND_ADD)
screen.blit(image,button.rect.topleft)
pygame.display.flip()
# QC lost
counts_zeros = board.curr_board_state.count(0)
if counts_zeros == 4:
qc_lost = True
board.quantum_computer_plays_first = False
x_pos = (W-notification_bg_W)/2
y_pos = (H-notification_bg_H)/2
transaprent_bg = pygame.Surface((W, H))
transaprent_bg.set_alpha(200)
screen.blit(transaprent_bg,(0,0))
screen.blit(notification_bg, (x_pos,y_pos))
x_pos = x_pos + (notification_bg_W-notification_W)/2
y_pos = y_pos+50
screen.blit(congratulations_you_won, (x_pos,y_pos))
board.okay_button.draw(screen)
pygame.display.flip()
continue
if board.quit_button.on_button_press(screen):
running = False
# to do
# add quit confirmation
if board.back_button.on_button_press(screen):
saved_snapshot = pygame.Surface.copy(screen)
config_page.bulb_counts_display.clear()
config_page.plus_buttons.clear()
config_page.minus_buttons.clear()
config_page.bulb_counts = [1,3,5,7]
config_page.quantum_computer_plays_first = False
in_game = False
in_home = True
home_page.draw(screen, saved_snapshot)
pygame.display.update()
# pygame.event.pump()
# pygame.time.delay(1000) # 1 second == 1000 milliseconds
# pygame.display.flip()
if __name__ == "__main__":
main()