-
Notifications
You must be signed in to change notification settings - Fork 1
/
forinarow.py
295 lines (242 loc) · 12.2 KB
/
forinarow.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
import math
import random
import sys
import numpy as np
import pygame
import music
pygame.init()
BLUE = pygame.Color('blue')
BLACK = pygame.Color('black')
RED = pygame.Color('red')
YELLOW = pygame.Color('yellow')
PLAYER = 0
AI = 1
EMPTY = 0
PLAYER_PIECE = 1
AI_PIECE = 2
WINDOW_LENGTH = 4
SQUARESIZE = 100
RADIUS = int(SQUARESIZE / 2 - 5)
class FourInARow:
def __init__(self, row_count, col_count):
self.row_count = row_count
self.col_count = col_count
self.width = col_count * SQUARESIZE
self.height = (row_count + 1) * SQUARESIZE
self.size = (self.width, self.height)
self.screen = pygame.display.set_mode(self.size)
self.board = self.create_board()
self.draw_board(self.board)
pygame.display.set_caption("FourInARow")
programicon = pygame.image.load('icons/4inarow.png')
pygame.display.set_icon(programicon)
def create_board(self): # создаем доску
board = np.zeros((self.row_count, self.col_count))
return board
@staticmethod
def drop_piece(board, row, col, piece): # делаем ход
board[row][col] = piece
def is_valid_location(self, board, col): # проверяем, можно ли пойти по этой координате(есть ли шарик вверху)
return board[self.row_count - 1][col] == 0
def get_next_open_row(self, board, col): # ищем номер ряда, в который упадет шарик
for r in range(self.row_count):
if board[r][col] == 0:
return r
def winning_move(self, board, piece): # проверка победы
for c in range(self.col_count - 3):
for r in range(self.row_count):
if board[r][c] == piece and board[r][c + 1] == piece and board[r][c + 2] == piece and \
board[r][c + 3] == piece:
return True
for c in range(self.col_count):
for r in range(self.row_count - 3):
if board[r][c] == piece and board[r + 1][c] == piece and board[r + 2][c] == piece and \
board[r + 3][c] == piece:
return True
for c in range(self.col_count - 3):
for r in range(self.row_count - 3):
if board[r][c] == piece and board[r + 1][c + 1] == piece and board[r + 2][c + 2] == piece and \
board[r + 3][c + 3] == piece:
return True
for c in range(self.col_count - 3):
for r in range(3, self.row_count):
if board[r][c] == piece and board[r - 1][c + 1] == piece and board[r - 2][c + 2] == piece and \
board[r - 3][c + 3] == piece:
return True
@staticmethod
def evaluate_window(window, piece): # оцениваем по ряду
score = 0
opp_piece = PLAYER_PIECE
if piece == PLAYER_PIECE:
opp_piece = AI_PIECE
if window.count(piece) == 4:
score += 100
elif window.count(piece) == 3 and window.count(EMPTY) == 1:
score += 5
elif window.count(piece) == 2 and window.count(EMPTY) == 2:
score += 2
if window.count(opp_piece) == 3 and window.count(EMPTY) == 1:
score -= 4
return score
def score_position(self, board, piece): # оцениваем ход
score = 0
center_array = [int(i) for i in list(board[:, self.col_count // 2])]
center_count = center_array.count(piece)
score += center_count * 3
for r in range(self.row_count):
row_array = [int(i) for i in list(board[r, :])]
for c in range(self.col_count - 3):
window = row_array[c:c + WINDOW_LENGTH]
score += self.evaluate_window(window, piece)
for c in range(self.col_count):
col_array = [int(i) for i in list(board[:, c])]
for r in range(self.row_count - 3):
window = col_array[r:r + WINDOW_LENGTH]
score += self.evaluate_window(window, piece)
for r in range(self.row_count - 3):
for c in range(self.col_count - 3):
window = [board[r + i][c + i] for i in range(WINDOW_LENGTH)]
score += self.evaluate_window(window, piece)
for r in range(self.row_count - 3):
for c in range(self.col_count - 3):
window = [board[r + 3 - i][c + i] for i in range(WINDOW_LENGTH)]
score += self.evaluate_window(window, piece)
return score
def is_terminal_node(self, board): # проверяем на возможность ходить
return self.winning_move(board, PLAYER_PIECE) or \
self.winning_move(board, AI_PIECE) or len(self.get_valid_locations(board)) == 0
def minimax(self, board, depth, alpha, beta, maximizing_player): # рекурсивный алгоритм для поиска лучшего хода
valid_locations = self.get_valid_locations(board)
is_terminal = self.is_terminal_node(board)
if depth == 0 or is_terminal:
if is_terminal:
if self.winning_move(board, AI_PIECE):
return None, 100000000000000
elif self.winning_move(board, PLAYER_PIECE):
return None, -10000000000000
else:
return None, 0
else:
return None, self.score_position(board, AI_PIECE)
if maximizing_player:
value = -math.inf
column = random.choice(valid_locations)
for col in valid_locations:
row = self.get_next_open_row(board, col)
b_copy = board.copy()
self.drop_piece(b_copy, row, col, AI_PIECE)
new_score = self.minimax(b_copy, depth - 1, alpha, beta, False)[1]
if new_score > value:
value = new_score
column = col
alpha = max(alpha, value)
if alpha >= beta:
break
return column, value
else:
value = math.inf
column = random.choice(valid_locations)
for col in valid_locations:
row = self.get_next_open_row(board, col)
b_copy = board.copy()
self.drop_piece(b_copy, row, col, PLAYER_PIECE)
new_score = self.minimax(b_copy, depth - 1, alpha, beta, True)[1]
if new_score < value:
value = new_score
column = col
beta = min(beta, value)
if alpha >= beta:
break
return column, value
def get_valid_locations(self, board): # возвращаем список колонок, в которые мы можем бросить шарик
valid_locations = []
for col in range(self.col_count):
if self.is_valid_location(board, col):
valid_locations.append(col)
return valid_locations
def pick_best_move(self, board, piece): # рассматриваем все ходы и выбираем лучший
valid_locations = self.get_valid_locations(board)
best_score = -10000
best_col = random.choice(valid_locations)
for col in valid_locations:
row = self.get_next_open_row(board, col)
temp_board = board.copy()
self.drop_piece(temp_board, row, col, piece)
score = self.score_position(temp_board, piece)
if score > best_score:
best_score = score
best_col = col
return best_col
def draw_board(self, board): # отрисовываем доску
for c in range(self.col_count):
for r in range(self.row_count):
pygame.draw.rect(self.screen, BLUE,
(c * SQUARESIZE, r * SQUARESIZE + SQUARESIZE, SQUARESIZE, SQUARESIZE))
pygame.draw.circle(self.screen, BLACK, (int(c * SQUARESIZE + SQUARESIZE / 2),
int(r * SQUARESIZE + SQUARESIZE + SQUARESIZE / 2)),
RADIUS)
for c in range(self.col_count):
for r in range(self.row_count):
if board[r][c] == PLAYER_PIECE:
pygame.draw.circle(self.screen, RED, (int(c * SQUARESIZE + SQUARESIZE / 2),
self.height - int(r * SQUARESIZE + SQUARESIZE / 2)),
RADIUS)
elif board[r][c] == AI_PIECE:
pygame.draw.circle(self.screen, YELLOW, (int(c * SQUARESIZE + SQUARESIZE / 2),
self.height - int(r * SQUARESIZE + SQUARESIZE / 2)),
RADIUS)
pygame.display.update()
def start(self): # основной цикл
pygame.display.update()
game_over = False
running = True
myfont = pygame.font.SysFont("monospace", 75)
turn = random.randint(PLAYER, AI)
while not game_over and running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.MOUSEMOTION: # отрисовываем шарик, который собираемся бросать
pygame.draw.rect(self.screen, BLACK, (0, 0, self.width, SQUARESIZE))
posx = event.pos[0]
if turn == PLAYER:
pygame.draw.circle(self.screen, RED, (posx, int(SQUARESIZE / 2)), RADIUS)
pygame.display.update()
if event.type == pygame.MOUSEBUTTONDOWN: # игрок делает ход
pygame.draw.rect(self.screen, BLACK, (0, 0, self.width, SQUARESIZE))
if turn == PLAYER:
posx = event.pos[0]
col = int(math.floor(posx / SQUARESIZE))
if self.is_valid_location(self.board, col):
row = self.get_next_open_row(self.board, col)
self.drop_piece(self.board, row, col, PLAYER_PIECE)
if self.winning_move(self.board, PLAYER_PIECE):
label = myfont.render("Красный победил!", True, RED)
self.screen.blit(label, (40, 10))
game_over = True
turn += 1
turn = turn % 2
self.draw_board(self.board)
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
running = False
with open("game_end.txt", "w+") as fil:
fil.write("1")
break
if event.type == music.STOPPED_PLAYING:
music.play_music()
if running:
if turn == AI and not game_over: # компьютер делает ход
col, minimax_score = self.minimax(self.board, 5, -math.inf, math.inf, True)
if self.is_valid_location(self.board, col):
row = self.get_next_open_row(self.board, col)
self.drop_piece(self.board, row, col, AI_PIECE)
if self.winning_move(self.board, AI_PIECE):
label = myfont.render("Желтый победил!", True, YELLOW)
self.screen.blit(label, (40, 10))
game_over = True
self.draw_board(self.board)
turn += 1
turn = turn % 2
if game_over:
pygame.time.wait(3000)