-
Notifications
You must be signed in to change notification settings - Fork 0
/
othello_gui.py
182 lines (156 loc) · 6.66 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
This module contains a simple graphical user interface for Othello.
Thanks to original author Daniel Bauer, Columbia University
"""
import sys, getopt
from tkinter import *
from tkinter import scrolledtext
from othello_game import OthelloGameManager, AiPlayerInterface, Player, InvalidMoveError, AiTimeoutError
from othello_shared import get_possible_moves, get_score
class OthelloGui(object):
def __init__(self, game_manager, player1, player2):
self.game = game_manager
self.players = [None, player1, player2]
self.height = self.game.dimension
self.width = self.game.dimension
self.offset = 3
self.cell_size = 50
root = Tk()
root.wm_title("Othello")
root.lift()
root.attributes("-topmost", True)
self.root = root
self.canvas = Canvas(root,height = self.cell_size * self.height + self.offset,width = self.cell_size * self.width + self.offset)
self.move_label = Label(root)
self.score_label = Label(root)
self.text = scrolledtext.ScrolledText(root, width=70, height=10)
self.move_label.pack(side="top")
self.score_label.pack(side="top")
self.canvas.pack()
self.text.pack()
self.draw_board()
def get_position(self,x,y):
i = (x -self.offset) // self.cell_size
j = (y -self.offset) // self.cell_size
return i,j
def mouse_pressed(self,event):
i,j = self.get_position(event.x, event.y)
try:
player = "Dark" if self.game.current_player == 1 else "Light"
self.log("{}: {},{}".format(player, i,j))
self.game.play(i, j)
self.draw_board()
if not get_possible_moves(self.game.board, self.game.current_player):
self.shutdown("Game Over")
elif isinstance(self.players[self.game.current_player], AiPlayerInterface):
self.root.unbind("<Button-1>")
self.root.after(100,lambda: self.ai_move())
except InvalidMoveError:
self.log("Invalid move. {},{}".format(i,j))
def shutdown(self, text):
self.move_label["text"] = text
self.root.unbind("<Button-1>")
if isinstance(self.players[1], AiPlayerInterface):
self.players[1].kill(self.game)
if isinstance(self.players[2], AiPlayerInterface):
self.players[2].kill(self.game)
def ai_move(self):
player_obj = self.players[self.game.current_player]
try:
i,j = player_obj.get_move(self.game)
player = "Dark" if self.game.current_player == 1 else "Light"
player = "{} {}".format(player_obj.name, player)
self.log("{}: {},{}".format(player, i,j))
self.game.play(i,j)
self.draw_board()
if not get_possible_moves(self.game.board, self.game.current_player):
self.shutdown("Game Over")
elif isinstance(self.players[self.game.current_player], AiPlayerInterface):
self.root.after(1, lambda: self.ai_move())
else:
self.root.bind("<Button-1>",lambda e: self.mouse_pressed(e))
except AiTimeoutError:
self.shutdown("Game Over, {} lost (timeout)".format(player_obj.name))
def run(self):
if isinstance(self.players[1], AiPlayerInterface):
self.root.after(10, lambda: self.ai_move())
else:
self.root.bind("<Button-1>",lambda e: self.mouse_pressed(e))
self.draw_board()
self.canvas.mainloop()
def draw_board(self):
self.draw_grid()
self.draw_disks()
player = "Dark" if self.game.current_player == 1 else "Light"
self.move_label["text"]= player
self.score_label["text"]= "Dark {} : {} Light".format(*get_score(self.game.board))
def log(self, msg, newline = True):
self.text.insert("end","{}{}".format(msg, "\n" if newline else ""))
self.text.see("end")
def draw_grid(self):
for i in range(self.height):
for j in range(self.width):
self.canvas.create_rectangle(i*self.cell_size + self.offset, j*self.cell_size + self.offset, (i+1)*self.cell_size + self.offset, (j+1)*self.cell_size + self.offset, fill="dark green")
def draw_disk(self, i,j, color):
x = i * self.cell_size + self.offset
y = j * self.cell_size + self.offset
padding =2
self.canvas.create_oval(x+padding, y+padding, x+self.cell_size-padding, y+self.cell_size-padding, fill=color)
def draw_disks(self):
for i in range(self.height):
for j in range(self.width):
if self.game.board[i][j] == 1:
self.draw_disk(j, i, "black")
elif self.game.board[i][j] == 2:
self.draw_disk(j, i, "white")
def main(argv):
size = 0
limit = -1
ordering = False
caching = False
minimax = False
agent1 = None
agent2 = None
try:
opts, args = getopt.getopt(argv,"hcmol:d:a:b:",["limit=","dimension=","agent1=","agent2="])
except getopt.GetoptError:
print('othello_gui.py -d <dimension> [-a <agentA> -b <agentB> -l <depth-limit> -c -o -m]')
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print('othello_gui.py -d <dimension> -a <agentA> [-b <agentB> -l <depth-limit> -c -o]')
sys.exit()
elif opt in ("-d", "--dimension"):
size = int(arg)
elif opt in ("-a", "--agentA"):
agent1 = arg
elif opt in ("-b", "--agentB"):
agent2 = arg
elif opt in ("-c", "--caching"):
caching = True
elif opt in ("-m", "--minimax"):
minimax = True
elif opt in ("-o", "--ordering"):
ordering = True
elif opt in ("-l", "--limit"):
limit = int(arg)
if size <= 0: #if no dimension provided
print('Please provide a board size.')
print('othello_gui.py -d <dimension> [-a <agentA> -b <agentB> -l <depth-limit> -c -o]')
sys.exit(2)
if agent1 != None and agent2 != None and size > 0:
p1 = AiPlayerInterface(agent1,1,limit,minimax,caching,ordering)
p2 = AiPlayerInterface(agent2,2,limit,minimax,caching,ordering)
elif agent1 != None and size > 0:
p1 = Player(1)
p2 = AiPlayerInterface(agent1,2,limit,minimax,caching,ordering)
else:
p1 = Player(1)
p2 = Player(2)
game = OthelloGameManager(size)
gui = OthelloGui(game, p1, p2)
gui.run()
if __name__ == "__main__":
main(sys.argv[1:])