-
Notifications
You must be signed in to change notification settings - Fork 22
/
TIC - TAC - TOE
66 lines (52 loc) · 1.37 KB
/
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
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
#check diagonally
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[2][0]==mark:
return True
return False
def play_tic_tac_toe():
current_player = "1"
mark = "x"
moves=0
while(moves<9):
print()
print_board(board)
print("player"+current_player+" move")
print("please enter the cell number :")
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 current_player=="1":
current_player="2"
mark="O"
else:
current_player="1"
mark="x"
if moves==9:
print("!!!Draw!!!")
play_tic_tac_toe()