-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtic_tac_toe.py
More file actions
124 lines (116 loc) · 2.69 KB
/
tic_tac_toe.py
File metadata and controls
124 lines (116 loc) · 2.69 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
mat= [['1','|','2' ,'|','3' ],
['_',' ','_',' ','_'],
['4','|','5','|','6'],
['_',' ','_',' ','_'],
['7','|','8','|','9'],]
def printMatrix(m):
for i in range(5):
print(mat[i][0], mat[i][1], mat[i][2], mat[i][3], mat[i][4])
def changeMatrix(m):
g= input("Are you X or O?")
position= int(input("Select the number you want to play (1 to 9): "))
if position == 1:
m[0][0]= g
elif position == 2:
m[0][2]= g
elif position == 3:
m[0][4]= g
elif position == 4:
m[2][0]= g
elif position == 5:
m[2][2]= g
elif position == 6:
m[2][4]= g
elif position == 7:
m[4][0]= g
elif position == 8:
m[4][2]= g
else:
m[4][4]= g
def endGame(m):
if m[0][0] == 'o' and m[0][2] == 'o' and m[0][4] == 'o':
global end
end= False
print("Player O wins!")
return end
elif m[2][0] == 'o' and m[2][2] == 'o' and m[2][4] == 'o':
global end
end= False
print("Player O wins!")
return end
elif m[4][0] == 'o' and m[4][2] == 'o' and m[4][4] == 'o':
global end
end= False
print("Player O wins!")
return end
elif m[0][0] == 'o' and m[2][0] == 'o' and m[4][0] == 'o':
global end
end= False
print("Player O wins!")
return end
elif m[0][2] == 'o' and m[2][2] == 'o' and m[4][2] == 'o':
global end
end= False
print("Player O wins!")
return end
elif m[0][4] == 'o' and m[2][4] == 'o' and m[4][4] == 'o':
global end
end= False
print("Player O wins!")
return end
elif m[0][0] == 'o' and m[2][2] == 'o' and m[4][4] == 'o':
global end
end= False
print("Player O wins!")
return end
elif m[4][0] == 'o' and m[2][2] == 'o' and m[0][4] == 'o':
global end
end= False
print("Player O wins!")
return end
elif m[0][0] == 'x' and m[0][2] == 'x' and m[0][4] == 'x':
global end
end= False
print("Player X wins!")
return end
elif m[2][0] == 'x' and m[2][2] == 'x' and m[2][4] == 'x':
global end
end= False
print("Player X wins!")
return end
elif m[4][0] == 'x' and m[4][2] == 'x' and m[4][4] == 'x':
global end
end= False
print("Player X wins!")
return end
elif m[0][0] == 'x' and m[2][0] == 'x' and m[4][0] == 'x':
global end
end= False
print("Player X wins!")
return end
elif m[0][2] == 'x' and m[2][2] == 'x' and m[4][2] == 'x':
global end
end= False
print("Player X wins!")
return end
elif m[0][4] == 'x' and m[2][4] == 'x' and m[4][4] == 'x':
global end
end= False
print("Player X wins!")
return end
elif m[0][0] == 'x' and m[2][2] == 'x' and m[4][4] == 'x':
global end
end= False
print("Player X wins!")
return end
elif m[4][0] == 'x' and m[2][2] == 'x' and m[0][4] == 'x':
global end
end= False
print("Player X wins!")
return end
end= True
while end:
printMatrix(mat)
changeMatrix(mat)
endGame(mat)
printMatrix(mat)