repl.it
Python

No description

fork
loading
Files
  • main.py
  • text based game
  • tic tac toe
  • repittictactoe.txt
  • worksinidle.txt
  • Packager files
  • requirements.txt
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
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
import replit, time, random, secrets

#Initialize Variables
one = "1"
two = "2"
three = "3"
four = "4"
five = "5"
six = "6"
seven = "7"
eight = "8"
nine = "9"
run = 1
winner = "0"
player = 1
availableList = ["1", "2", "3", "4", "5", "6", "7", "8", "9"]
count = 0

#Win condition for Player 1 (x)
def win_x():
	global winner
	if one == "x" and two == "x" and three == "x":
		winner = "1"
		run = 0
	elif one == "x" and four == "x" and seven == "x": 
		winner = "1"
		run = 0
	elif one == "x" and five == "x" and nine == "x":
		winner = "1"
		run = 0
	elif four == "x" and five == "x" and six == "x": 
		winner = "1"
		run = 0
	elif seven == "x" and eight == "x" and nine == "x":
		winner = "1"
		run = 0
	elif two == "x" and five == "x" and eight == "x":
		winner = "1"
		run = 0
	elif three == "x" and five == "x" and seven == "x": 
		winner = "1"
		run = 0
	elif three == "x" and six == "x" and nine == "x":
		winner = "1"
		run = 0
	
	return winner;


#Win condition for Player 2/Computer
def win_o():
	global winner
	if one == "o" and two == "o" and three == "o":
		winner = "2"
		run = 0
	elif one == "o" and four == "o" and seven == "o": 
		winner = "2"
		run = 0
	elif one == "o" and five == "o" and nine == "o":
		winner = "2"
		run = 0
	elif four == "o" and five == "o" and six == "o": 
		winner = "2"
		run = 0
	elif seven == "o" and eight == "o" and nine == "o":
		winner = "2"
		run = 0
	elif two == "o" and five == "o" and eight == "o":
		winner = "2"
		run = 0
	elif three == "o" and five == "o" and seven == "o": 
		winner = "2"
		run = 0
	elif three == "o" and six == "o" and nine == "o":
		winner = "2"
		run = 0

	return winner;
	
		
#Asks if you want to play against a computer, or against a friend.
opponent = int(input("Would you like to play against a friend (1) or a computer (2)?"))

#
while True:
	replit.clear()
	winner = win_x()
	winner = win_o()
	
	if winner == "1" or winner == "2":
		break

	if count == 9:
		break
	
	print(one, "|", two, "|", three, "\n" + four, "|", five, "|", six, "\n" + seven, "|", eight, "|", nine, "\n",)

	print("Player", str(player), " turn!")

	if player == 1:
		turn = input("Which square would you like to go in? ")
		
		if turn not in availableList:
			print("Invalid entry!")
			time.sleep(1)
			
		else:
			availableList.remove(turn)
			
			if turn == "1":
				one = "x"
			
			elif turn == "2":
				two = "x"
			
			elif turn == "3":
				three = "x"
			
			elif turn == "4":
				four = "x"
			
			elif turn == "5":
				five = "x"
			
			elif turn == "6":
				six = "x"
			
			elif turn == "7":
				seven = "x"
			
			elif turn == "8":
				eight = "x"
			
			elif turn == "9":
				nine = "x"
			
			player = 2
			count += 1

	elif player == 2 and opponent == 1:
		
		turn = input("Which square would you like to go in? ")
		
		if turn not in availableList:
			print("Invalid entry!")
			time.sleep(1)
		
		else:
			availableList.remove(turn)
			
			if turn == "1":
				one = "o"
			
			elif turn == "2":
				two = "o"
			
			elif turn == "3":
				three = "o"
			
			elif turn == "4":
				four = "o"
			
			elif turn == "5":
				five = "o"
			
			elif turn == "6":
				six = "o"
			
			elif turn == "7":
				seven = "o"
			
			elif turn == "8":
				eight = "o"
			
			elif turn == "9":
				nine = "o"
			
			player = 1
			count += 1
			
	elif player == 2 and opponent == 2:
		
		if count == 1 and "5" in availableList:
			turn = "5"
		
		elif one == "o" and two == "o" and "3" in availableList:
			turn = "3"
		
		elif one == "o" and four == "o" and "7" in availableList: 
			turn = "7"
		
		elif one == "o" and five == "o" and "9" in availableList:
			turn = "9"
		
		elif four == "o" and five == "o" and "6" in availableList:
			turn = "6" 
		
		elif seven == "o" and eight == "o" and "9" in availableList:
			turn = "9"
		
		elif two == "o" and five == "o" and "8" in availableList:
			turn = "8"
		
		elif three == "o" and five == "o" and "7" in availableList:
			turn = "7"
		
		elif three == "o" and six == "o" and "9" in availableList:
			turn = "9"
		
		elif two == "o" and three == "o" and "1" in availableList:
			turn = "1"
		
		elif four == "o" and seven == "o" and "1" in availableList:
			turn = "1" 
		
		elif five == "o" and nine == "o" and "1" in availableList:
			turn = "1"
		
		elif five == "o" and six == "o" and "4" in availableList: 
			turn = "4"
		
		elif eight == "o" and nine == "o" and "7" in availableList:
			turn = "7"
		
		elif five == "o" and eight == "o" and "2" in availableList:
			turn = "2"
		
		elif five == "o" and seven == "o" and "3" in availableList: 
			turn = "3"
		
		elif six == "o" and nine == "o" and "3" in availableList:
			turn = "3"
		
		elif one == "o" and three == "o" and "2" in availableList:
			turn = "2"
		
		elif one == "o" and seven == "o" and "4" in availableList: 
			turn = "4"
		
		elif one == "o" and nine == "o" and "5" in availableList:
			turn = "5"
		
		elif four == "o" and six == "o" and "5" in availableList: 
			turn = "5"
		
		elif seven == "o" and nine == "o" and "8" in availableList:
			turn = "8"
		
		elif two == "o" and eight == "o" and "5" in availableList:
			turn = "5"
		
		elif three == "o" and seven == "o" and "5" in availableList:
			turn = "5" 
		
		elif three == "o" and nine == "o" and "6" in availableList:
			turn = "6"
		
		elif one == "x" and two == "x" and "3" in availableList:
			turn = "3"
		
		elif one == "x" and four == "x" and "7" in availableList: 
			turn = "7"
		
		elif one == "x" and five == "x" and "9" in availableList:
			turn = "9"
		
		elif four == "x" and five == "x" and "6" in availableList:
			turn = "6" 
		
		elif seven == "x" and eight == "x" and "9" in availableList:
			turn = "9"
		
		elif two == "x" and five == "x" and "8" in availableList:
			turn = "8"
		
		elif three == "x" and five == "x" and "7" in availableList:
			turn = "7"
		
		elif three == "x" and six == "x" and "9" in availableList:
			turn = "9"
		
		elif two == "x" and three == "x" and "1" in availableList:
			turn = "1"
		
		elif four == "x" and seven == "x" and "1" in availableList:
			turn = "1" 
		
		elif five == "x" and nine == "x" and "1" in availableList:
			turn = "1"
		
		elif five == "x" and six == "x" and "4" in availableList: 
			turn = "4"
		
		elif eight == "x" and nine == "x" and "7" in availableList:
			turn = "7"
		
		elif five == "x" and eight == "x" and "2" in availableList:
			turn = "2"
		
		elif five == "x" and seven == "x" and "3" in availableList: 
			turn = "3"
		
		elif six == "x" and nine == "x" and "3" in availableList:
			turn = "3"
		
		elif one == "x" and three == "x" and "2" in availableList:
			turn = "2"
		
		elif one == "x" and seven == "x" and "4" in availableList: 
			turn = "4"
		
		elif one == "x" and nine == "x" and "5" in availableList:
			turn = "5"
		
		elif four == "x" and six == "x" and "5" in availableList: 
			turn = "5"
		
		elif seven == "x" and nine == "x" and "8" in availableList:
			turn = "8"
		
		elif two == "x" and eight == "x" and "5" in availableList:
			turn = "5"
		
		elif three == "x" and seven == "x" and "5" in availableList:
			turn = "5" 
		
		elif three == "x" and nine == "x" and "6" in availableList:
			turn = "6"
		
		else:
			turn = secrets.choice(availableList)
		
		if turn == "1":
			one = "o"
			
		elif turn == "2":
			two = "o"
			
		elif turn == "3":
			three = "o"
			
		elif turn == "4":
			four = "o"
		
		elif turn == "5":
			five = "o"
		
		elif turn == "6":
			six = "o"
		
		elif turn == "7":
			seven = "o"
			
		elif turn == "8":
			eight = "o"
			
		elif turn == "9":
			nine = "o"
	
		player = 1
		count += 1

		availableList.remove(turn)

print(one, "|", two, "|", three, "\n" + four, "|", five, "|", six, "\n" + seven, "|", eight, "|", nine, "\n",)

if winner == "1" or winner == "2":
	
	if opponent == 1:
		print("Player", winner, "wins!")
	
	elif opponent == 2:
		if winner == "1":
			print("You win!")
		
		elif winner == "2":
			print("Computer wins!")

if winner == "0":
	print("Tie!")

input("Enter to quit.")
?