Repository navigation
Expand file tree
/
Copy pathgame.py
More file actions
256 lines (219 loc) · 8.93 KB
/
Copy pathgame.py
File metadata and controls
256 lines (219 loc) · 8.93 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
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
#
import numpy as np
from dictgeneration import gen_dict
NUM_ROW = 6
NUM_COL = 7
EMPTY = "-"
PLAYER_PIECE = "X"
AI_PIECE = "O"
index_dict,pos_dict = gen_dict()
class game:
def __init__(self, calculate_heuristics=True):
self.game_winner = EMPTY # variáveis que controlam o fim do jogo
self.board_is_full = False
if calculate_heuristics: self.segment_heuristics = [0 for _ in range (69)] # Caso bool seja falso, não é necessario inicializar essa variavel
self.board = np.full([NUM_ROW, NUM_COL], EMPTY)
self.last_move = None
self.calculate_heuristics = calculate_heuristics
self.first = PLAYER_PIECE #or AI_PIECE
self.turn = 0
def drawBoard(self):
for i in range(7): print(i, end=" ") #imprime os numeros das colunas
print() # coloca newline
for line in np.flip(self.board, 0):
for piece in line:
print(piece, end=" ")
print()# coloca newline
return
"""Dado um input, verifica a função availableCollumns para saber se é válido.
Se não for pede novamente o input, do contrário usa a função putGamePiece para alterar a board."""
def playOneTurn(self):
available = self.availableCollumns()
try: # error handling
collumn = int(
input("Choose in which collumn do you wanna play or press 9 to quit: ")
)
except:
collumn = -1
while collumn not in available:
if collumn == 9:
quit() # opção para quitar
try: # more error handlings
collumn = int(
input(
"The collumn you selected is either full or invalid, choose another one or press 9 to quit: "
)
)
except:
collumn = -1
self.putGamePiece(collumn, PLAYER_PIECE)
return
"""Verifica qual a próxima row vazia e põe a peça nesta row.
Checa após cada movimento a função check_win_after_move para saber se houve ganhador.
caso calculate_heuristics==True calcula as heuristicas dos segmentos alterados por aquele movimento
"""
def putGamePiece(self, collumn, piece):
available = self.availableCollumns()
if collumn not in available:
print("The last move was invalid, below is the last state of the board and the move made, the game will quit")
self.drawBoard()
print("last move = {collumn}")
quit()
piece_placement = self.nextEmptyRowinCollumn(collumn)
self.board[piece_placement][collumn] = piece
if self.check_win_after_move(piece_placement, collumn, piece):
self.game_winner = piece # checar se houve ganhador
elif not self.availableCollumns():
self.board_is_full = True # se não há colunas vazias, a board está cheia
# not list aparentemente é um dos jeitos mais eficientes de checar se uma lista está vazia, python é estranho - M
if self.calculate_heuristics: self.update_heuristics(piece_placement, collumn) # atualiza as heuristicas se o bool for True
self.last_move = collumn
self.turn += 1
return
"""Checa a última row para saber quais colunas não estão cheias. Retorna a lista de colunas."""
def availableCollumns(self):
available = []
for i, value in enumerate(self.board[NUM_ROW - 1]):
if value == EMPTY:
available.append(i)
return available
"""Dado uma coluna, verifica qual a próxima row vazia. Retorna o número da row."""
def nextEmptyRowinCollumn(self, collumn):
for i, value in enumerate(self.board[:, collumn]):
if value == EMPTY:
return i
return
"""Função que checa se houve vitória após cada movimento.
Verifica somente os arrays que contém a peça em [move_row, move_col]."""
def check_win_after_move(self, move_row, move_col, piece):
win_segment = [piece, piece, piece, piece]
for i in self.segments_that_intersect(move_row, move_col): # verifica se houve vitoria em todos os segmentos que são modificados pelo ultimo movimento
if i == win_segment: return True
return False
# retorna lista com todos os segmentos de 4 em todas as direções
def get_segments(self):
segments = []
# Verifica linhas e colunas
for i in range(NUM_ROW):
line = self.board[i]
for j in range(4):
segments.append(line[j : j + 4])
col = self.board[:, i]
for j in range(3):
segments.append(col[j : j + 4])
# Verifica a ultima coluna
col = self.board[:, NUM_COL - 1]
for j in range(3):
segments.append(col[j : j + 4])
# Verifica as diagonais principais
for i in range(-2, 4):
dia = np.diag(self.board, i)
for j in range(len(dia) - 3):
segments.append(dia[j : j + 4])
# Dá flip no array e verifica as diagonais principais do array flipado
# (equivalentes às diagonais perpendiculares às principais do array original)
state_tr = np.fliplr(self.board)
for i in range(-2, 4):
dia = np.diag(state_tr, i)
for j in range(len(dia) - 3):
segments.append(dia[j : j + 4])
return segments
def segments_that_intersect(self, move_row, move_col):
segments = [[self.board[pos[0],pos[1]] for pos in list] for list in pos_dict[(move_row,move_col)]]
return segments
# avalia um segmento e retorna a sua pontuação
def update_heuristics(self,move_row,move_col):
indexes = index_dict[(move_row,move_col)]
segments = self.segments_that_intersect(move_row, move_col)
for i in range(len(indexes)):
self.segment_heuristics[indexes[i]] = self.evaluate(segments[i])
def evaluate(self, segment):
count_x = 0
count_o = 0
for i in segment:
if i == PLAYER_PIECE:
count_x += 1
elif i == AI_PIECE:
count_o += 1
if (count_x == 0 and count_o == 0) or (count_x > 0 and count_o > 0):
return 0
match count_x:
case 1:
return 1
case 2:
return 10
case 3:
return 50
case 4:
return 512
match count_o:
case 1:
return -1
case 2:
return -10
case 3:
return -50
case 4:
return -512
# avalia todos as posições para descobrir se há vencedor
def evaluate_all(self):
if self.game_winner == PLAYER_PIECE:
return 512
elif self.game_winner == AI_PIECE:
return -512
elif self.board_is_full:
return 0
else:
sum = 0
if self.player() == PLAYER_PIECE:
sum = sum + 16
elif self.player() == AI_PIECE:
sum = sum - 16
for segment in self.get_segments():
sum = sum + self.evaluate(segment)
return sum
"""Verifica se um determinado estado é um estado terminal/final
(estado em que um dos jogadores ganhou ou em que não há ações possíveis)"""
def terminal(self):
if self.game_winner == PLAYER_PIECE or self.game_winner == AI_PIECE:
return True
else:
return self.board_is_full
"""Recebe um estado e retorna o vencedor (no caso deste existir)"""
def checkwin_wholeboard(self):
for (
segment
) in self.get_segments(): # Itera sobre todos os segmentos de tamanho 4
if np.array_equal(segment, ["X", "X", "X", "X"]):
return "X"
elif np.array_equal(segment, ["O", "O", "O", "O"]):
return "O"
return None
#Função que retorna qual é o proximo jogador.
def player(self):
if self.board_is_full:
return None
elif self.first == AI_PIECE:
if self.turn % 2 == 0:
return AI_PIECE
else:
return PLAYER_PIECE
elif self.first == PLAYER_PIECE:
if self.turn % 2 == 0:
return PLAYER_PIECE
else:
return AI_PIECE
else:
print("Problema na função player")
quit()
def utility(self):
if self.game_winner == PLAYER_PIECE:
return 512
elif self.game_winner == AI_PIECE:
return -512
else:
return 0
def stop_calculate(self): # Para uso no mcts, faz com que parem de ser calculado as heuristicas
self.calculate_heuristics = False
delattr(self, "segment_heuristics")
return