-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtictactoe.py
154 lines (135 loc) · 4.11 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
# display grid function
def display(grid):
for i in range(0, len(grid), 3):
print(' | '.join(grid[i:i + 3]))
if i < 6:
print('---------')
score = {"player1": 0, "player2": 0}
def scorecard(score, p1, p2):
if p1 == 1:
score["player1"] += 1
elif p2 == 1:
score["player2"] += 1
return score
# main function
def tictactoe():
grid = ["1", "2", "3", "4", "5", "6", "7", "8", "9"]
numch = {1, 2, 3, 4, 5, 6, 7, 8, 9}
display(grid)
p1 = 0
p2 = 0
while len(numch) != 0:
check = len(numch)
if check != 0:
# player1 turn
while True:
user1 = int(input("PLAYER1 TURN , enter number b/w 1-9: "))
if user1 in numch:
grid[user1 - 1] = "o"
numch.discard(user1)
break
else:
# display the move
display(grid)
print("invalid input,enter again")
# display the move
display(grid)
# check if player1 wins
if (
grid[0] == "o"
and grid[1] == "o"
and grid[2] == "o"
or grid[3] == "o"
and grid[4] == "o"
and grid[5] == "o"
or grid[6] == "o"
and grid[7] == "o"
and grid[8] == "o"
or grid[0] == "o"
and grid[3] == "o"
and grid[6] == "o"
or grid[1] == "o"
and grid[4] == "o"
and grid[7] == "o"
or grid[2] == "o"
and grid[5] == "o"
and grid[8] == "o"
or grid[0] == "o"
and grid[4] == "o"
and grid[8] == "o"
or grid[2] == "o"
and grid[4] == "o"
and grid[6] == "o"
):
print("player1 wins!!")
p1 += 1
scorecard(score, p1, p2)
return
else:
break
check = len(numch)
if check != 0:
# player2 turn
while True:
user2 = int(input("PLAYER2 TURN , enter number: "))
if user2 in numch:
grid[user2 - 1] = "x"
numch.discard(user2)
break
else:
# display the move
display(grid)
print("invalid input,enter again")
# display the move
display(grid)
# check if player2 wins
if (
grid[0] == "x"
and grid[1] == "x"
and grid[2] == "x"
or grid[3] == "x"
and grid[4] == "x"
and grid[5] == "x"
or grid[6] == "x"
and grid[7] == "x"
and grid[8] == "x"
or grid[0] == "x"
and grid[3] == "x"
and grid[6] == "x"
or grid[1] == "x"
and grid[4] == "x"
and grid[7] == "x"
or grid[2] == "x"
and grid[5] == "x"
and grid[8] == "x"
or grid[0] == "x"
and grid[4] == "x"
and grid[8] == "x"
or grid[2] == "x"
and grid[4] == "x"
and grid[6] == "x"
):
print("player2 wins!!")
p2 += 1
scorecard(score, p1, p2)
return
else:
break
# if no one wins
print("its a TIE!!")
tictactoe()
while True:
print(scorecard(score, p1=int, p2=int))
cont = input("NEW GAME??, enter y/n")
if cont.lower() == "y":
tictactoe()
else:
if score["player1"] > score["player2"]:
print("player1 wins")
break
elif score["player1"] < score["player2"]:
print("player2 wins")
break
else:
print("its a tie")
break