-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
321 lines (255 loc) · 9.18 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
import asyncio
from threading import Thread
import pygame
import sys
import math
from pygame import MOUSEBUTTONDOWN, gfxdraw
from board import Board
from solver import Solver
BLUE = (72, 63, 161)
BLACK = (0,0,0)
RED = (153, 0, 0)
YELLOW = (230, 202, 25)
WHITE = (250,250,250)
DEPTH = 7
ROW_COUNT = 6
COLUMN_COUNT = 7
AI_PIECE = Board.YELLOW_PIECE
USER_PIECE = Board.RED_PIECE
def make_move(col):
global Color, game_over, screen
if board.canPlay(col):
board.drop_piece(col)
Board.current_piece = 3 - Board.current_piece
if board.winning_move(3-board.current_piece):
Color = YELLOW if board.current_piece == Board.RED_PIECE else RED
winstring = "Player "+ str(3 - board.current_piece) +" wins!!"
label = myfont.render( winstring , True, Color)
screen.blit(label, (20,5))
game_over = True
elif board.boardfilled():
label = myfont.render( "Draw!!" , True, Color)
screen.blit(label, (30,10))
game_over = True
Board.moves = Board.moves + str(col+1)
board.print_board()
print("eval: " , evaluation)
print("ends in :" , 42 - board.nbMoves() - 2*abs(evaluation))
draw_board(board)
board = Board()
solver = Solver()
board.print_board()
game_over = False
pygame.init()
SQUARESIZE = 100
ACTIVE_COLOR = pygame.Color('dodgerblue1')
INACTIVE_COLOR = pygame.Color('dodgerblue4')
FONT = pygame.font.SysFont("arial", 20)
# Font(None, 32)
AI_Enabled = True
width = Board.COLUMN_COUNT * SQUARESIZE
height = (Board.ROW_COUNT+1) * SQUARESIZE
size = (width, height+50)
RADIUS = int(SQUARESIZE/2 - 5)
evalbarfont = pygame.font.SysFont("monospace", 23)
evalstr = "0-0"
evalsurf = FONT.render(evalstr, True, WHITE)
screen = pygame.display.set_mode(size)
def draw_button(button, screen):
"""Draw the button rect and the text surface."""
pygame.draw.rect(screen, button['color'], button['rect'])
screen.blit(button['text'], button['text rect'])
def create_button(x, y, w, h, text, callback):
"""A button is a dictionary that contains the relevant data.
Consists of a rect, text surface and text rect, color and a
callback function.
"""
# The button is a dictionary consisting of the rect, text,
# text rect, color and the callback function.
text_surf = FONT.render(text, True, WHITE)
button_rect = pygame.Rect(x, y, w, h)
text_rect = text_surf.get_rect(center=button_rect.center)
button = {
'rect': button_rect,
'text': text_surf,
'text rect': text_rect,
'color': INACTIVE_COLOR,
'callback': callback,
}
return button
def toggleAi():
global AI_Enabled
AI_Enabled = not AI_Enabled
if not AI_Enabled:
btnAiToggle['text'] = FONT.render("Manual", True, WHITE)
btnAiToggle['text rect'] = btnAiToggle['text'].get_rect(center = btnAiToggle['rect'].center)
else:
btnAiToggle['text'] = FONT.render("AI", True, WHITE)
btnAiToggle['text rect'] = btnAiToggle['text'].get_rect(center = btnAiToggle['rect'].center)
def retryGame():
global game_over
game_over = False
def depthincr():
global DEPTH
DEPTH += 1
def depthdecr():
global DEPTH
DEPTH -= 1
def draw_board(board):
boord = board.getArrayRep()
global evaluation
for c in range(Board.COLUMN_COUNT):
for r in range(Board.ROW_COUNT):
pygame.draw.rect(screen, BLUE, (c*SQUARESIZE, r*SQUARESIZE+SQUARESIZE, SQUARESIZE, SQUARESIZE))
gfxdraw.aacircle(screen, int(c*SQUARESIZE+SQUARESIZE/2), int(r*SQUARESIZE+SQUARESIZE+SQUARESIZE/2), RADIUS, BLACK)
gfxdraw.filled_circle(screen, int(c*SQUARESIZE+SQUARESIZE/2), int(r*SQUARESIZE+SQUARESIZE+SQUARESIZE/2), RADIUS, BLACK)
for c in range(Board.COLUMN_COUNT):
for r in range(Board.ROW_COUNT):
if boord[r][c] == 1:
gfxdraw.filled_circle(screen, int(c*SQUARESIZE+SQUARESIZE/2), height - int(r*SQUARESIZE+SQUARESIZE/2), RADIUS-5, RED)
gfxdraw.aacircle(screen, int(c*SQUARESIZE+SQUARESIZE/2), height - int(r*SQUARESIZE+SQUARESIZE/2), RADIUS-5, RED)
elif boord[r][c] == 2:
gfxdraw.aacircle(screen, int(c*SQUARESIZE+SQUARESIZE/2), height - int(r*SQUARESIZE+SQUARESIZE/2), RADIUS-5, YELLOW)
gfxdraw.filled_circle(screen, int(c*SQUARESIZE+SQUARESIZE/2), height - int(r*SQUARESIZE+SQUARESIZE/2), RADIUS-5, YELLOW)
if evaluation >100 or evaluation <-100 or board.move_number> 15:
if evaluation == 0:
msg = "Draw in "
else:
msg = "You lose in " if evaluation >0 else "You win in "
evalstr = msg + str(ROW_COUNT*COLUMN_COUNT - board.nbMoves() +2 - 2*abs(evaluation)) + " moves"
else :
evalstr = str(board.move_number) +" | Evaluation = " + str(evaluation)
nodestr = "| Nodes visited = " + str(Solver.nodesvisited)
dpthstr = "| Depth = " + str(DEPTH)
nodebar = FONT.render(nodestr, True, WHITE)
dpthbar = FONT.render(dpthstr, True, WHITE)
evalbar = FONT.render(evalstr, True, WHITE)
barlen = (evaluation)/10
yelobarlen = min(width, barlen)
evalsurf = pygame.Surface((width, 50))
evalsurf.blit(evalbar, (10,2))
evalsurf.blit(dpthbar, (200,2))
evalsurf.blit(nodebar, (350,2))
pygame.draw.rect(evalsurf, RED, (0, 30, width/2 + width, 10 ))
pygame.draw.rect(evalsurf, YELLOW, (0, 30, width/2 + width * (yelobarlen), 10 ))
# pygame.display.update()
screen.blit(evalsurf, (0, height))
for button in buttonlist:
draw_button(button, screen)
# pygame.display.update()
analisys = Thread(None)
btnAiToggle = create_button(width - 130, height + 2, 90, 25, 'AI', toggleAi)
btnRetry = create_button( screen.get_rect().centery -150 ,300,200,75, 'Retry', retryGame)
btnDepthincr = create_button( 300, height+1, 13, 13,"+", depthincr)
btnDepthdecr = create_button(300, btnDepthincr['rect'].bottom +1 ,13,13, "-", depthdecr )
buttonlist = [btnAiToggle, btnDepthdecr ,btnDepthincr]
pygame.display.update()
myfont = pygame.font.SysFont("monospace", 75)
nodesvisited = 0
evaluation = 0
draw_board(board)
async def main():
global board, solver, game_over, analisys, evaluation
col = 0
calcrunning = 0
calchandled = True
while not game_over:
playernotdone = True
event = pygame.event.wait(300)
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.MOUSEMOTION:
Color = RED #if Board.current_piece == Board.RED_PIECE else YELLOW
pygame.draw.rect(screen, BLACK, (0,0, width, SQUARESIZE))
posx = event.pos[0]
pygame.draw.circle(screen, Color, (posx, int(SQUARESIZE/2)), RADIUS)
for button in buttonlist:
if button['rect'].collidepoint(event.pos):
button['color'] = ACTIVE_COLOR
else:
button['color'] = INACTIVE_COLOR
# pygame.display.update()
#get the position of user mouseclick
if (event.type == MOUSEBUTTONDOWN or event.type ==pygame.KEYDOWN) and playernotdone or not AI_Enabled:
if event.type == MOUSEBUTTONDOWN:
clicked = False
for button in buttonlist:
# `event.pos` is the mouse position.
if button['rect'].collidepoint(event.pos):
# Increment the number by calling the callback
# function in the button list.
button['callback']()
clicked = True
if not clicked:
pygame.draw.rect(screen, BLACK, (0,0, width, SQUARESIZE))
posx = event.pos[0]
col = int(math.floor(posx/SQUARESIZE))
# make_move(col)
# nodesvisited = 0
playernotdone = False
elif event.type == pygame.KEYDOWN:
playernotdone = False
if event.key == pygame.K_1:
col = 0
if event.key == pygame.K_2:
col = 1
if event.key == pygame.K_3:
col = 2
if event.key == pygame.K_4:
col = 3
if event.key == pygame.K_5:
col = 4
if event.key == pygame.K_6:
col = 5
if event.key == pygame.K_7:
col = 6
else: playernotdone = True
# # AI test
# if board.move_number <20 :
# col, evaluation = solver.minimax1(board, DEPTH,-math.inf , math.inf)
# else:
# col, evaluation = solver.negamax_solverr(board, -math.inf , math.inf )
# evaluation =-evaluation
if playernotdone == False and Board.current_piece == USER_PIECE:
make_move(col)
playernotdone = False
if Board.current_piece == AI_PIECE and AI_Enabled and not game_over and not analisys.is_alive():
if calchandled:
def lambad():
global evaluation
nonlocal col
# calcrunning = True
if board.move_number <15 :
col , evaluation = solver.minimax1(board, DEPTH, -math.inf, math.inf)
else:
analysis = solver.analyze(board)
col = max(analysis, key= lambda x : analysis[x])
evaluation = analysis[col]
# calcrunning = False
Solver.nodesvisited = 0
analisys = Thread(target= lambad)
analisys.start()
calchandled = False
elif not calchandled:
make_move(col)
calchandled = True
if game_over:
draw_button(btnRetry, screen)
pygame.display.update()
madedec = False
while not madedec:
for event in pygame.event.get():
if event.type == pygame.QUIT:
solver.table.reset()
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
if btnRetry['rect'].collidepoint(event.pos):
game_over = False
board = Board()
Board.moves = ""
madedec = True
pygame.time.wait(500)
draw_board(board)
pygame.display.update()
if __name__ == '__main__':
asyncio.run(main())