Skip to content

Instantly share code, notes, and snippets.

@masouduut94
Last active August 29, 2023 10:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masouduut94/fb63371ba1d2404d1bb5d5282008b592 to your computer and use it in GitHub Desktop.
Save masouduut94/fb63371ba1d2404d1bb5d5282008b592 to your computer and use it in GitHub Desktop.
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