-
Notifications
You must be signed in to change notification settings - Fork 0
/
TTT.py
208 lines (199 loc) · 5.94 KB
/
TTT.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
while True:
#moves = input("Enter cells: ").upper()
#if len(moves) != 9:
# continue
#if not all(x in ['X', 'O', '_'] for x in moves):
# continue
board = [[' ' for row in range(3)] for col in range(3)]
break
print(f"""
---------
| {board[0][0]} {board[0][1]} {board[0][2]} |
| {board[1][0]} {board[1][1]} {board[1][2]} |
| {board[2][0]} {board[2][1]} {board[2][2]} |
---------
""")
game_state = None
x_wins = False
o_wins = False
player = 'X'
#print(game_state)
while game_state not in ('O wins', 'X wins', 'Draw'):
try:
coords = tuple([int(x) for x in input("Enter the coordinates: ").split()])
except ValueError:
print("You should enter numbers!")
continue
if coords[0] not in (1, 2, 3) or coords[1] not in (1, 2, 3):
print("Coordinates should be from 1 to 3!")
continue
if coords == (1, 1):
if board[0][0] == ' ':
if player != 'O':
board[0][0] = 'X'
player = 'O'
else:
board[0][0] = 'O'
player = 'X'
else:
print("This cell is occupied! Choose another one!")
continue
elif coords == (1, 2):
if board[0][1] == ' ':
if player != 'O':
board[0][1] = 'X'
player = 'O'
else:
board[0][1] = 'O'
player = 'X'
else:
print("This cell is occupied! Choose another one!")
continue
elif coords == (1, 3):
if board[0][2] == ' ':
if player != 'O':
board[0][2] = 'X'
player = 'O'
else:
board[0][2] = 'O'
player = 'X'
else:
print("This cell is occupied! Choose another one!")
continue
elif coords == (2, 1):
if board[1][0] == ' ':
if player != 'O':
board[1][0] = 'X'
player = 'O'
else:
board[1][0] = 'O'
player = 'X'
else:
print("This cell is occupied! Choose another one!")
continue
elif coords == (2, 2):
if board[1][1] == ' ':
if player != 'O':
board[1][1] = 'X'
player = 'O'
else:
board[1][1] = 'O'
player = 'X'
else:
print("This cell is occupied! Choose another one!")
continue
elif coords == (2, 3):
if board[1][2] == ' ':
if player != 'O':
board[1][2] = 'X'
player = 'O'
else:
board[1][2] = 'O'
player = 'X'
else:
print("This cell is occupied! Choose another one!")
continue
elif coords == (3, 1):
if board[2][0] == ' ':
if player != 'O':
board[2][0] = 'X'
player = 'O'
else:
board[2][0] = 'O'
player = 'X'
else:
print("This cell is occupied! Choose another one!")
continue
elif coords == (3, 2):
if board[2][1] == ' ':
if player != 'O':
board[2][1] = 'X'
player = 'O'
else:
board[2][1] = 'O'
player = 'X'
else:
print("This cell is occupied! Choose another one!")
continue
elif coords == (3, 3):
if board[2][2] == ' ':
if player != 'O':
board[2][2] = 'X'
player = 'O'
else:
board[2][2] = 'O'
player = 'X'
else:
print("This cell is occupied! Choose another one!")
continue
print(f"""
---------
| {board[0][0]} {board[0][1]} {board[0][2]} |
| {board[1][0]} {board[1][1]} {board[1][2]} |
| {board[2][0]} {board[2][1]} {board[2][2]} |
---------
""")
if abs(board.count('X') - board.count('O')) >= 2:
game_state = 'Impossible'
if board[0][:3] == ['X', 'X', 'X']:
game_state = 'X wins'
x_wins = True
if board[0][:3] == ['O', 'O', 'O']:
game_state = 'O wins'
o_wins = True
if board[1][:3] == ['X', 'X', 'X']:
game_state = 'X wins'
x_wins = True
if board[1][:3] == ['O', 'O', 'O']:
game_state = 'O wins'
o_wins = True
if board[2][:3] == ['X', 'X', 'X']:
game_state = 'X wins'
x_wins = True
if board[2][:3] == ['O', 'O', 'O']:
game_state = 'O wins'
o_wins = True
if [board[0][0], board[1][1], board[2][2]] == ['X', 'X', 'X']:
game_state = 'X wins'
x_wins = True
if [board[0][0], board[1][1], board[2][2]] == ['O', 'O', 'O']:
game_state = 'O wins'
o_wins = True
if board[:3][0] == ['X', 'X', 'X']:
game_state = 'X wins'
x_wins = True
if board[:3][0] == ['O', 'O', 'O']:
game_state = 'O wins'
o_wins = True
if board[:3][1] == ['X', 'X', 'X']:
game_state = 'X wins'
x_wins = True
if board[:3][1] == ['O', 'O', 'O']:
game_state = 'O wins'
o_wins = True
if board[:3][2] == ['X', 'X', 'X']:
game_state = 'X wins'
x_wins = True
if board[:3][2] == ['O', 'O', 'O']:
game_state = 'O wins'
o_wins = True
if [board[0][2], board[1][1], board[2][0]] == ['X', 'X', 'X']:
game_state = 'X wins'
x_wins = True
if [board[0][2], board[1][1], board[2][0]] == ['O', 'O', 'O']:
game_state = 'O wins'
o_wins = True
if x_wins and o_wins:
game_state = 'Impossible'
if game_state in ('X wins', 'O wins'):
print(game_state)
break
if any(x == ' ' for y in board for x in y):
game_state = 'Game not finished'
else:
game_state = 'Draw'
print(game_state)
break
if game_state in ('X wins', 'O wins'):
print(game_state)
break