forked from sahil078/Hacktoberfest2024_jh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tic_tac-toe.py
85 lines (61 loc) · 1.28 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
'''Player:1 Is "X"
Player:2 Is "O"'''
board = [
['0','1','2'],
['3','4','5'],
['6','7','8']
]
def print_board():
for i in board:
print(i)
def p1():
n = int(input("Player 1_move. "))
row = n//3
column = n%3
board[row][column]="X"
print_board()
def p2():
n = int(input("Player 2_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("Player 1")
break
if count==9:
print("draw")
break
p2()
count+=1
a = end()
if a=="O":
print("Game Ended")
print("Player 2")
break