-
Notifications
You must be signed in to change notification settings - Fork 21
/
play_tic_tac_toe
67 lines (49 loc) · 1.5 KB
/
play_tic_tac_toe
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
board=[['0','1','2'],
['3','4','5'],
['6','7','8']]
def print_board(board):
for row in board:
print(row)
def check_game(mark):
#first let's check rows
for i in range(3):
if board[i][0]==mark and board[i][1]==mark and board[i][2]==mark:
return True
#then columns
for i in range(3):
if board[0][i]==mark and board[1][i]==mark and board[2][i]==mark:
return True
if board[0][0]==mark and board[1][1]==mark and board[2][2]==mark:
return True
if board[0][2]==mark and board[1][1]==mark and board[0][2]==mark:
return True
def play_tic_tac_toe():
current_player="1"
mark="X"
moves=0
while (moves<9):
print_board(board)
print("player"+current_player+"move.")
print("please enter the cell no:")
cell=int(input())
row=cell//3
col=cell%3
if board[row][col]!="X" and board[row][col]!="O":
board[row][col]= mark
moves+=1
result=check_game(mark)
else:
print("this cell is already occupied!!, try ANOTHER ONE.")
continue
if result:
print("Player" +current_player+"has won the game")
break
if moves==9:
print("!!!!Draw!!!!")
if current_player=="1":
current_player="2"
mark="O"
else:
current_player="1"
mark="X"
play_tic_tac_toe()