-
Notifications
You must be signed in to change notification settings - Fork 48
/
tic_tac_toe.py
115 lines (73 loc) · 1.7 KB
/
tic_tac_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
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
'''Player:1 Is "X"
Player:2 Is "O"'''
=======
Player1's symbol is "X"
Player2's symbol is "O"
The player's should give a input which is any one number which is available in the grid .
so that at the position at which that number exists will be updated with the player's symbol.
>
board = [
['0','1','2'],
['3','4','5'],
['6','7','8']
]
def print_board():
for i in board:
print(i)
def p1():
n = int(input("Jeevan's_move. "))
row = n//3
n = int(input("Player1's_move. "))
row = n//3
column = n%3
board[row][column]="X"
print_board()
def p2():
n = int(input("Thrishul's_move. "))
n = int(input("Player2's_move. "))
row = n//3
column = n%3
board[row][column]="O"
print_board()
def end():
if board[0][0]==board[1][0]==board[2][0]:
return(board[0][0])
elif board[0][1]==board[1][1]==board[2][1]:
return(board[0][1])
elif board[0][2]==board[1][2]==board[2][2]:
return(board[0][2])
elif board[0][0]==board[0][1]==board[0][2]:
return(board[0][0])
elif board[1][0]==board[1][1]==board[1][2]:
return(board[1][1])
elif board[2][0]==board[2][1]==board[2][2]:
return(board[2][0])
elif board[0][0]==board[1][1]==board[2][2]:
return(board[0][0])
elif board[0][2]==board[1][1]==board[2][0]:
return(board[0][2])
else:
return("draw")
print_board()
count=0
for i in range(5):
p1()
count+=1
a = end()
if a=="X":
print("Game Ended")
print("Jeevan won")
print("Player1 won")
break
if count==9:
print("draw")
break
p2()
count+=1
a = end()
if a=="O":
print("Game Ended")
print("Thrishul won")
break
print("Player2 won")
break