-
Notifications
You must be signed in to change notification settings - Fork 0
/
MancalaGUI.py
226 lines (190 loc) · 7.66 KB
/
MancalaGUI.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import sys
from tkinter import *
from MancalaBoard import *
from Player import *
class MancalaWindow:
"""# A very simple GUI for playing the game of Mancala."""
def __init__(self, master, p1, p2, enableGUI):
self.CUPW = 75
self.HEIGHT = 200
self.BOARDW = 400
self.PAD = 0
self.game = MancalaBoard()
self.p1 = p1
self.p2 = p2
self.BINW = self.BOARDW / self.game.NCUPS
self.turn = p1
self.wait = p2
self.root = master
self.GUI = enableGUI
frame = Frame(master)
frame.pack()
# Create the board
self.makeBoard( frame )
displayStr = "Welcome to Mancala"
self.status = Label(frame, text=displayStr)
self.status.pack(side=BOTTOM)
self.won = Player(-1, 0, 0)
def enableBoard(self):
""" Allow a human player to make moves by clicking"""
for i in [0, 1]:
for j in range(self.game.NCUPS):
self.cups[i][j].bind("<Button-1>", self.callback)
def disableBoard(self):
""" Prevent the human player from clicking while the computer thinks"""
for i in [0, 1]:
for j in range(self.game.NCUPS):
self.cups[i][j].unbind("<Button-1>")
def makeBoard( self, frame ):
""" Create the board """
boardFrame = Frame(frame)
boardFrame.pack(side=TOP)
self.button = Button(frame, text="Start New Game", command=self.newgame)
self.button.pack(side=BOTTOM)
gamef = Frame(boardFrame)
topRow = Frame(gamef)
bottomRow = Frame(gamef)
topRow.pack(side=TOP)
bottomRow.pack(side=TOP)
tmpCups = []
tmpCups2 = []
binW = self.BOARDW/self.game.NCUPS
binH = self.HEIGHT/2
for i in range(self.game.NCUPS):
c = Canvas(bottomRow, width=binW, height=binH)
c.pack(side=LEFT)
tmpCups += [c]
c = Canvas(topRow, width=binW, height=binH)
c.pack(side=LEFT)
tmpCups2 += [c]
self.cups = [tmpCups, tmpCups2]
self.p1cup = Canvas(boardFrame, width=self.CUPW, height=self.HEIGHT)
self.p2cup = Canvas(boardFrame, width=self.CUPW, height=self.HEIGHT)
self.p2cup.pack(side=LEFT)
gamef.pack(side=LEFT)
self.p1cup.pack(side=LEFT)
self.drawBoard()
def drawBoard( self ):
""" Draw the board on the canvas """
self.p2cup.create_oval(self.PAD, self.PAD, self.CUPW, 0.9*self.HEIGHT, width=2 )
binW = self.BOARDW/self.game.NCUPS
binH = self.HEIGHT/2
for j in [0, 1]:
for i in range(self.game.NCUPS):
self.cups[j][i].create_rectangle(self.PAD, self.PAD, binW, binH)
self.p1cup.create_oval(self.PAD, self.PAD+0.1*self.HEIGHT, self.CUPW, self.HEIGHT, width=2 )
def newgame(self):
""" Start a new game between the players """
self.game.reset()
self.turn = self.p1
self.wait = self.p2
s = "Player " + str(self.turn) + "'s turn"
if self.turn.type != Player.HUMAN:
s += " Please wait..."
self.status['text'] = s
self.resetStones()
self.continueGame()
# Board must be disabled to call continueGame
def continueGame( self ):
""" Find out what to do next. If the game is over, report who
won. If it's a human player's turn, enable the board for
a click. If it's a computer player's turn, choose the next move."""
self.root.update()
if self.game.gameOver():
if self.game.hasWon(self.p1.num):
self.status['text'] = "Player " + str(self.p1) + " wins"
self.won = self.p1
elif self.game.hasWon(self.p2.num):
self.status['text'] = "Player " + str(self.p2) + " wins"
self.won = self.p2
else:
self.status['text'] = "Tie game"
self.won = 0
if not self.GUI:
self.root.quit()
self.root.destroy()
return
if self.turn.type == Player.HUMAN:
self.enableBoard()
else:
move = self.turn.choose_move(self.game)
playAgain = self.game.makeMove( self.turn, move )
if not playAgain:
self.swapTurns()
self.resetStones()
self.continueGame()
def swapTurns( self ):
""" Change whose turn it is"""
temp = self.turn
self.turn = self.wait
self.wait = temp
statusstr = "Player " + str(self.turn) + "\'s turn "
if self.turn.type != Player.HUMAN:
statusstr += "Please wait..."
self.status['text'] = statusstr
def resetStones(self):
""" Clear the stones and redraw them """
# Put the stones in the cups
for i in range(len(self.game.P2Cups)):
index = (len(self.game.P2Cups)-i)-1
self.clearCup(self.cups[1][index])
# put the number of stones at the top of the canvas
self.cups[1][index].create_text(self.BINW/2, 0.05*self.HEIGHT, text=str(self.game.P2Cups[i]), tag="num")
for i in range(len(self.game.P1Cups)):
# put the number of stones at the bottom of the canvas
self.clearCup(self.cups[0][i])
self.cups[0][i].create_text(self.BINW/2, 0.05*self.HEIGHT, text=str(self.game.P1Cups[i]), tag="num")
self.clearCup(self.p1cup)
self.clearCup(self.p2cup)
self.p2cup.create_text(self.CUPW/2, 10, text=str(self.game.scoreCups[1]), tag="num")
self.p1cup.create_text(self.CUPW/2, 10+0.1*self.HEIGHT, text=str(self.game.scoreCups[0]), tag="num")
def clearCup( self, cup ):
""" Clear the stones in the given cup"""
titems = cup.find_withtag("num")
stones = cup.find_withtag("stone")
cup.delete(titems)
cup.delete(stones)
def callback(self, event):
""" Handle the human player's move"""
# calculate which box the click was in
moveAgain = True
self.disableBoard()
if self.turn.num == 1:
for i in range(len(self.cups[0])):
if self.cups[0][i] == event.widget:
if self.game.legalMove( self.turn, i+1 ):
moveAgain = self.game.makeMove( self.turn, i+1 )
if not moveAgain:
self.swapTurns()
self.resetStones()
else:
for i in range(len(self.cups[1])):
if self.cups[1][i] == event.widget:
index = self.game.NCUPS - i
if self.game.legalMove( self.turn, index ):
moveAgain = self.game.makeMove( self.turn, index )
if not moveAgain:
self.swapTurns()
self.resetStones()
if moveAgain:
self.enableBoard()
else:
self.continueGame()
def startGame(p1, p2, game_num):
""" Start the game of Mancala with two players """
root = Tk()
app = MancalaWindow(root, p1, p2, True)
root.mainloop()
def startGameNoGUI(p1, p2, game_num):
""" Start the game of Mancala with two players """
root = Tk()
app = MancalaWindow(root, p1, p2, False)
app.root.withdraw()
app.newgame()
# root.mainloop()
if app.won == 0:
# print("Game n.{0}: There was a tie".format(str(game_num+1)))
return 0
else:
# print("Game n.{0}: Player {1} won".format(str(game_num+1), str(app.won.num)))
return app.won.num