-
Notifications
You must be signed in to change notification settings - Fork 48
/
tic_tac_toe_vs_MAC.py
102 lines (78 loc) · 1.79 KB
/
tic_tac_toe_vs_MAC.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
'''
It is a tic tac toe game.
It is a player vs computer game.
Here in this game , YOU (player) should do the first move .
And your symbol is "X". whereas computer'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.
'''
import random
board = [
['0','1','2'],
['3','4','5'],
['6','7','8']
]
def print_board():
for i in board:
print(i)
def p1():
n = int(input(namex))
row = n//3
column = n%3
board[row][column]="X"
print_board()
print()
global arr
arr.remove(n)
def mac():
global arr
n = random.choice(arr)
print("Mac's move is"," ",n)
row = n//3
column = n%3
board[row][column]="O"
print_board()
print()
arr.remove(n)
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])
arr = [0,1,2,3,4,5,6,7,8]
count=0
name = input("Your sweet name please ")
print()
namex = name+"'s move. "
print_board()
print()
for i in range(5):
p1()
count+=1
a = end()
if a=="X":
print("Game Ended")
print(name,"won")
break
if count==9:
print("Game Drawn")
break
mac()
count+=1
a = end()
if a=="O":
print("Game Ended")
print("Mac won")
break