-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatchbox.py
385 lines (330 loc) · 9.26 KB
/
matchbox.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
import pygame as pg,sys
from pygame.locals import *
import time
import copy
import random
import flatten_dict
from flatten_dict import flatten
from flatten_dict import unflatten
#initialize global variables
database={}
record=[]
XO = 'x'
winner = None
width = 400
height = 400
white = (255, 255, 255)
line_color = (10,10,10)
attempt=0
#initizlizing array defining the game
TTT = [['x++']*3,[None]*3,['o++']*3]
#initializing pygame window
pg.init()
fps = 30
CLOCK = pg.time.Clock()
screen = pg.display.set_mode((width, height+100),0,32)
pg.display.set_caption("HEXAPAWN")
#loading the images
opening = pg.image.load('Tic-tac-toe.png')
clear_img=pg.image.load('clear.png')
x_img = pg.image.load('x.png')
o_img = pg.image.load('o.png')
rx_img = pg.image.load('Red_x.png')
ro_img = pg.image.load('red_o.png')
#resizing images
x_img = pg.transform.scale(x_img, (80,80))
o_img = pg.transform.scale(o_img, (80,80))
ro_img = pg.transform.scale(ro_img, (80,80))
rx_img = pg.transform.scale(rx_img, (80,80))
clear_img = pg.transform.scale(clear_img, (80,80))
opening = pg.transform.scale(opening, (width, height+100))
def game_opening():
#screen.blit(opening,(0,0))
pg.display.update()
time.sleep(1)
screen.fill(white)
# Drawing vertical lines
pg.draw.line(screen,line_color,(width/3,0),(width/3, height),7)
pg.draw.line(screen,line_color,(width/3*2,0),(width/3*2, height),7)
# Drawing horizontal lines
pg.draw.line(screen,line_color,(0,height/3),(width, height/3),7)
pg.draw.line(screen,line_color,(0,height/3*2),(width, height/3*2),7)
screen.blit(x_img,(30,30))
screen.blit(x_img,(width/3+30,30))
screen.blit(x_img,(2*width/3+30,30))
screen.blit(o_img,(30,2*width/3+30))
screen.blit(o_img,(width/3+30,2*width/3+30))
screen.blit(o_img,(2*width/3+30,2*width/3+30))
draw_status()
def draw_status():
global draw,winner
if winner is None:
message = XO.upper() + "'s Turn"
else:
message = winner.upper() + " won!"
font = pg.font.Font(None, 30)
text = font.render(message, 1, (255, 255, 255))
# copy the rendered message onto the board
screen.fill ((0, 0, 0), (0, 400, 500, 100))
text_rect = text.get_rect(center=(width/2, 500-50))
screen.blit(text, text_rect)
pg.display.update()
def check_win():
global TTT, winner,draw
checko=0
checki=0
# check for winning rows
for i in TTT:
if 'x++' in i:
if TTT.index(i)==2:
winner='x'
# break
checki=1
for i in TTT:
if 'o++' in i:
if TTT.index(i)==0:
winner='o'
# break
checko=1
if checki==0:
winner = 'o'
if(checko==0):
winner = 'x'
if winner:
modification()
reset_game()
draw_status()
def drawXO(row,col):
global TTT,XO,srow,scol,posy,posx,attempt,winner
status=""
#if in insertion phase make sure that it is moving directly forward when there's nobody there and move diagonally only whne ther'e somebody there
if(XO=='x++'):
if (row==(1+srow) and col==(scol)) and ( TTT[row-1][col-1]==None):
attempt=0
TTT[srow-1][scol-1]=None
status='moving'
elif(row==(1+srow) and ((col==(scol+1))or(col==(scol-1))) and TTT[row-1][col-1]=='o++'):
attempt=0
TTT[srow-1][scol-1]=None
status='attacking'
screen.blit(clear_img,(posy,posx))
else:
XO='x'
attempt+=1
screen.blit(clear_img,(posy,posx))
screen.blit(x_img,(posy,posx))
if attempt>=3:
winner='o'
return;
if(XO=='o++'):
time.sleep(1)
if (row==(-1+srow) and col==(scol) and TTT[row-1][col-1]==None):
attempt=0
TTT[srow-1][scol-1]=None
status='moving'
elif(row==(-1+srow) and ((col==(scol+1))or(col==(scol-1))) and TTT[row-1][col-1]=='x++'):
attempt=0
TTT[srow-1][scol-1]=None
status='attacking'
screen.blit(clear_img,(posy,posx))
else:
XO='o'
attempt+=1
screen.blit(clear_img,(posy,posx))
screen.blit(o_img,(posy,posx))
modification()
if attempt>=3:
winner='x'
return;
if row==1:
posx = 30
srow=1
if row==2:
posx = width/3 + 30
srow=2
if row==3:
posx = width/3*2 + 30
srow=3
if col==1:
posy = 30
scol=1
if col==2:
posy = height/3 + 30
scol=2
if col==3:
posy = height/3*2 + 30
scol=3
if(XO == 'x++'):
TTT[row-1][col-1] = XO#figuire this out
if status=='moving':
screen.blit(clear_img,(posy,posx-height/3))
screen.blit(x_img,(posy,posx))
XO='o'
return;
if status=='attacking':
# screen.blit(clear_img,(posy,posx-height/3))
screen.blit(clear_img,(posy,posx))
screen.blit(x_img,(posy,posx))
TTT[row-1][col-1]='x++'
XO='o'
return;
if(XO == 'o++'):
TTT[row-1][col-1] = XO
if status=='moving':
screen.blit(clear_img,(posy,posx+height/3))
screen.blit(o_img,(posy,posx))
XO='x'
return;
if status=='attacking':
# screen.blit(clear_img,(posy,posx+height/3))
screen.blit(clear_img,(posy,posx))
screen.blit(o_img,(posy,posx))
TTT[row-1][col-1]='o++'
XO='x'
return;
if(XO == 'x'):
if (TTT[row-1][col-1]=='x++'):
screen.blit(clear_img,(posy,posx))
screen.blit(rx_img,(posy,posx))
XO= 'x++'
return
if XO=='o':
if (TTT[row-1][col-1]=='o++'):
screen.blit(ro_img,(posy,posx))
XO= 'o++'
pg.display.update()
def userClick():
#get coordinates of mouse click
x,y = pg.mouse.get_pos()
#get column of mouse click (1-3)
if(x<width/3):
col = 1
elif (x<width/3*2):
col = 2
elif(x<width):
col = 3
else:
col = None
#get row of mouse click (1-3)
if(y<height/3):
row = 1
elif (y<height/3*2):
row = 2
elif(y<height):
row = 3
else:
row = None
if(row and col):
global XO
#draw the x or o on screen
drawXO(row,col)
check_win()
def reset_game():
print('entered reset game')
print(database)
global TTT, winner,XO, draw
time.sleep(3)
XO = 'x'
draw = False
draw_status()
winner=None
TTT = [['x++']*3,[None]*3,['o++']*3]
game_opening()
def modification():
global database,value,winner,record,XO
if winner=='x':
dict=flatten(database)
for i in record:
state=(list(i.keys()))
r=list(i.values())[0][0][0]
o=list(i.values())[0][0][1]
if i==record[-1]:
dict[(state[0]),r,o]/=8
else:
dict[(state[0]),r,o]/=2
record=[]
database=unflatten(dict)
return
elif winner=='o':
dict=flatten(database)
for i in record:
state=(list(i.keys()))
r=list(i.values())[0][0][0]
o=list(i.values())[0][0][1]
if i==record[-1]:
dict[(state[0]),r,o]*=8
else:
dict[(state[0]),r,o]*=2
record=[]
database=unflatten(dict)
return
elif (attempt!=0):
dict=flatten(database)
i=record[-1]
state=(list(i.keys()))
r=list(i.values())[0][0][0]
o=list(i.values())[0][0][1]
dict[(state[0]),r,o]=0
database=unflatten(dict)
def computers_turn():
global database,value,winner,record,XO
red_o={}
black_o={}
TTT1=[]
TTT1=copy.deepcopy(TTT)
#print(TTT1)
if str(TTT) not in database:
database[str(TTT)]=red_o#creating database with key as state and value as dictionary with possible o++'s to be moved
for i in (TTT1):
for j in i:
if j=='o++':#if o++ id in the state
#print("entering o++ state")
#print(i,i.index(j))
red_o[(str(TTT1.index(i)+1))+','+str(i.index(j)+1)]=black_o#creating dictionary with possible places to place the o
for k in range(3):
black_o[str(TTT1.index(i))+','+str(k+1)]=10
a=TTT1.index(i)
b=i.index(j)
TTT1[a][b]='-'
TTT[a][b]='o++'
lst=[]
weight=[]
for i in database[str(TTT)]:
for j in database[str(TTT)][i]:
lst+=[[i,j]]
weight.append(database[str(TTT)][i][j])
if sum(weight)==0:
winner='x'
modification()
draw_status()
reset_game()
return
value=random.choices(lst,weights=weight,k=1)
dicts={str(TTT):value}
record.append(dicts)#checkback
rovalue=row,col=map(int,(value[0][0]).split(','))
XO='o'
drawXO(row,col)
rovalue=row,col=map(int,(value[0][1]).split(','))
XO='o++'
drawXO(row,col)
check_win()
#starting the game
game_opening()
# run the game loop forever
while(True):
for event in pg.event.get():
if event.type == QUIT:
print("quitting")
pg.quit()
sys.exit()
elif XO=='o':
print("entering computer phase")
computers_turn()
elif event.type == MOUSEBUTTONDOWN:
# the user clicked; place an X or O
userClick()
if(winner):
reset_game()
pg.display.update()
CLOCK.tick(fps)