-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgui.py
118 lines (101 loc) · 4.25 KB
/
gui.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
import pygame, sys
from pygame.locals import *
from environment import *
from ai import *
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
CELLWIDTH = 60
CELLHEIGHT = 60
BOARDX = 30
BOARDY = 30
def terminate():
pygame.quit()
sys.exit()
pygame.init()
mainClock = pygame.time.Clock()
boardImage = pygame.image.load('resources/board.png')
boardRect = boardImage.get_rect()
blackImage = pygame.image.load('resources/black.png')
blackRect = blackImage.get_rect()
whiteImage = pygame.image.load('resources/white.png')
whiteRect = whiteImage.get_rect()
frameImage = pygame.image.load('resources/frame.png')
frameRect = whiteImage.get_rect()
basicFont = pygame.font.SysFont(None, 50)
def draw_board(reversi, last_move=(-1,-1)):
windowSurface.fill(WHITE)
windowSurface.blit(boardImage, boardRect, boardRect)
for i, j in reversi.black_pieces:
rectDst = pygame.Rect(BOARDX+j*CELLWIDTH,
BOARDY+i*CELLHEIGHT, CELLWIDTH, CELLHEIGHT)
windowSurface.blit(blackImage, rectDst, blackRect)
for i, j in reversi.white_pieces:
rectDst = pygame.Rect(BOARDX+j*CELLWIDTH,
BOARDY+i*CELLHEIGHT, CELLWIDTH, CELLHEIGHT)
windowSurface.blit(whiteImage, rectDst, whiteRect)
if last_move != (-1,-1):
rectDst = pygame.Rect(BOARDX+last_move[1]*CELLWIDTH,
BOARDY+last_move[0]*CELLHEIGHT, CELLWIDTH, CELLHEIGHT)
windowSurface.blit(frameImage, rectDst, frameRect)
reversi = Reversi()
ai = HardAI()
windowSurface = pygame.display.set_mode((boardRect.width, boardRect.height))
pygame.display.set_caption('Reversi by Aoxue and Song')
last_move = (-1,-1)
while True:
for event in pygame.event.get():
if event.type == QUIT:
terminate()
if not reversi.game_end:
if reversi.current_player == -ai.ai_color:
if event.type == MOUSEBUTTONDOWN and event.button == 1:
valid_moves = reversi.find_valid_moves()
if len(valid_moves) == 0:
reversi.current_player = -reversi.current_player
else:
x, y = pygame.mouse.get_pos()
col = int((x-BOARDX)/CELLWIDTH)
row = int((y-BOARDY)/CELLHEIGHT)
reversi.make_move(row, col, hint=True, trace=False)
last_move = (row, col)
else:
valid_moves = reversi.find_valid_moves()
if len(valid_moves) == 0:
reversi.current_player = -reversi.current_player
else:
row, col = ai.find_best_move(reversi)
reversi.make_move(row, col, hint=True, trace=False)
last_move = (row, col)
draw_board(reversi, last_move)
pygame.display.flip()
if reversi.is_game_over():
reversi.game_end = True
if len(reversi.black_pieces) > len(reversi.white_pieces):
outputStr = "You Win!"
reversi.winner = 1
elif len(reversi.black_pieces) < len(reversi.white_pieces):
outputStr = "You Lose!"
reversi.winner = -1
else:
outputStr = "Draw!"
reversi.winner = 0
outputStr += " {}:{}".format(len(reversi.black_pieces),len(reversi.white_pieces))
text = basicFont.render(outputStr, True, WHITE, BLACK)
textRect = text.get_rect()
textRect.centerx = windowSurface.get_rect().centerx
textRect.centery = windowSurface.get_rect().centery
windowSurface.blit(text, textRect)
pygame.display.flip()
pause = True
while pause:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pause = False
terminate()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
reversi.reset()
last_move = (-1, -1)
draw_board(reversi, last_move)
pygame.display.flip()
pause = False