Last active
August 29, 2023 10:31
-
-
Save masouduut94/fb63371ba1d2404d1bb5d5282008b592 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DecisiveMoveMctsAgent(RaveMctsAgent): | |
def roll_out(self, state: GameState) -> tuple: | |
""" | |
Simulate a random game except that we play all known critical cells | |
first, return the winning player and record critical cells at the end. | |
""" | |
moves = state.moves() | |
good_moves = moves.copy() | |
good_opponent_moves = moves.copy() | |
to_play = state.turn() | |
while state.winner == GameMeta.PLAYERS["none"]: | |
done = False | |
# In each state transition, check if there is any critical move between all possible moves. | |
while len(good_moves) > 0 and not done: | |
move = choice(good_moves) | |
good_moves.remove(move) | |
if not state.would_lose(move, to_play): | |
# is this chosen move ending in win | |
state.play(move) | |
moves.remove(move) | |
if move in good_opponent_moves: | |
good_opponent_moves.remove(move) | |
done = True | |
if not done: | |
move = choice(moves) | |
state.play(move) | |
moves.remove(move) | |
if move in good_opponent_moves: | |
good_opponent_moves.remove(move) | |
# state has changed, so good moves are exchanged between players. | |
good_moves, good_opponent_moves = good_opponent_moves, good_moves | |
black_rave_pts = [] | |
white_rave_pts = [] | |
for x in range(state.size): | |
for y in range(state.size): | |
if state.board[(x, y)] == GameMeta.PLAYERS["black"]: | |
black_rave_pts.append((x, y)) | |
elif state.board[(x, y)] == GameMeta.PLAYERS["white"]: | |
white_rave_pts.append((x, y)) | |
return state.winner, black_rave_pts, white_rave_pts |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment