-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOthello.py
354 lines (319 loc) · 12.4 KB
/
Othello.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
'''
Daniel Wen
This project is an implementation of the game Othello.
There are four player/opponent types: random choice, human choice,
moves chosen by a minimax algorithm, and moves chosen by an alpha-beta
algorithm.
The minimax and alpha-beta algirthms are opitmized such that they will always
win against a random choice opponent. To play, simply copy and past the following
board setup:
...........................OX......XO...........................
'''
import random
def main():
inp = input("Input a starting state: ")
print("Player Type Options: \"random\", \"human\", \"minimax\", \"alpha-beta\"")
type1 = input("Input Player 1 type: ")
type2 = input("Input Player 2 type: ")
for i in range(0, 50):
initial = Board(inp, type1, type2)
initial.printBoard()
initial.playGame(initial.player1, initial.player2)
class Player:
symbol = ""
positions = set()
board = dict()
type = ""
directions = [-11, -10, -9, -1, 1, 9, 10, 11]
possibleMoves = set()
start = ""
def __init__(self, b, state, s, t):
self.symbol = s
self.positions = set()
self.start = state
self.parent = b
for i in range(0, len(state)):
if state[i] == self.symbol:
self.positions.add(i)
self.board = state
self.type = t
def copy(self):
return Player(self.parent, self.start, self.symbol, self.type)
def findLegalMoves(self):
self.possibleMoves = set()
for pos in self.positions:
for i in self.directions:
temp = pos + i
if self.board[temp] == "W" or self.board[temp] == self.symbol or self.board[temp] == ".":
continue
while self.board[temp] != "W" and self.board[temp] != self.symbol and self.board[temp] != ".":
temp = temp + i
if self.board[temp] == ".":
self.possibleMoves.add(temp)
return self.possibleMoves
def chooseMove(self):
self.findLegalMoves()
if len(self.possibleMoves) == 0:
return -1
elif self.type == "random":
return random.choice(list(self.possibleMoves))
elif self.type == "human":
print("Possible moves: " + str(self.possibleMoves))
move = input("Where will Player " + self.symbol + " move? ")
return int(move)
elif self.type == "minimax":
return self.parent.minimax(self.symbol, None, self.parent, 0, 0)[1]
else:
return self.parent.alpha_beta_search(self.symbol, self.parent)
def update(self, b, state):
self.parent = b
self.positions = set()
board = state
for i in range(0, len(state)):
if board[i] == self.symbol:
self.positions.add(i)
class Board:
turn = 0
player1 = None
player2 = None
board = dict()
def __init__(self, str, t1, t2):
self.state = str
if len(str) == 64:
self.board = self.makeBoard(str)
else:
self.board = dict()
for i in range(0, 100):
self.board[i] = self.state[i]
self.player1 = Player(self, self.board, "X", t1)
self.player2 = Player(self, self.board, "O", t2)
def makeBoard(self, string):
ret = {}
for i in range(0, 100):
ret[i] = "."
for i in range(0, 10):
ret[i] = "W"
for i in range(10, 90, 10):
ret[i] = "W"
ret[i + 9] = "W"
for j in range(i + 1, i + 9):
ret[j] = string[(int(i / 10) - 1) * 8 + j - i - 1]
for i in range(90, 100):
ret[i] = "W"
return ret
def printBoard(self):
string = self.board
for i in range(11, 90):
if i % 10 == 0:
print()
if string[i] != "W":
print(string[i], end=' ')
print()
def isComplete(self):
set1 = self.player1.findLegalMoves()
set2 = self.player2.findLegalMoves()
if len(set1) == 0 and len(set2) == 0:
return True
if len(self.player1.positions) == 0 or len(self.player2.positions) == 0:
return True
return False
def difference(self, char):
if char == "X":
char1 = "O"
else:
char1 = "X"
count1 = 0
count2 = 0
for i in range(0, 100):
if self.board[i] == char:
count1 += 1
elif self.board[i] == char1:
count2 += 1
return count1 - count2
def updateBoard(self, move, symbol):
directions = [-11, -10, -9, -1, 1, 9, 10, 11]
self.board[move] = symbol
for i in directions:
temp = move + i
current = set()
while self.board[temp] != symbol and self.board[temp] != "W" and self.board[temp] != ".":
current.add(temp)
temp = temp + i
if self.board[temp] == symbol:
for c in current:
self.board[c] = symbol
self.player1.update(self, self.board)
self.player2.update(self, self.board)
self.state = ""
for i in range(0, len(self.board)):
self.state += self.board[i]
def playGame(self, player1, player2):
turn = 0
while not self.isComplete():
if turn == 0:
print("Player X's move: ")
move1 = player1.chooseMove()
if move1 == -1:
print("No possible moves, skip turn.")
turn = (turn + 1) % 2
continue
self.updateBoard(move1, "X")
turn = (turn + 1) % 2
else:
print("Player O's move: ")
move2 = player2.chooseMove()
if move2 == -1:
print("No possible moves, skip turn.")
turn = (turn + 1) % 2
continue
self.updateBoard(move2, "O")
turn = (turn + 1) % 2
self.printBoard()
countx = 0
counto = 0
for i in range(0, len(self.board)):
if self.board[i] == "X":
countx += 1
elif self.board[i] == "O":
counto += 1
print("Score: " + str(countx) + " X pieces " + str(counto) + " O pieces")
if countx == counto:
print("It's a tie!")
elif countx > counto:
print("Player X wins!")
else:
print("Player O wins!")
def generateChildren(self, char):
if char == "X":
return self.player1.findLegalMoves()
else:
return self.player2.findLegalMoves()
def copy(self):
return Board(self.state, self.player1.copy(), self.player2.copy())
def newState(self, i, char):
tboard = self.copy()
tboard.updateBoard(i, char)
return tboard
def minimax(self, char, m, state, turn, end):
if char == "X":
char2 = "O"
else:
char2 = "X"
if end - turn == 3:
if state.startGame():
return (state.startGameEval(char), m)
return (state.difference(char), m)
if end % 2 == 0 and char == "X" and len(state.player1.findLegalMoves()) == 0:
return state.minimax(char, m, state, turn, end + 1)
if end % 2 == 0 and char == "O" and len(state.player2.findLegalMoves()) == 0:
return state.minimax(char, m, state, turn, end + 1)
if end % 2 == 1 and char2 == "X" and len(state.player1.findLegalMoves()) == 0:
return state.minimax(char, m, state, turn, end + 1)
if end % 2 == 1 and char2 == "O" and len(state.player2.findLegalMoves()) == 0:
return state.minimax(char, m, state, turn, end + 1)
else:
if end % 2 == 0:
list = state.generateChildren(char)
max = -9999
maxindex = 0
for i in list:
w = state.minimax(char, i, state.newState(i, char), turn, end + 1)[0]
if w > max:
max = w
maxindex = i
#if maxindex == 0:
#return (-9999, m)
return (max, maxindex)
else:
list = state.generateChildren(char2)
min = 9999
minindex = 0
for i in list:
w = state.minimax(char, i, state.newState(i, char2), turn, end + 1)[0]
if w < min:
min = w
minindex = i
#if minindex == 0:
#return (9999, m)
return (min, minindex)
def alphabeta(self, char, m, state, alpha, beta, turn, end):
if char == "X":
char2 = "O"
else:
char2 = "X"
if end - turn == 4:
if state.startGame():
return (state.startGameEval(char), m)
return (state.difference(char), m)
if end % 2 == 0 and char == "X" and len(state.player1.findLegalMoves()) == 0:
return state.alphabeta(char, m, state, alpha, beta, turn, end + 1)
if end % 2 == 0 and char == "O" and len(state.player2.findLegalMoves()) == 0:
return state.alphabeta(char, m, state, alpha, beta, turn, end + 1)
if end % 2 == 1 and char2 == "X" and len(state.player1.findLegalMoves()) == 0:
return state.alphabeta(char, m, state, alpha, beta, turn, end + 1)
if end % 2 == 1 and char2 == "O" and len(state.player2.findLegalMoves()) == 0:
return state.alphabeta(char, m, state, alpha, beta, turn, end + 1)
else:
if end % 2 == 0:
list = state.generateChildren(char)
maxindex = 0
v = float("-inf")
for i in list:
temp = state.alphabeta(char, i, state.newState(i, char), alpha, beta, turn, end + 1)
if temp[0] > v:
v = temp[0]
maxindex = i
if v > beta:
return (v, maxindex)
return(v, maxindex)
else:
list = state.generateChildren(char2)
minindex = 0
v = float('inf')
for i in list:
temp = state.alphabeta(char, i, state.newState(i, char2), alpha, beta, turn, end + 1)
if temp[0] < v:
v = temp[0]
minindex = i
if v < alpha:
return (v, minindex)
return (v, minindex)
def alpha_beta_search(self, char, state):
v = state.alphabeta(char, None, state, float("-inf"), float("inf"), 0, 0)
return v[1]
def startGameEval(self, char):
sides = [13, 14, 15, 16, 31, 41, 51, 61, 38, 48, 58, 68]
count1 = 0
count2 = 0
if char == "X":
char2 = "O"
else:
char2 = "X"
for i in range(0, len(self.board)):
if self.board[i] == char:
count1 += 1
if i == 11 or i == 18 or i == 81 or i == 88:
count1 += 100
if i in sides:
count1 += 10
elif self.board[i] == char2:
count2 += 1
if i == 11 or i == 18 or i == 81 or i == 88:
count2 += 100
if i in sides:
count2 += 10
if char == "X":
count1 += 10 * len(self.player1.findLegalMoves())
count2 += 10 * len(self.player2.findLegalMoves())
else:
count2 += 10 * len(self.player1.findLegalMoves())
count1 += 10 * len(self.player2.findLegalMoves())
return count1 - count2
def startGame(self):
count = 0
for i in range(0, len(self.board)):
if self.board[i] == "X" or self.board[i] == "O":
count += 1
return count < 58
if __name__ == '__main__':
main()