-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
302 lines (269 loc) · 8.59 KB
/
main.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
#!/usr/bin/python3.7
import random
lBaseBoard = [ ["| ", "| ", "| "], ["| ", "| ", "| "], ["| ", "| ", "| "]]
lBoard = [ ["| ", "| ", "| "], ["| ", "| ", "| "], ["| ", "| ", "| "]]
bTwoPlayer = False
bBotGoesFirst = False
def printBoard():
global lBoard
iCount = 1
print(" 1 2 3 ")
for lRow in lBoard:
for sCell in lRow:
print(sCell, end="")
print("| "+str(iCount)+"\n")
iCount += 1
def getBoardCopy(board):
duplicateBoard = [[],[],[]]
for x in range(3):
for y in range(3):
duplicateBoard[x].append(board[x][y])
return duplicateBoard
def checkVictory(iRow, iColumn):
global lBoard, lBaseBoard, sPlay, sBotGame, bTwoPlayer
bSomeoneWon = False
bSkipLoop = False
if lBoard[0][iColumn] == lBoard[1][iColumn] == lBoard[2][iColumn]:
bSomeoneWon = True
elif lBoard[iRow][0] == lBoard[iRow][1] == lBoard[iRow][2]:
bSomeoneWon = True
elif iRow == iColumn and (lBoard[0][0] == lBoard[1][1] == lBoard[2][2]):
#0,0 1,1 2,2
bSomeoneWon = True
elif iRow+iColumn == 2 and (lBoard[0][2] == lBoard[1][1] == lBoard[2][0]):
#0,2 1,1 2,0
bSomeoneWon = True
if bSomeoneWon :
bSkipLoop = True
if lBoard[iRow][iColumn] == "|X":
print("Player 1 (X) WON the game! Gratz")
elif lBoard[iRow][iColumn] == "|O":
print("Player 2 (O) WON the game! Gratz")
sPlay = input("Play again? (yes/no): ")
if(sPlay == "yes" or sPlay == "Yes"):
sBotGame = input("Do you want to play with a bot? (yes/no): ")
if(sBotGame == "Yes" or sBotGame == "yes"):
bTwoPlayer = False
bBotGoesFirst = botGoesFirst()
if(bBotGoesFirst):
print("The bot goes first (x)")
else:
print("You go first (x)")
else:
bTwoPlayer = True
lBoard = getBoardCopy(lBaseBoard)
printBoard()
return bSkipLoop
def checkDraw():
global lBoard, lBaseBoard, sPlay, sBotGame, bTwoPlayer
bSkipLoop = False
iFreeCells = 0
for lRow in lBoard:
for sCell in lRow:
if sCell == "| ":
iFreeCells += 1
if iFreeCells == 0:
bSkipLoop = True
print("The table is full. It's a DRAW!")
sPlay = input("Play again? (yes/no): ")
if(sPlay == "yes" or sPlay == "Yes"):
sBotGame = input("Do you want to play with a bot? (yes/no): ")
if(sBotGame == "Yes" or sBotGame == "yes"):
bTwoPlayer = False
bBotGoesFirst = botGoesFirst()
if(bBotGoesFirst):
print("The bot goes first (x)")
else:
print("You go first (x)")
else:
bTwoPlayer = True
lBoard = getBoardCopy(lBaseBoard)
printBoard()
#if 2 empty cells check neighbhours. check conditions
return bSkipLoop
def playerOneTurn():
global lBoard
print("Player 1 turn (X)!")
iRow = int(input("Enter Row:"))
iColumn = int(input("Enter Column:"))
if iRow >= 1 and iRow <= 3 and iColumn >= 1 and iColumn <= 3 and lBoard[iRow-1][iColumn-1] == "| ":
lBoard[iRow-1][iColumn-1] = "|X"
printBoard()
else:
print("Wrong move! Come Again.")
playerOneTurn()
bIWon = checkVictory(iRow-1,iColumn-1)
if bIWon:
return bIWon
else:
return checkDraw()
def playerTwoTurn():
global lBoard
print("Player 2 turn (O)!")
iRow = int(input("Enter Row:"))
iColumn = int(input("Enter Column:"))
if iRow >= 1 and iRow <= 3 and iColumn >= 1 and iColumn <= 3 and lBoard[iRow-1][iColumn-1] == "| ":
lBoard[iRow-1][iColumn-1] = "|O"
printBoard()
else:
print("Wrong move! Come Again.")
playerTwoTurn()
bIWon = checkVictory(iRow-1,iColumn-1)
if bIWon:
return bIWon
else:
return checkDraw()
def victoryCheckForBot(lBoardCopy, iRow, iColumn, sMove):
if(lBoardCopy[iRow][iColumn] != "| "):
return False
else:
lBoardCopy[iRow][iColumn] = sMove
if lBoardCopy[0][iColumn] == lBoardCopy[1][iColumn] == lBoardCopy[2][iColumn]:
return True
elif lBoardCopy[iRow][0] == lBoardCopy[iRow][1] == lBoardCopy[iRow][2]:
return True
elif iRow == iColumn and (lBoardCopy[0][0] == lBoardCopy[1][1] == lBoardCopy[2][2]):
#0,0 1,1 2,2
return True
elif iRow+iColumn == 2 and (lBoardCopy[0][2] == lBoardCopy[1][1] == lBoardCopy[2][0]):
#0,2 1,1 2,0
return True
else:
return False
def makeBotMove(iRow, iColumn, sBotLetter):
global lBoard
print("The moved to the row: " + str(iRow+1) + " and the column: " + str(iColumn+1))
lBoard[iRow][iColumn] = sBotLetter
printBoard()
bIWon = checkVictory(iRow,iColumn)
if bIWon:
return bIWon
else:
return checkDraw()
def botTurn(bBotGoesFirst):
global lBoard
if(bBotGoesFirst):
sBotLetter = "|X"
sPlayerLetter = "|O"
else:
sBotLetter = "|O"
sPlayerLetter = "|X"
bFound = False
#check if we can win the next move
for x in range(3):
for y in range(3):
lBoardCopy = getBoardCopy(lBoard)
if(victoryCheckForBot(lBoardCopy, x, y, sBotLetter)):
iRow = x
iColumn = y
bFound = True
break
if bFound:
break
#check if the player can win the next move
if( not bFound):
for x in range(3):
for y in range(3):
lBoardCopy = getBoardCopy(lBoard)
if(victoryCheckForBot(lBoardCopy, x, y, sPlayerLetter)):
iRow = x
iColumn = y
bFound = True
break
if bFound:
break
#take the opposite corner from the player
if(not bFound):
if(lBoard[0][0] == sPlayerLetter and lBoard[2][2] == "| "):
iRow = 2
iColumn = 2
bFound = True
elif(lBoard[2][2] == sPlayerLetter and lBoard[0][0] == "| " ):
iRow = 0
iColumn = 0
bFound = True
elif(lBoard[0][2] == sPlayerLetter and lBoard[2][0] == "| " ):
iRow = 2
iColumn = 0
bFound = True
elif(lBoard[2][0] == sPlayerLetter and lBoard[0][2] == "| " ):
iRow = 0
iColumn = 2
bFound = True
#take center if it's free
if(not bFound):
if(lBoard[1][1] == "| "):
iRow = 1
iColumn = 1
bFound = True
#take corners if they are free
if (not bFound):
if(lBoard[0][0] == "| "):
iRow = 0
iColumn = 0
bFound = True
elif(lBoard[0][2] == "| "):
iRow = 0
iColumn = 2
bFound = True
elif(lBoard[2][0] == "| "):
iRow = 2
iColumn = 0
bFound = True
elif(lBoard[2][2] == "| "):
iRow = 2
iColumn = 2
bFound = True
#if nothing works move to the sides
if (not bFound):
if(lBoard[0][1] == "| "):
iRow = 0
iColumn = 1
bFound = True
elif(lBoard[1][0] == "| "):
iRow = 1
iColumn = 0
bFound = True
elif(lBoard[1][2] == "| "):
iRow = 1
iColumn = 2
bFound = True
elif(lBoard[2][1] == "| "):
iRow = 2
iColumn = 1
bFound = True
return makeBotMove(iRow, iColumn, sBotLetter)
def botGoesFirst():
if (random.randint(0,1) == 0):
return True
else:
return False
printBoard()
sPlay = input("Do you want to play? (yes/no): ")
if(sPlay == "yes" or sPlay == "Yes"):
sBotGame = input("Do you want to play with a bot? (yes/no): ")
if(sBotGame == "Yes" or sBotGame == "yes"):
bTwoPlayer = False
bBotGoesFirst = botGoesFirst()
if(bBotGoesFirst):
print("The bot goes first (x)")
else:
print("You go first (x)")
else:
bTwoPlayer = True
while (sPlay == "yes" or sPlay == "Yes"):
if(bTwoPlayer):
if(playerOneTurn()):
continue
if(playerTwoTurn()):
continue
elif (bBotGoesFirst):
if(botTurn(bBotGoesFirst)):
continue
if(playerTwoTurn()):
continue
else:
if(playerOneTurn()):
continue
if(botTurn(bBotGoesFirst)):
continue