-
Notifications
You must be signed in to change notification settings - Fork 0
/
othello_gui.py
144 lines (122 loc) · 5.28 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
COMS W4701 Artificial Intelligence - Programming Homework 2
This module contains a simple graphical user interface for Othello.
@author: Daniel Bauer
"""
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():
if len(sys.argv) == 3:
p1 = AiPlayerInterface(sys.argv[1],1)
p2 = AiPlayerInterface(sys.argv[2],2)
elif len(sys.argv) == 2:
p1 = Player(1)
p2 = AiPlayerInterface(sys.argv[1],2)
else:
p1 = Player(1)
p2 = Player(2)
game = OthelloGameManager(dimension=6)
gui = OthelloGui(game, p1, p2)
gui.run()
if __name__ == "__main__":
main()