-
Notifications
You must be signed in to change notification settings - Fork 1
/
python_tictactoe.py
247 lines (217 loc) · 7.44 KB
/
python_tictactoe.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
import random as r
gameBoard = []
for x in range(9):
gameBoard.append(' ')
def gameCon():
global gameBoard
global gameActive
gameActive = True
for x in range(9):
gameBoard[x] = ' '
def displayBoard():
global gameBoard
print("\n")
print(gameBoard[0] + " | " + gameBoard[1] + " | " + gameBoard[2])
print("-------------")
print(gameBoard[3] + " | " + gameBoard[4] + " | " + gameBoard[5])
print("-------------")
print(gameBoard[6] + " | " + gameBoard[7] + " | " + gameBoard[8])
print("\n")
def isEmpty(n):
global gameBoard
if gameBoard[n-1] == ' ':
return True
else:
print("This place is already taken.")
return False
def notValid(n):
global gameBoard
if n > 9 or n < 1 or not isEmpty(n):
print("Invalid move.")
return True
else:
return False
def makeMove(n, player):
global gameBoard
gameBoard[n-1] = player
def askPlayer(player):
try:
p = int(input("Player {}, enter an integer: ".format(player)))
if notValid(p):
askPlayer(player)
else:
makeMove(p, player)
pass
except ValueError:
print("You didn't enter any integer. Try again.")
askPlayer(player)
def checkForWinner():
global gameActive
if gameBoard[0] == gameBoard[1] and gameBoard[0] == gameBoard[2] and gameBoard[0] != ' ' and gameActive:
print("The winner is player {}".format(gameBoard[0]))
gameActive = False
elif gameBoard[3] == gameBoard[4] and gameBoard[3] == gameBoard[5] and gameBoard[3] != ' ' and gameActive:
print("The winne is player {}".format(gameBoard[3]))
gameActive = False
elif gameBoard[6] == gameBoard[7] and gameBoard[6] == gameBoard[8] and gameBoard[6] != ' ' and gameActive:
print("The winner is player {}".format(gameBoard[6]))
gameActive = False
elif gameBoard[0] == gameBoard[4] and gameBoard[8] == gameBoard[0] and gameBoard[0] != ' ' and gameActive:
print("The winner is player {}".format(gameBoard[0]))
gameActive = False
elif gameBoard[2] == gameBoard[4] and gameBoard[2] == gameBoard[6] and gameBoard[2] != ' ' and gameActive:
print("The winner is player {}".format(gameBoard[2]))
gameActive = False
elif gameBoard[0] == gameBoard[3] and gameBoard[0] == gameBoard[6] and gameBoard[0] != ' ' and gameActive:
print("The winner is player {}".format(gameBoard[0]))
gameActive = False
elif gameBoard[1] == gameBoard[4] and gameBoard[1] == gameBoard[7] and gameBoard[1] != ' ' and gameActive:
print("The winner is player {}".format(gameBoard[1]))
gameActive = False
elif gameBoard[2] == gameBoard[5] and gameBoard[2] == gameBoard[8] and gameBoard[2] != ' ' and gameActive:
print("The winner is player {}".format(gameBoard[2]))
gameActive = False
def start():
global gameActive
global counter
counter = 0
print("Player 'X' makes the first move. Good luck.")
gameCon()
displayBoard()
counter = 0
while gameActive:
if (counter%2 == 0):
askPlayer('X')
else:
askPlayer('O')
displayBoard()
counter += 1
checkForWinner()
if counter == 9:
if gameActive:
print("The match is a tie.")
gameActive = False
_restart()
def _isEmpty(n):
global gameBoard
if gameBoard[n-1] == ' ':
return True
else:
return False
def _notValid(n):
global gameBoard
if n > 9 or n < 1 or not _isEmpty(n):
return True
else:
return False
def randomMove(player):
global nv
nv = True
a = r.randrange(1, 10)
while _notValid(a):
randomMove(player)
print("The computer has made a movea at {}".format(a))
gameBoard[a-1] = player
def _start():
global counter
counter = 0
global gameActive
gameCon()
try:
a = int(input("Press 1 to play with the computer; otherwise press 2... "))
if (a == 1):
b = int(input("Press 1 to choose 'X', otherwise press 2: "))
r = 0
if b == 1:
while (gameActive):
if (counter % 2 == 0):
askPlayer('X')
else:
_ai('X', 'O')
displayBoard()
counter += 1
checkForWinner()
if counter == 9:
if gameActive:
print("The match is a tie.")
gameActive = False
elif b == 2:
while (gameActive):
if counter % 2 == 0:
_ai('O', 'X')
else:
askPlayer('O')
displayBoard()
counter += 1
checkForWinner()
if counter == 9:
if gameActive:
print("The match is a tie.")
gameActive = False
else:
print("Enter the right key. Try again.")
_start()
elif (a == 2):
start()
else:
print("Invaid input. Try again.")
_start()
pass
except ValueError:
print("Invalid input. Try again...")
_start()
_restart()
def _restart():
global gameActive
if not gameActive:
try:
d = str(input("Do you want to play again? Press 'y'if 'yes' or 'n' if not...."))
if d == 'y' or d == 'Y':
gameActive = True
_start()
elif d == 'n' or d == 'N':
print('\tExiting game')
exit()
else:
print("Please enter the correct input.")
_restart()
pass
except ValueError:
print("The computer is unable to understad your input. Try again.")
_restart()
def _checkForWinner():
global gameActive
if gameBoard[0] == gameBoard[1] and gameBoard[0] == gameBoard[2] and gameBoard[0] != ' ':
return True
elif gameBoard[3] == gameBoard[4] and gameBoard[3] == gameBoard[5] and gameBoard[3] != ' ':
return True
elif gameBoard[6] == gameBoard[7] and gameBoard[6] == gameBoard[8] and gameBoard[6] != ' ':
return True
elif gameBoard[0] == gameBoard[4] and gameBoard[8] == gameBoard[0] and gameBoard[0] != ' ':
return True
elif gameBoard[2] == gameBoard[4] and gameBoard[2] == gameBoard[6] and gameBoard[2] != ' ':
return True
elif gameBoard[0] == gameBoard[3] and gameBoard[0] == gameBoard[6] and gameBoard[0] != ' ':
return True
elif gameBoard[1] == gameBoard[4] and gameBoard[1] == gameBoard[7] and gameBoard[1] != ' ':
return True
elif gameBoard[2] == gameBoard[5] and gameBoard[2] == gameBoard[8] and gameBoard[2] != ' ':
return True
win1 = False
def _ai(player, antiplayer):
global win1
global gameBoard
for x in range(9):
if _isEmpty(x+1):
gameBoard[x] = player
if _checkForWinner():
gameBoard[x] = antiplayer
print("The computer has made a move at {}".format(x+1))
win1 = True
break
else:
gameBoard[x] = ' '
if not win1:
randomMove(antiplayer)
gameCon()
_start()