-
Notifications
You must be signed in to change notification settings - Fork 48
/
Tic-Tac-Toe_game
69 lines (54 loc) · 1.47 KB
/
Tic-Tac-Toe_game
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
Tic_Tac__Toe_game
The program is written in python of where two players can play in pyhton.
board=[["0","1","2"],
["3","4","5"],
["6","7","8"]]
def print_board():
for row in board:
print(row)
def check_game(mark):
#first let's check row manually
for i in range(3):
if board[i][0]==mark and board[i][1]==mark and board[i][2]==mark:
return True
#Then colunms manully
for i in range(3):
if board[0][i]==mark and board[1][i]==mark and board[2][i]==mark:
return True
#first diagonal manually
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
#main game funtion
def play_tic_tac_toe():
current_player="1"
mark="X"
moves=0
while(moves<9):
#print("player",current_player,"moves.")
print("player"+current_player+"moves.")
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 occupied!!,Try another one")
continue
if result:
print("player"+current_player+"has won the game")
break
if moves==9:
print("It's a Draw")
if current_player=="1":
current_player="2"
mark="O"
else:
current_player="1"
marl="X"
print_board()
play_tic_tac_toe()