-
Notifications
You must be signed in to change notification settings - Fork 5
/
tic_tac_toe.py
344 lines (264 loc) · 10.1 KB
/
tic_tac_toe.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
from random import randint
####################################
# DEVELOPER : VIKRAM SINGH #
# TECHNOLOGY STACK : PYTHON #
####################################
# Evaluation Definitions
X_WINS = 1000
O_WINS = -1000
DRAW = 0
class TicTacToe():
'''
ALGO: MIN-MAX
By making any of the move if in the upcoming possibilities if opponent has even
one single winning move and all loosing move then I AM LOST.
If opponent has all lost moves then I win.
'''
def __init__(self):
self.outcomes = []
self.gameBoard = ['.'] * 9
self.coinsType = ['x', 'o']
self.gameOn = True
self.computerPlay = 0
self.computerType = '-'
self.humanType = '-'
def _checkIfWinner(self, playerType):
# check verticals
if self.gameBoard[0] == self.gameBoard[3] and self.gameBoard[0] == self.gameBoard[6] and self.gameBoard[0] == playerType:
return True
if self.gameBoard[1] == self.gameBoard[4] and self.gameBoard[1] == self.gameBoard[7] and self.gameBoard[1] == playerType:
return True
if self.gameBoard[2] == self.gameBoard[5] and self.gameBoard[2] == self.gameBoard[8] and self.gameBoard[2] == playerType:
return True
# check horizontals
if self.gameBoard[0] == self.gameBoard[1] and self.gameBoard[0] == self.gameBoard[2] and self.gameBoard[0] == playerType:
return True
if self.gameBoard[3] == self.gameBoard[4] and self.gameBoard[3] == self.gameBoard[5] and self.gameBoard[3] == playerType:
return True
if self.gameBoard[6] == self.gameBoard[7] and self.gameBoard[6] == self.gameBoard[8] and self.gameBoard[6] == playerType:
return True
# check diagonal
if self.gameBoard[0] == self.gameBoard[4] and self.gameBoard[8] == self.gameBoard[0] and self.gameBoard[0] == playerType:
return True
if self.gameBoard[2] == self.gameBoard[4] and self.gameBoard[2] == self.gameBoard[6] and self.gameBoard[2] == playerType:
return True
return False
def _switchPlayerType(self, playerType):
if playerType == 'x':
return 'o'
return 'x'
def _findPlayerTypeWins(self, playerType):
if playerType == 'x':
return X_WINS
return O_WINS
def _isBoardFull(self):
if '.' in self.gameBoard:
return False
return True
def _checkGameOver(self):
if self._checkIfWinner('x'):
return (True, 'x')
if self._checkIfWinner('o'):
return (True, 'o')
if self._isBoardFull():
return (True, 'd')
return (False, '.')
def _validPositionToEnterCoin(self, pos):
return self.gameBoard[pos] == '.'
def _positionEvaluation(self, playerType):
if self._checkIfWinner(playerType):
return self._findPlayerTypeWins(playerType)
if self._checkIfWinner(self._switchPlayerType(playerType)):
return self._findPlayerTypeWins(self._switchPlayerType(playerType))
return DRAW
def _findMove(self, depth, playerType):
ans = 0
possibilities = [-1] * 9
if self._positionEvaluation(playerType) != DRAW:
return (possibilities, self._positionEvaluation(playerType))
if self._isBoardFull():
return (possibilities, self._positionEvaluation(playerType))
for idx, e in enumerate(self.gameBoard):
if e == '.':
self.gameBoard[idx] = playerType
res = self._findMove(depth + 1, self._switchPlayerType(playerType))
possibilities[idx] = res[1]
self.gameBoard[idx] = '.'
if self._findPlayerTypeWins(playerType) in possibilities:
ans = self._findPlayerTypeWins(playerType)
elif DRAW in possibilities:
ans = DRAW
else:
ans = self._findPlayerTypeWins(self._switchPlayerType(playerType))
return (possibilities, ans)
def _printGameBoard(self):
print "\n 0 1 2 | " + self.gameBoard[0] + " " + self.gameBoard[1] + " " + self.gameBoard[2]
print " 3 4 5 | " + self.gameBoard[3] + " " + self.gameBoard[4] + " " + self.gameBoard[5]
print " 6 7 8 | " + self.gameBoard[6] + " " + self.gameBoard[7] + " " + self.gameBoard[8]
print "\n"
return
def _getPossibilitiesInterpretation(self, playerType):
possibilities = ["You Win" if possibility == self._findPlayerTypeWins(playerType) else possibility for possibility in self.outcomes]
possibilities = ["You Loose" if possibility == self._findPlayerTypeWins(self._switchPlayerType(playerType)) else possibility for possibility in possibilities]
interpreatation = zip([i for i in range(9)], ["Draw" if possibility == 0 else possibility for possibility in possibilities])
return interpreatation
def _printPossibilitiesInterpretation(self, playerType):
interpreatation = self._getPossibilitiesInterpretation(playerType)
for tup in interpreatation:
print tup[0], " : ", tup[1]
print
return
def _getBestPossibleIndexForPlaying(self, playerType):
interpreatation = self._getPossibilitiesInterpretation(playerType)
# first check if winning possible
res = [tup[0] for tup in interpreatation if tup[1] == "You Win"]
if res:
return res[randint(0, len(res)-1)]
# then check if draw possible
res = [tup[0] for tup in interpreatation if tup[1] == "Draw"]
if res:
return res[randint(0, len(res)-1)]
# last possible
res = [tup[0] for tup in interpreatation if tup[1] == "You Loose"]
if res:
return res[randint(0, len(res)-1)]
return -1
def _startGameLoop(self):
# Game Loop till it ends or crashes
while(self.gameOn):
humanValidPos = False
self._printGameBoard()
pos = input("Enter position for `" + self.humanType + "` : ")
while not humanValidPos:
if not self._validPositionToEnterCoin(pos):
pos = input("Please enter valid position for `" + self.humanType + "` : ")
else:
humanValidPos = True
self.gameBoard[pos] = self.humanType
# Checking If game over
result = self._checkGameOver()
if result[0]:
self._printGameBoard()
if result[1] == 'd':
print "Game Over ! Result => DRAW"
else:
if self.computerType == result[1]:
print "Game Over ! Result => Computer Wins"
else:
print "Game Over ! Result => Human Wins, Actually just writing, even though it will never happen"
return 1
self.outcomes = self._findMove(1, self.computerType)[0]
print
print self.outcomes
self._printPossibilitiesInterpretation(self.computerType)
pos = self._getBestPossibleIndexForPlaying(self.computerType)
self.gameBoard[pos] = self.computerType
# Checking If game over
result = self._checkGameOver()
if result[0]:
self._printGameBoard()
if result[1] == 'd':
print "Game Over ! Result => DRAW"
else:
if self.computerType == result[1]:
print "Game Over ! Result => Computer Wins"
else:
print "Game Over ! Result => Human Wins, Actually just writing, even though it will never happen"
return 1
return 0
def startNewGameOnTerminal(self):
self.gameBoard = ['.'] * 9;
self.computerPlay = input("\nWho starts first ?\n 1: Computer\n 2: Player\n\n ")
if self.computerPlay == 1:
self.computerType = self.coinsType[randint(0,1)] # Random Selection
self.humanType = self._switchPlayerType(self.computerType)
print "\nComputer Selects : ", self.computerType
print "You are now : ", self.humanType
# Computer Making First Move
firstPossibilities = [0, 2, 4, 6, 8] #best possible moves, though all are equal if played optimally.
pos = firstPossibilities[randint(0, len(firstPossibilities)-1)]
self.gameBoard[pos] = self.computerType
else:
self.humanType = raw_input("Choose your coin type [`x` / `o`]: ")
self.computerType = self._switchPlayerType(self.humanType)
print "\nComputer is : ", self.computerType
print "You are : ", self.humanType
self._startGameLoop()
return
def robotsNextMove(self, humanMove):
# while not humanValidPos:
# if not self._validPositionToEnterCoin(pos):
# pos = input("Please enter valid position for `" + self.humanType + "` : ")
# else:
# humanValidPos = True
response = ""
returnMove = -1
self.gameBoard[humanMove] = self.humanType
self._printGameBoard()
# Checking If game over
result = self._checkGameOver()
if result[0]:
if result[1] == 'd':
print "Game Over ! Result => DRAW"
response = "DR"
else:
if self.computerType == result[1]:
print "Game Over ! Result => Computer Wins"
response = "CW"
else:
print "Game Over ! Result => Human Wins, Actually just writing, even though it will never happen"
response = "HW"
return (True, returnMove, response)
self.outcomes = self._findMove(1, self.computerType)[0]
print
print self.outcomes
self._printPossibilitiesInterpretation(self.computerType)
pos = self._getBestPossibleIndexForPlaying(self.computerType)
returnMove = pos
self.gameBoard[pos] = self.computerType
self._printGameBoard()
# Checking If game over
result = self._checkGameOver()
if result[0]:
if result[1] == 'd':
print "Game Over ! Result => DRAW"
response = "DR"
else:
if self.computerType == result[1]:
print "Game Over ! Result => Computer Wins"
response = "CW"
else:
print "Game Over ! Result => Human Wins, Actually just writing, even though it will never happen"
response = "HW"
return (True, returnMove, response)
return (False, returnMove, response)
def startNewGameWithRobot(self, playerOne, playerType):
self.gameBoard = ['.'] * 9
pos = -1
if playerOne == "COMPUTER":
self.computerType = self.coinsType[randint(0,1)] # Random Selection
self.humanType = self._switchPlayerType(self.computerType)
print "\nComputer Selects : ", self.computerType
print "You are now : ", self.humanType
# Computer Making First Move
firstPossibilities = [0, 2, 4, 6, 8] #best possible moves, though all are equal if played optimally.
pos = firstPossibilities[randint(0, len(firstPossibilities)-1)]
self.gameBoard[pos] = self.computerType
self._printGameBoard()
if playerOne == "HUMAN":
self.humanType = playerType
self.computerType = self._switchPlayerType(self.humanType)
print "\nComputer is : ", self.computerType
print "You are : ", self.humanType
self._printGameBoard()
dic = {}
dic["computerMove"] = pos
dic["computerType"] = self.computerType
dic["humanType"] = self.humanType
return dic
def run(self):
self.startNewGameOnTerminal()
return
if __name__ == "__main__":
game = TicTacToe()
game.run()