-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tic-Tic-Toe.py
68 lines (54 loc) · 1.83 KB
/
Tic-Tic-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
choices = []
for x in range (0, 9) :
choices.append(str(x + 1))
playerOneTurn = True
winner = False
def printBoard() :
print( '\n -----')
print( '|' + choices[0] + '|' + choices[1] + '|' + choices[2] + '|')
print( ' -----')
print( '|' + choices[3] + '|' + choices[4] + '|' + choices[5] + '|')
print( ' -----')
print( '|' + choices[6] + '|' + choices[7] + '|' + choices[8] + '|')
print( ' -----\n')
while not winner :
printBoard()
if playerOneTurn :
print( "Player 1:")
else :
print( "Player 2:")
try:
choice = int(input(">> "))
except:
print("Invalid! Enter Again: ")
continue
try:
choices[choice-1]
except IndexError:
print("Invalid! Enter Again:")
continue
if choices[choice - 1] == 'X' or choices [choice-1] == 'O':
print("Not Allowed, try again")
continue
if playerOneTurn :
choices[choice - 1] = 'X'
else :
choices[choice - 1] = 'O'
playerOneTurn = not playerOneTurn
for x in range (0, 3) :
y = x * 3
if (choices[y] == choices[(y + 1)] and choices[y] == choices[(y + 2)]) :
winner = True
printBoard()
if (choices[x] == choices[(x + 3)] and choices[x] == choices[(x + 6)]) :
winner = True
printBoard()
if((choices[0] == choices[4] and choices[0] == choices[8]) or
(choices[2] == choices[4] and choices[4] == choices[6])) :
winner = True
printBoard()
if (winner != True):
if (choices[0] !='1' and choices[1] !='2' and choices[2] !='3' and choices[3] !='4' and choices[4] !='5' and choices[5] !='6' and choices[6] !='7' and choices[7] !='8' and choices[8] !='9'):
print("Draw")
exit()
print ("Player " + str(int(playerOneTurn + 1)) + " wins!\n")