-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
90 lines (47 loc) · 1.92 KB
/
Copy pathmain.py
File metadata and controls
90 lines (47 loc) · 1.92 KB
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
import pygame
from checkers.constants import WIDTH, HEIGHT, SQUARE_SIZE,WHITE
from checkers.game import Game
from checkers.ai import minimax
# Set up the display
FPS = 60
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('Checkers')
def get_row_col_from_mouse(pos):
x, y = pos
row = y // SQUARE_SIZE
col = x // SQUARE_SIZE
return row, col
# The main game loop function
def main():
run = True
clock = pygame.time.Clock()
game = Game(WIN)
while run:
clock.tick(FPS)
# --- AI's Turn ---
if game.turn == WHITE:
value, new_board = minimax(game.get_board(), 3, WHITE, game)
game.ai_move(new_board)
# After every turn, check if someone has won.
if game.winner() is not None:
if game.winner() == WHITE:
winner_text = "WHITE WINS!"
else:
winner_text = "RED WINS!"
font = pygame.font.SysFont("comicsans", 80)
text_surface = font.render(winner_text, 1, (255, 255, 255))
WIN.blit(text_surface, (WIDTH/2 - text_surface.get_width()/2, HEIGHT/2 - text_surface.get_height()/2))
pygame.display.update()
pygame.time.delay(5000)
break
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.MOUSEBUTTONDOWN:
pos = pygame.mouse.get_pos()
row, col = get_row_col_from_mouse(pos)
game.select(row, col)
game.update()
pygame.quit()
if __name__ == '__main__':
main()