-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOthello.py
More file actions
209 lines (185 loc) · 6.76 KB
/
Copy pathOthello.py
File metadata and controls
209 lines (185 loc) · 6.76 KB
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
## Written by Ben Fritzeen
## 12/10/2017
## Othello game to be used in PiDay
## Instantiate game board
## 0: Empty, 1: White, 2: Black
class Othello:
def __init__(self):
self.gameBoard = []
self.placeCtr = 4
self.oneCtr = 2
self.twoCtr = 2
for i in range(8):
self.gameBoard.append([0, 0, 0, 0, 0, 0, 0, 0])
def getSpotState(self, row, col):
return self.gameBoard[row][col]
def setSpotState(self, row, col, state):
self.gameBoard[row][col] = state
self.placeCtr += 1
if state == 1:
self.oneCtr += 1
elif state == 2:
self.twoCtr += 1
def getNumSpotsLeft(self):
return 64 - self.placeCtr
def isWinner(self):
return 0 == self.getNumSpotsLeft()
def whoseTurn(self):
if self.getNumSpotsLeft() % 2 == 0:
return 2
else:
return 1
def playerMove(self, row, col, state):
self.setSpotState(row, col, state)
self.allSwaps = self.checkForSwaps(row, col, state)
for spot in self.allSwaps:
self.gameBoard[spot[0]][spot[1]] = state
if state == 2:
self.twoCtr += len(self.allSwaps)
self.oneCtr -= len(self.allSwaps)
else:
self.oneCtr += len(self.allSwaps)
self.twoCtr -= len(self.allSwaps)
return self.allSwaps
def checkForSwaps(self, row, col, state):
self.allSwaps = []
self.downSwaps = self.checkDownForSwaps(row, col, state)
self.upSwaps = self.checkUpForSwaps(row, col, state)
self.leftSwaps = self.checkLeftForSwaps(row, col, state)
self.rightSwaps = self.checkRightForSwaps(row, col, state)
self.downRightSwaps = self.checkDownRightForSwaps(row, col, state)
self.downLeftSwaps = self.checkDownLeftForSwaps(row, col, state)
self.upRightSwaps = self.checkUpRightForSwaps(row, col, state)
self.upLeftSwaps = self.checkUpLeftForSwaps(row, col, state)
if self.downSwaps != None:
for spot in self.downSwaps:
self.allSwaps.append(spot)
if self.upSwaps != None:
for spot in self.upSwaps:
self.allSwaps.append(spot)
if self.leftSwaps != None:
for spot in self.leftSwaps:
self.allSwaps.append(spot)
if self.rightSwaps != None:
for spot in self.rightSwaps:
self.allSwaps.append(spot)
if self.downRightSwaps != None:
for spot in self.downRightSwaps:
self.allSwaps.append(spot)
if self.downLeftSwaps != None:
for spot in self.downLeftSwaps:
self.allSwaps.append(spot)
if self.upRightSwaps != None:
for spot in self.upRightSwaps:
self.allSwaps.append(spot)
if self.upLeftSwaps != None:
for spot in self.upLeftSwaps:
self.allSwaps.append(spot)
return self.allSwaps
def checkUpRightForSwaps(self, row, col, state):
upRightList = []
ctr = 1
while (row - ctr != -1 and col + ctr != 8):
if self.gameBoard[row - ctr][col + ctr] == state:
if len(upRightList) != 0:
return upRightList
else:
return []
elif self.gameBoard[row - ctr][col + ctr] == 0:
return []
else:
upRightList.append([row - ctr, col + ctr])
ctr += 1
def checkDownLeftForSwaps(self, row, col, state):
downLeftList = []
ctr = 1
while (row + ctr != 8 and col - ctr != -1):
if self.gameBoard[row + ctr][col - ctr] == state:
if len(downLeftList) != 0:
return downLeftList
else:
return []
elif self.gameBoard[row + ctr][col - ctr] == 0:
return []
else:
downLeftList.append([row + ctr, col - ctr])
ctr += 1
def checkUpLeftForSwaps(self, row, col, state):
upLeftList = []
ctr = 1
while (row - ctr != -1 and col - ctr != -1):
if self.gameBoard[row - ctr][col - ctr] == state:
if len(upLeftList) != 0:
return upLeftList
else:
return []
elif self.gameBoard[row - ctr][col - ctr] == 0:
return []
else:
upLeftList.append([row - ctr, col - ctr])
ctr += 1
def checkDownRightForSwaps(self, row, col, state):
downRightList = []
ctr = 1
while (row + ctr != 8 and col + ctr != 8):
if self.gameBoard[row + ctr][col + ctr] == state:
if len(downRightList) != 0:
return downRightList
else:
return []
elif self.gameBoard[row + ctr][col + ctr] == 0:
return []
else:
downRightList.append([row + ctr, col + ctr])
ctr += 1
def checkDownForSwaps(self, row, col, state):
downList = []
for i in range(row + 1, 8):
if self.gameBoard[i][col] == state:
if len(downList) != 0:
return downList
else:
return []
elif self.gameBoard[i][col] == 0:
return []
else:
downList.append([i, col])
def checkUpForSwaps(self, row, col, state):
upList = []
for i in range(row - 1, -1, -1):
if self.gameBoard[i][col] == state:
if len(upList) != 0:
return upList
else:
return []
elif self.gameBoard[i][col] == 0:
return []
else:
upList.append([i, col])
def checkLeftForSwaps(self, row, col, state):
leftList = []
for i in range(col - 1, -1, -1):
if self.gameBoard[row][i] == state:
if len(leftList) != 0:
return leftList
else:
return []
elif self.gameBoard[row][i] == 0:
return []
else:
leftList.append([row, i])
def checkRightForSwaps(self, row, col, state):
rightList = []
for i in range(col + 1, 8):
if self.gameBoard[row][i] == state:
if len(rightList) != 0:
return rightList
else:
return []
elif self.gameBoard[row][i] == 0:
return []
else:
rightList.append([row, i])
def displayBoard(self):
for i in range(8):
print(self.gameBoard[i])