-
Notifications
You must be signed in to change notification settings - Fork 0
/
othello_gui.py
188 lines (156 loc) · 6.77 KB
/
othello_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
179
180
181
182
183
184
185
186
187
188
# othello_gui: a GUI based interface to get the user's move
# Copyright (C) 2006 Nimar S. Arora
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
import Tkinter
import time
import othello
import game2
import minimax
BOXWIDTH=80
BOXHEIGHT=80
class player:
"""Make a user player to play the game via a GUI."""
def __init__(self):
# create the GUI state variables
self.alive = True
self.move = None
self.move_played = False
# create the GUI windows and handlers
self.root = Tkinter.Tk()
self.root.protocol("WM_DELETE_WINDOW", self.quit)
# create a button to get the No move command from the user
Tkinter.Button(self.root, text="No Move", command = self.nomove).pack()
# create a label for displaying the next player's name
self.movemesg = Tkinter.StringVar()
Tkinter.Label(self.root, textvariable=self.movemesg).pack()
self.canvas = Tkinter.Canvas(self.root, bg="lightblue",
height = BOXHEIGHT*othello.size,
width = BOXWIDTH*othello.size)
self.canvas.bind("<Button-1>", self.click)
# create a box for highlighting the last move
self.lastbox = self.canvas.create_rectangle(0, 0, BOXWIDTH*othello.size,
BOXHEIGHT*othello.size,
outline="yellow")
# draw the game canvas
for i in xrange(1,othello.size):
# horizontal lines
self.canvas.create_line(0, i*BOXHEIGHT,
BOXWIDTH*othello.size, i*BOXHEIGHT)
# vertical lines
self.canvas.create_line(i*BOXWIDTH, 0,
i*BOXWIDTH, BOXHEIGHT*othello.size)
# the board will store the widgets to be displayed in each square
self.board = [[None for y in range(othello.size)]
for x in range(othello.size)]
# display the window
self.canvas.pack()
self.canvas.focus_set()
self.root.update()
def draw_board(self, game, last_move):
"""Draw an othello game on the board."""
if game.player == -1:
self.movemesg.set("Black to play")
else:
self.movemesg.set("White to play")
for i in range(othello.size):
for j in range(othello.size):
color = game.get_color((i,j))
if color == -1:
board_color = "black"
elif color == 1:
board_color = "white"
else:
if self.board[i][j] is not None:
self.canvas.delete(self.board[i][j])
self.board[i][j] = None
continue
if self.board[i][j] is None:
self.board[i][j] = self.canvas.create_oval(
j*BOXWIDTH+2, i*BOXHEIGHT+2, (j+1)*BOXWIDTH-2,
(i+1)*BOXHEIGHT-2, fill = board_color)
else:
self.canvas.itemconfig(self.board[i][j], fill=board_color)
# highlight the last move
if last_move is None:
self.canvas.coords(self.lastbox,
1, 1, BOXWIDTH*othello.size-1,BOXHEIGHT*othello.size-1)
else:
self.canvas.coords(
self.lastbox, last_move[1]*BOXWIDTH+1, last_move[0]*BOXHEIGHT+1,
(last_move[1]+1)*BOXWIDTH-1, (last_move[0]+1)*BOXHEIGHT-1)
def nomove(self):
self.move = None
self.move_played = True
def click(self, event):
self.move = (event.y/BOXHEIGHT, event.x/BOXWIDTH)
self.move_played = True
def quit(self):
self.alive = False
self.root.destroy()
def play(self, game, last_move):
# keep looping for a user move unless the user quits
while self.alive:
# wait for a user move
self.move_played = False
# grab the focus to ask the user for a move
self.draw_board(game, last_move)
self.canvas.focus_force()
self.root.configure(cursor="target")
while (not self.move_played) and self.alive:
self.root.update()
time.sleep(0.1)
if not self.move_played:
continue
# check the move
if self.move not in game.generate_moves():
self.root.bell()
continue
# display the new move
game.play_move(self.move)
self.draw_board(game, self.move)
self.root.configure(cursor="watch")
self.root.update()
# give a pause so I can see my move
time.sleep(.1)
return (0, self.move)
# if the user has quit the GUI then the game has to terminate,
# we force a termination by returning an illegal value
else:
return None
def gameover(self, game, last_move):
score = game.score() * game.player
if score > 0:
win_text = "White Won"
elif score < 0:
win_text = "Black Won"
else:
win_text = "Draw"
self.draw_board(game, last_move)
self.root.configure(cursor="X_cursor")
self.movemesg.set("Game Over "+win_text)
# wait for the user to quit the game
while self.alive:
self.root.update()
time.sleep(.1)
return
if __name__ == "__main__":
print """othello_gui, Copyright (C) 2006 Nimar S. Arora
othello_gui comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions."""
game2.play(othello.game(),
game2.player(lambda x: minimax.alphabeta(x, 4, othello.edge_eval)),
player(), True)