-
Notifications
You must be signed in to change notification settings - Fork 0
/
Checkerboard.py
260 lines (219 loc) · 10.7 KB
/
Checkerboard.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# Ziccardi, Zachary; Donohue, Mark; Kasliwal, Mannan; and Suesser, Marc
# CS110 A52, A53, A53, and A53
# Project: Checkerboard
from tkinter import *
import checkersEvaluation
class Checkerboard(Frame):
def __init__(self, turn, spaceContents):
Frame.__init__(self)
self.master.title("Checkerboard")
self.grid()
#Team 1 can move south
#Team 2 can move north
#Holds which team's turn it currently is
self.__turn = turn
#Will hold the contents of each space by using the space as a key, with
#data type being a tuple of integer, corresponding to (row, column).
# Value of each key will contain a reference to the checker occupying
#the space if it exists
self.__spaceContents = spaceContents
# New Frame for menu title
self.__titleFrame = Frame(self)
self.__titleFrame.grid(column = 9, row = 1)
# New Frame for Names
self.__nameFrame = Frame(self)
self.__nameFrame.grid(column = 9, row = 2, padx = 30)
# New Frame for Turn
self.__turnFrame = Frame(self)
self.__turnFrame.grid(column = 9, row = 3)
# Label for the 'title' of the menu
self.__startGameLabel = Label(self.__titleFrame, \
text = "Let's play Checkers!")
# Player name labels, Player name entries,
# Submit buttons
self.__redPlayerLabel = Label(self.__nameFrame, \
text = "Red Team Player Name:")
self.__redNameEntryVar = StringVar()
self.__redNameEntryVar.set("Red Player")
self.__redNameEntry = Entry(self.__nameFrame, \
width = 15,
textvariable = self.__redNameEntryVar)
self.__whitePlayerLabel = Label(self.__nameFrame, \
text = "White Team Player Name:")
self.__whiteNameEntryVar = StringVar()
self.__whiteNameEntryVar.set("White Player")
self.__whiteNameEntry = Entry(self.__nameFrame, \
width = 15,
textvariable = self.__whiteNameEntryVar)
self.__turnLabel = Label(self.__turnFrame, \
text = "Your Turn:")
self.__turnVar = StringVar()
if self.__turn == 1:
self.__turnVar.set(self.__redNameEntryVar.get())
else:
self.__turnVar.set(self.__whiteNameEntryVar.get())
self.__displayTurn = Label(self.__turnFrame, \
textvariable = self.__turnVar)
# Updating label that tells user if their move was valid or invalid
self.__moveAnalysisLabel = Label(self.__turnFrame, \
text = "Move Analysis:")
'''Code to make the move analysis label change with each move'''
self.__analysisValue = StringVar()
self.__displayAnalysis = Label(self.__turnFrame, \
textvariable = self.__analysisValue)
# Organize titleFrame
self.__startGameLabel.grid( column = 1, row = 1)
# Organize nameFrame
self.__redPlayerLabel.grid( column = 1, row = 2)
self.__redNameEntry.grid( column = 2, row = 2)
self.__whitePlayerLabel.grid( column = 1, row = 3)
self.__whiteNameEntry.grid( column = 2, row = 3)
# Organize turnFrame
self.__turnLabel.grid( column = 1, row = 1)
self.__displayTurn.grid( column = 2, row = 1)
self.__moveAnalysisLabel.grid(column = 1, row = 2)
self.__displayAnalysis.grid( column = 2, row = 2)
self.__images = {
"red" : PhotoImage(file = "redChecker.gif"),
"redKing" : PhotoImage(file = "redCheckerKing.gif"),
"redHighlighted" : PhotoImage(file = "redCheckerHighlighted.gif"),
"redKingHighlighted" : PhotoImage(file = \
"redCheckerKingHighlighted.gif"),
"white" : PhotoImage(file = "whiteChecker.gif"),
"whiteKing" : PhotoImage(file = "whiteCheckerKing.gif"),
"whiteHighlighted" : PhotoImage(file = \
"whiteCheckerHighlighted.gif"),
"whiteKingHighlighted" : PhotoImage(file = \
"whiteCheckerKingHighlighted.gif"),
"blackBlank" : PhotoImage(file = "blackBlank.gif"),
"whiteBlank" : PhotoImage(file = "whiteBlank.gif")
}
#Reference to the checker currently chosen by the player whose turn it
#currently is, 0 if no checker is currently chosen
self.__curChecker = 0
self.__buttons = {}
for i in range(64):
r = (i // 8) + 1
c = (i % 8) + 1
space = (r, c)
photoFile = self.__getImageFile(space)
#lambda
#
#info from: http://stackoverflow.com/questions/
# 890128/python-lambda-why
#
#description: allows us to pass the space variable into the
# callback function for the button
#
# the lambda command's general use is to replicate a
# function. an example from the website referenced
# sums it up well:
# > f = lambda x: x + 1
# > f(3)
# 4
self.__buttons[space] = Button(self, command=lambda widget=space: \
self.__activated(widget), \
image = self.__images[photoFile],
activebackground = "black")
self.__buttons[space].grid(row=r, column=c)
#Narrative: Retrieves the value of the current turn
#Precondition: Must be called
#Postcondition: Returns integer value of turn, 1 or 2
def getTurn(self):
return self.__turn
#Narrative: Changes the turn value to given argument
#Precondition: Must be called, takes turn as argument (1 or 2)
#Postcondition: Sets turn to the given value
def setTurn(self, turn):
self.__turn = turn
#Narrative: Retrieves the reference to the currently selected checker
#Precondition: Must be called
#Postcondition: Returns reference to the checker, 0 if none
def getCurChecker(self):
return self.__curChecker
#Narrative: Changes the currently assigned checker
#Precondition: Must be called, takes checker or 0 as an argument
#Postcondition: Sets currently assigned checker to new checker or 0 (none)
def setCurChecker(self, curChecker):
self.__curChecker = curChecker
#Narrative: Retrieves the dictionary containing all spaces
#Precondition: Must be called
#Postcondition: Returns integer value of turn, 1 or 2
def getSpaceContents(self):
return self.__spaceContents
#Narrative: Changes dictionary referring to spaces and their contents
#Precondition: Must be called, takes spaceContents as argument (dictionary)
#Postcondition: Changes the value of dictionary containing space contents
def setSpaceContents(self, spaceContents):
self.__spaceContents = spaceContents
#Narrative: Updates label to reflect the move previously made
#Precondition: Must be called, takes string value message as argument
#Postcondition: Sets label to given message argument
def updateAnalysisValue(self, message):
self.__analysisValue.set(message)
#Narrative: Adds a checker to the board
#Precondition: Takes a reference to the checker as an argument
#Postcondition: Adds checker to the dictionary containing all checkers
def checkerCreated(self, checker):
self.__spaceContents[checker.getSpace()] = checker
#Narrative: Changes the turn values and resets curChecherk
#Precondition: Reference to the checker board must be passed as an argument
#Postcondition: Toggles turn attribute on board, resets curChecker and
# updates label to reflect changes
def nextTurn(self):
if self.__turn == 1:
self.__turn = 2
self.__turnVar.set(self.__whiteNameEntryVar.get())
else:
self.__turn = 1
self.__turnVar.set(self.__redNameEntryVar.get())
self.__curChecker = 0
#Narrative: Sends the space value to evaluations module to evaluate how
# event should be handled within the rules of checkers
#Precondition: Button must be pressed on the checkerboard
#Postcondition: Passes space as argument to find how to proceed
def __activated(self, space):
checkersEvaluation.spaceClicked(self, space)
#Narrative: Gets corresponding image reference for a space's conditions
#Precondition: Must be called and given the space as argument, which is
# a tuple of format (row, column)
#Postcondition: Returns a string value that can be used in the dict of image
# files to reference the correct image based on conditions
# at that space
def __getImageFile(self, space):
r = space[0]
c = space[1]
checker = self.__spaceContents.get(space, 0)
photoFile = ""
if checker:
if checker.getTeam() == 1:
photoFile += "red"
else:
photoFile += "white"
if checker.getIsKing():
photoFile += "King"
if checker == self.__curChecker:
photoFile += "Highlighted"
else:
if r % 2:
if c % 2:
photoFile += "blackBlank"
else:
photoFile += "whiteBlank"
else:
if c % 2:
photoFile += "whiteBlank"
else:
photoFile += "blackBlank"
return photoFile
#Narrative: Updates images on the checkerboard to refleect changes
#Precondition: Must be called
#Postcondition: Gets correct image name for given conditions in each space
# then changes the button's image
def updateBoard(self):
for i in range(64):
r = (i // 8) + 1
c = (i % 8) + 1
space = (r, c)
self.__buttons[space].config(image = \
self.__images[self.__getImageFile(space)])