-
Notifications
You must be signed in to change notification settings - Fork 0
/
oxo_gui.py
115 lines (94 loc) · 3.17 KB
/
oxo_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
import tkinter as tk
import tkinter.messagebox as mb
import oxo_logic
root = tk.Tk()
gameover = False
def buildMenu(parent):
menus = (
("File", (("New", evNew),
("Resume", evResume),
("Save", evSave),
("Exit", evExit))),
("Help", (("Help", evHelp),
("About", evAbout)))
)
menubar = tk.Menu(parent)
for menu in menus:
m = tk.Menu(parent)
for item in menu[1]:
m.add_command(label=item[0], command=item[1])
menubar.add_cascade(label=menu[0], menu=m)
return menubar
def dummy():
mb.showinfo('Dummy', 'Event to be linked')
def evNew():
status['text'] = "Game On"
game2cells(oxo_logic.newGame())
def evResume():
status['text'] = "Game On"
game = oxo_logic.restoreGame()
game2cells(game)
def evSave():
game = cells2game()
oxo_logic.saveGame(game)
def evExit():
if status['text'] == "Game On":
if mb.askyesno("Quitting", "Do you want to save the game before quitting?"):
evSave()
root.quit()
def evHelp():
mb.showinfo("Help", '''
File -> New: starts a new game of tic-tac-toe
File -> Resume: restores the last saved game and commences play
File -> Save: Saves current game
File -> Exit: quits, prompts to save active game
Help -> Help: blach'''
)
def evAbout():
mb.showinfo("About", "Tic Tac Toe, bro")
def evClick(row, col):
if status['text'] == 'Game over':
mb.showerror("Game over", "Game over!")
return
game = cells2game() #translate the gameboard to the game string
index = (3 * row) + col #record the index of the string
result = oxo_logic.userMove(game, index) #get "", "X", "O", or "D"
game2cells(game) #draw the new user move
if not result:
result = oxo_logic.computerMove(game)
game2cells(game) #if no result, get a computer move and draw it
if result == "D":
mb.showinfo("Result", "It's a draw!")
status['text'] == 'Game over'
else:
if result == "X" or result == "O":
mb.showinfo("Result", "The winner is: {}".format(result))
status['text'] == 'Game over'
def game2cells(game):
table = board.pack_slaves()[0] #gets rootmost child?
for row in range(3):
for col in range(3):
table.grid_slaves(row=row, column=col)[0]['text'] = game[3 * row + col]
def cells2game():
values = []
table = board.pack_slaves()[0]
for row in range(3):
for col in range(3):
values.append(table.grid_slaves(row=row, column=col)[0]['text'])
return values
def buildBoard(parent):
outer = tk.Frame(parent, border=2, relief='sunken')
inner = tk.Frame(outer)
inner.pack()
for row in range(3):
for col in range(3):
cell = tk.Button(inner, text=" ", width='5', height='2', command=lambda r=row, c=col : evClick(r,c))
cell.grid(row=row, column=col)
return outer
mbar = buildMenu(root)
root["menu"] = mbar
board = buildBoard(root)
board.pack()
status = tk.Label(root, text='Game on', border=0, background='lightgrey', foreground='red')
status.pack(anchor='s', fill='x', expand=True)
tk.mainloop()