-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathskeleton.py
331 lines (255 loc) · 9.19 KB
/
skeleton.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
from pandas import DataFrame
from random import choice
def new_board():
'''Creates 3x3 board (so-called board) to play with.'''
board = [['-' for row in range(3)] for rows in range(3)]
return board
def render(board):
'''Prints Data Frame of current status of board.'''
print(DataFrame(board))
def is_invalid_move(board, coordinates):
'''Checks if the square isn't already taken.'''
x = coordinates[0]
y = coordinates[1]
#print((x, y))
if board[y][x] != '-':
return True
else:
return False
def empty_squares(board):
'''Finds every empty square in the given board.'''
empty_squares = []
for ri, row in enumerate(board):
for ei, elem in enumerate(row):
if board[ri][ei] == '-':
empty_squares.append((ei, ri))
else:
pass
if len(empty_squares) != 0:
return empty_squares
else:
return None
def rotate90Clockwise(board):
'''Rotates board by 90 degrees clockwise.'''
board = board
row = len(board[0])
for i in range(row // 2):
for j in range(i, row - i - 1):
temp = board[i][j]
board[i][j] = board[row - 1 - j][i]
board[row - 1 - j][i] = board[row - 1 - i][row - 1 - j]
board[row - 1 - i][row - 1 - j] = board[j][row - 1 - i]
board[j][row - 1 - i] = temp
return board
def rotate90AntiClockwise(board):
'''Rotates matrix by 90 degrees anti clockwise.'''
N = len(board[0])
# Consider all squares one by one
for x in range(0, int(N/2)):
# Consider elements in group
# of 4 in current square
for y in range(x, N-x-1):
# store current cell in temp variable
temp = board[x][y]
# move values from right to top
board[x][y] = board[y][N-1-x]
# move values from bottom to right
board[y][N-1-x] = board[N-1-x][N-1-y]
# move values from left to bottom
board[N-1-x][N-1-y] = board[N-1-y][x]
# assign temp to left
board[N-1-y][x] = temp
return board
def random_ai(board):
'''Randomly chooses not owned square on board.'''
choosed_square = choice(empty_squares(board))
return choosed_square
def find_opp_win(board, opponent):
'''Rudimentary AI choosing last settling move, if such moves aren't possible, chooses randomly.'''
player = None
if opponent == 'X':
player = 'O'
else:
player = 'X'
# checks if settling move haven't been done in horizontal rows
for ri, row in enumerate(board):
if row.count(opponent) == 2 and row.count(player) == 0:
for ei, elem in enumerate(row):
if board[ri][ei] == '-':
return (ei, ri)
board_length = len(board[0])
# checks if settling move haven't been done in vertical rows
for ri, row in enumerate(board):
if row.count(opponent) == 2 and row.count(player) == 0:
for ei, elem in enumerate(row):
if board[ri][ei] == '-':
rotate90AntiClockwise(board)
if ei == 0 and ri == 0:
return (2, 0)
else:
return (ri, board_length -1 - ei)
# diagonal from top left to bottom right
diagonal1 = [board[0][0], board[1][1], board[2][2]]
if diagonal1.count(opponent) == 2 and diagonal1.count(player) == 0:
for ei, elem in enumerate(diagonal1):
if ei == 0 and elem == '-':
return (0, 0)
elif ei == 1 and elem == '-':
return (1, 1)
elif ei == 2 and elem == '-':
return (2, 2)
# diagonal from top right to bottom left
diagonal2 = [board[0][2], board[1][1], board[2][0]]
if diagonal2.count(opponent) == 2 and diagonal2.count(player) == 0:
for ei, elem in enumerate(diagonal2):
if ei == 0 and elem == '-':
return (2, 0)
elif ei == 1 and elem == '-':
return (1, 1)
elif ei == 2 and elem == '-':
return (0, 2)
return None
def finds_winning_move_and_blocks_win_ai(board, player):
'''Rudimentary AI choosing last settling move, if such moves aren't possible, chooses randomly.'''
opponent = None
if player == 'X':
opponent = 'O'
else:
opponent = 'X'
# checks if settling move haven't been done in horizontal rows
for ri, row in enumerate(board):
if row.count(player) == 2 and row.count(opponent) == 0:
for ei, elem in enumerate(row):
if board[ri][ei] == '-':
return (ei, ri)
board_length = len(board[0])
# checks if settling move haven't been done in vertical rows
for ri, row in enumerate(board):
if row.count(player) == 2 and row.count(opponent) == 0:
for ei, elem in enumerate(row):
if board[ri][ei] == '-':
rotate90AntiClockwise(board)
if ei == 0 and ri == 0:
return (2, 0)
else:
return (ri, board_length -1 - ei)
# diagonal from top left to bottom right
diagonal1 = [board[0][0], board[1][1], board[2][2]]
if diagonal1.count(player) == 2 and diagonal1.count(opponent) == 0:
for ei, elem in enumerate(diagonal1):
if ei == 0 and elem == '-':
return (0, 0)
elif ei == 1 and elem == '-':
return (1, 1)
elif ei == 2 and elem == '-':
return (2, 2)
# diagonal from top right to bottom left
diagonal2 = [board[0][2], board[1][1], board[2][0]]
if diagonal2.count(player) == 2 and diagonal2.count(opponent) == 0:
for ei, elem in enumerate(diagonal2):
if ei == 0 and elem == '-':
return (2, 0)
elif ei == 1 and elem == '-':
return (1, 1)
elif ei == 2 and elem == '-':
return (0, 2)
# if AI didn't notice any settling move, chooses random move
if find_opp_win(board, opponent) != None:
return find_opp_win(board, opponent)
else:
return random_ai(board)
def board_is_full(board):
'''Checks if the board is filled. Returns True or False.'''
counter = 0
# count filled rows
for i in board:
if i.count('-') == 0:
counter += 1
if counter == 3:
return True
else:
return False
def check_win(board):
'''Checks if recent move isn't a winning move. If it was returns player. If board is full, returns 'DRAW'. Eventually None.'''
X = 'X'
O = 'O'
count = 0
# automatically rotates a board to check if settling move wasn't done somewhere in the rows
while count != 2:
r_board = rotate90Clockwise(board)
for i in r_board:
if i.count(O) == 3:
return O
elif i.count(X) == 3:
return X
else:
pass
count += 1
# diagonal from top left to bottom right
diagonal1 = [r_board[0][0], r_board[1][1], r_board[2][2]]
# diagonal from top right to bottom left
diagonal2 = [r_board[0][2], r_board[1][1], r_board[2][0]]
# checks if settling move wasn't done in diagonals
if diagonal1.count(O) == 3 or diagonal2.count(O) == 3:
return O
elif diagonal1.count(X) == 3 or diagonal2.count(X) == 3:
return X
elif board_is_full(board):
return 'DRAW'
else:
return None
def make_move(board, player):
'''Puts a pawn on a specific square specified by coordinates.'''
if player == 'X':
# HERE YOU CAN CHANGE TYPE OF PLAYER
coordinates = finds_winning_move_and_blocks_win_ai(board, 'X')
else:
# HERE AN OPPONENT
coordinates = input('x y: ').split(' ')
x = int(coordinates[0])
y = int(coordinates[1])
if is_invalid_move(board, (x, y)):
print(f"Can't make move {(x, y)} square is already taken!")
make_move(board, player)
else:
board[y][x] = player
return board
def game(board, player):
'''The core of the game.'''
board = make_move(board, player)
render(board)
print('------------')
if player == 'X':
player = 'O'
else:
player = 'X'
winner = check_win(board)
if winner == 'DRAW':
return 'DRAW'
elif winner != None:
print(winner)
return winner
board = rotate90Clockwise(rotate90Clockwise(board))
return game(board, player)
def play_lab(n):
'''Plays n games and returns statistics about number of wins and draws.'''
results = []
for _ in range(n):
board = new_board()
#render(board)
result = game(board, 'X')
if result == 'X':
results.append(1)
elif result == 'O':
results.append(2)
else:
results.append(3)
stats = {}
stats['X'] = results.count(1)
stats['O'] = results.count(2)
stats['DRAW'] = results.count(3)
print(f'THE FINAL STATS ARE: {stats}')
return stats
board = new_board()
render(board)
game(board, 'X')