-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtictactoe.py
318 lines (316 loc) · 9.95 KB
/
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
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
#Astro Ohnuma
#11/1/17
#tictactoe.py - first project, tictactoe without graphics
from random import randint
square1 = 1#setting the variables for the different spaces in the board
square2 = 2
square3 = 3
square4 = 4
square5 = 5
square6 = 6
square7 = 7
square8 = 8
square9 = 9
def printboard():#actually creates the board and sets the spaces in the board to be the square variables
print(' ---','---','---')
print('|',square1,'|',square2,'|',square3,'|')
print(' ---','---','---')
print('|',square4,'|',square5,'|',square6,'|')
print(' ---','---','---')
print('|',square7,'|',square8,'|',square9,'|')
print(' ---','---','---')
def isempty(num1):#checks if a specific space in the board has an X or an O
if num1 == 1:
if square1 == 'X' or square1 == 'O':
return False
else:
return True
elif num1 == 2:
if square2 == 'X' or square2 == 'O':
return False
else:
return True
elif num1 == 3:
if square3 == 'X' or square3 == 'O':
return False
else:
return True
elif num1 == 4:
if square4 == 'X' or square4 == 'O':
return False
else:
return True
elif num1 == 5:
if square5 == 'X' or square5 == 'O':
return False
else:
return True
elif num1 == 6:
if square6 == 'X' or square6 == 'O':
return False
else:
return True
elif num1 == 7:
if square7 == 'X' or square7 == 'O':
return False
else:
return True
elif num1 == 8:
if square8 == 'X' or square8 == 'O':
return False
else:
return True
elif num1 == 9:
if square9 == 'X' or square9 == 'O':
return False
else:
return True
def winner():#checks if someone won the game
if square1 == square2 and square3 == square2:
return True
elif square4 == square5 and square6 == square5:
return True
elif square7 == square8 and square9 == square8:
return True
elif square1 == square5 and square9 == square5:
return True
elif square3 == square5 and square7 == square5:
return True
elif square1 == square4 and square7 == square4:
return True
elif square2 == square5 and square8 == square5:
return True
elif square3 == square6 and square9 == square6:
return True
else:
return False
def fullboard():#checks if the board is full and noone won
if isempty(1) == False and isempty(2) == False and isempty(3) == False and isempty(4) == False and isempty(5) == False and isempty(6) == False and isempty(7) == False and isempty(8) == False and isempty(9) == False:
return True
else:
return False
if __name__ == '__main__':#makes the program work
printboard()#calling the function to print the board
while winner() == False:#checking if someone won the game, if not, starts the first turn or begins another turn
turn = int(input('Where would you like to go?: '))#asks where the player would like to put their X
while isempty(turn) == False:#checks if the space the player chose is empty, if it is, asks the player for another space
turn = int(input('Where would you like to go?: '))
if turn == 1:#checks if the player chose to place an X on space 1, checks if it is empty, sets the space to an X, reprints the board, checks if the move made the player win, checks if the move made the board full
isempty(turn)
square1 = 'X'
printboard()
winner()
if winner() == True:
print('Player Wins! Nice Job!')
break
fullboard()
if fullboard() == True:
print('Full board! Nobody wins!')
break
elif turn == 2:
isempty(turn)
square2 = 'X'
printboard()
winner()
if winner() == True:
print('Player Wins! Nice Job!')
break
fullboard()
if fullboard() == True:
print('Full board! Nobody wins!')
break
elif turn == 3:
isempty(turn)
square3 = 'X'
printboard()
winner()
if winner() == True:
print('Player Wins! Nice Job!')
break
fullboard()
if fullboard() == True:
print('Full board! Nobody wins!')
break
elif turn == 4:
isempty(turn)
square4 = 'X'
printboard()
winner()
if winner() == True:
print('Player Wins! Nice Job!')
break
fullboard()
if fullboard() == True:
print('Full board! Nobody wins!')
break
elif turn == 5:
isempty(turn)
square5 = 'X'
printboard()
winner()
if winner() == True:
print('Player Wins! Nice Job!')
break
fullboard()
if fullboard() == True:
print('Full board! Nobody wins!')
break
elif turn == 6:
isempty(turn)
square6 = 'X'
printboard()
winner()
if winner() == True:
print('Player Wins! Nice Job!')
break
fullboard()
if fullboard() == True:
print('Full board! Nobody wins!')
break
elif turn == 7:
isempty(turn)
square7 = 'X'
printboard()
winner()
if winner() == True:
print('Player Wins! Nice Job!')
break
fullboard()
if fullboard() == True:
print('Full board! Nobody wins!')
break
elif turn == 8:
isempty(turn)
square8 = 'X'
printboard()
winner()
if winner() == True:
print('Player Wins! Nice Job!')
break
fullboard()
if fullboard() == True:
print('Full board! Nobody wins!')
break
elif turn == 9:
isempty(turn)
square9 = 'X'
printboard()
winner()
if winner() == True:
print('Player Wins! Nice Job!')
break
fullboard()
if fullboard() == True:
print('Full board! Nobody wins!')
break
com = randint(1,9)#chooses a random space for the computer to place an O
while isempty(com) == False:#checks if the space is full and if it is, chooses a new random space
com = randint(1,9)
if com == 1:
isempty(com)
square1 = 'O'
printboard()
winner()
if winner() == True:
print('Com Wins! Too Bad..')
break
fullboard()
if fullboard() == True:
print('Full board! Nobody wins!')
break
elif com == 2:
isempty(com)
square2 = 'O'
printboard()
winner()
if winner() == True:
print('Com Wins! Too Bad..')
break
fullboard()
if fullboard() == True:
print('Full board! Nobody wins!')
break
elif com == 3:
isempty(com)
square3 = 'O'
printboard()
winner()
if winner() == True:
print('Com Wins! Too Bad..')
break
fullboard()
if fullboard() == True:
print('Full board! Nobody wins!')
break
elif com == 4:
isempty(com)
square4 = 'O'
printboard()
winner()
if winner() == True:
print('Com Wins! Too Bad..')
break
fullboard()
if fullboard() == True:
print('Full board! Nobody wins!')
break
elif com == 5:
isempty(com)
square5 = 'O'
printboard()
winner()
if winner() == True:
print('Com Wins! Too Bad..')
break
fullboard()
if fullboard() == True:
print('Full board! Nobody wins!')
break
elif com == 6:
isempty(com)
square6 = 'O'
printboard()
winner()
if winner() == True:
print('Com Wins! Too Bad..')
break
fullboard()
if fullboard() == True:
print('Full board! Nobody wins!')
break
elif com == 7:
isempty(com)
square7 = 'O'
printboard()
winner()
if winner() == True:
print('Com Wins! Too Bad..')
break
fullboard()
if fullboard() == True:
print('Full board! Nobody wins!')
break
elif com == 8:
isempty(com)
square8 = 'O'
printboard()
winner()
if winner() == True:
print('Com Wins! Too Bad..')
break
fullboard()
if fullboard() == True:
print('Full board! Nobody wins!')
break
elif com == 9:
isempty(com)
square9 = 'O'
printboard()
winner()
if winner() == True:
print('Com Wins! Too Bad..')
break
fullboard()
if fullboard() == True:
print('Full board! Nobody wins!')
break