-
Notifications
You must be signed in to change notification settings - Fork 0
/
othello_base_GUI.py
181 lines (151 loc) · 7.12 KB
/
othello_base_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
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
import othello_base as ob
import pygame
import time
BOARD_X0 = 290
BOARD_Y0 = 10
SQUARE_WIDTH = 70
class OthelloGUI(ob.OthelloBase):
def __init__(self):
pygame.mixer.pre_init(44100, -16, 2, 2048)
pygame.init()
self.screen = pygame.display.set_mode((1280, 720))
self.board_image = pygame.image.load("othello.png")
black_piece_image = pygame.image.load("square-black.png").convert()
black_piece_images = [pygame.transform.scale(black_piece_image, (10 + 10 * i, 10 + 10 * i)) for i in range(7)]
white_piece_image = pygame.image.load("square-white.png").convert()
white_piece_images = [pygame.transform.scale(white_piece_image, (10 + 10 * i, 10 + 10 * i)) for i in range(7)]
self.player_images = {ob.BLACK: black_piece_images,
ob.WHITE: white_piece_images}
white_sound = pygame.mixer.Sound("gem_ping_A.wav")
black_sound = pygame.mixer.Sound("gem_ping_B.wav")
self.player_sounds = {ob.BLACK: black_sound,
ob.WHITE: white_sound}
self.flip_sound = pygame.mixer.Sound("flip2.wav")
self.score_font = pygame.font.SysFont("charter", size=60, bold=True)
self.name_font = pygame.font.SysFont("charter", size=30, bold=False)
def idx2rc(self, idx):
return(idx//10, idx%10)
def draw_move(self, move, player, board, animate = True):
(row, col) = self.idx2rc(move)
putX = BOARD_X0 + col * SQUARE_WIDTH
putY = BOARD_Y0 + row * SQUARE_WIDTH
if animate:
for (d,img) in zip(range(30,-1,-5),self.player_images[player]):
self.screen.blit(img, (putX+d, putY+d, SQUARE_WIDTH-2*d, SQUARE_WIDTH-2*d))
pygame.display.update(pygame.Rect(putX, putY, SQUARE_WIDTH-2*d, SQUARE_WIDTH-2*d))
else:
self.screen.blit(self.player_images[player][-1], (putX, putY, SQUARE_WIDTH, SQUARE_WIDTH))
pygame.display.flip()
def draw_flip(self, move: object, player: object, board: object, update_screen = True) -> object:
(row, col) = self.idx2rc(move)
putX = BOARD_X0 + col * SQUARE_WIDTH
putY = BOARD_Y0 + row * SQUARE_WIDTH
img = self.player_images[player][-1]
self.screen.blit(img, (putX, putY, SQUARE_WIDTH, SQUARE_WIDTH))
if update_screen:
pygame.display.update(pygame.Rect(putX, putY, SQUARE_WIDTH, SQUARE_WIDTH))
def make_move(self, move, player, board, silent = True):
"""Update the board to reflect the move by the specified player."""
board[move] = player
if not silent:
self.draw_move(move, player, board)
self.player_sounds[player].play()
for d in ob.DIRECTIONS:
self.make_flips(move, player, board, d, silent)
return board
def make_flips(self, move, player, board, direction, silent = True):
"""Flip pieces in the given direction as a result of the move by player."""
bracket = self.find_bracket(move, player, board, direction)
if not bracket:
return
square = move + direction
while square != bracket:
board[square] = player
if not silent:
self.draw_flip(square, player, board)
self.flip_sound.play()
square += direction
#print(direction, player, move)
def count_up(self, board, player):
return len([i for i in board if i == player])
def count_blacks(self, board):
return self.count_up(board, ob.BLACK)
def count_whites(self, board):
return self.count_up(board, ob.WHITE)
def text_objects(self, text, font):
textSurface = font.render(text, True, (255, 255, 240))
return textSurface, textSurface.get_rect()
def update_score(self, board):
score_w, rect_w = self.text_objects(str(self.count_whites(board)), self.score_font)
score_b, rect_b = self.text_objects(str(self.count_blacks(board)), self.score_font)
rect_b.midtop = 190, 150
rect_w.midtop = 1090, 150
# pygame.draw.rect(screen, (180, 180, 180), rect_b)
# pygame.draw.rect(screen, (180, 180, 180), rect_w)
self.screen.blit(self.board_image, (140, 70), (140, 70, 100, 150))
self.screen.blit(self.board_image, (140, 70), (140, 70, 100, 150))
self.screen.blit(self.board_image, (1050, 70), (1050, 70, 100, 150))
self.screen.blit(score_w, rect_w)
self.screen.blit(score_b, rect_b)
pygame.display.flip()
def show_player_names(self, black = "Black", white = "White"):
name_b, rect_b = self.text_objects(black, self.name_font)
name_w, rect_w = self.text_objects(white, self.name_font)
rect_b.midbottom = 190, 60
rect_w.midbottom = 1090,60
self.screen.blit(name_w, rect_w)
self.screen.blit(name_b, rect_b)
def post_winner(self, winner):
if winner=="Tie":
winner = "Tie Game!"
else:
winner = winner + " Wins!"
text, rect = self.text_objects(winner, self.name_font)
rect.bottomright = 1260, 700
self.screen.blit(text, rect)
pygame.display.flip()
def setup_board(self, board, black_name, white_name):
imagerect = self.board_image.get_rect()
self.screen.blit(self.board_image, imagerect)
self.draw_flip(44, ob.WHITE, board, update_screen=False)
self.draw_flip(55, ob.WHITE, board, update_screen=False)
self.draw_flip(45, ob.BLACK, board, update_screen=False)
self.draw_flip(54, ob.BLACK, board, update_screen=False)
self.show_player_names(black_name, white_name)
pygame.display.flip()
def play(self, black_strategy, white_strategy, black_name, white_name):
"""Play a game of Othello and return the final board and score."""
board = self.initial_board()
self.setup_board(board, black_name, white_name)
self.update_score(board)
player = ob.BLACK
strategy = lambda who: black_strategy if who == ob.BLACK else white_strategy
while player is not None:
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
mousepos = pygame.mouse.get_pos()
elif event.type == pygame.QUIT:
pygame.quit()
break
move = self.get_move(strategy(player), player, board)
self.make_move(move, player, board, silent = False)
self.update_score(board)
# print(self.print_board(board))
player = self.next_player(board, player)
black_score = self.score(ob.BLACK, board)
if black_score > 0:
winner = black_name
elif black_score < 0:
winner = white_name
else:
winner = "TIE"
self.post_winner(winner)
return board, self.score(ob.BLACK, board)
def end_wait(self):
while True:
event = pygame.event.wait()
if event.type == pygame.MOUSEBUTTONDOWN:
mousepos = pygame.mouse.get_pos()
elif event.type == pygame.QUIT:
pygame.quit()
break