Skip to content

Instantly share code, notes, and snippets.

@cashlo
Created June 3, 2020 16:30
Show Gist options
  • Save cashlo/75cc148e8aa498b35c73d6279ce612f9 to your computer and use it in GitHub Desktop.
Save cashlo/75cc148e8aa498b35c73d6279ce612f9 to your computer and use it in GitHub Desktop.
Gomoku MCST implementation
class GomokuSearchTree(Node):
def __init__(self, parent, board, from_move, next_player, simulation_limit=1, exploration_constant=1):
Node.__init__(self, parent=parent, simulation_limit=simulation_limit, exploration_constant=exploration_constant)
self.board = board
self.from_move = from_move
self.next_player = next_player
def create_from_move(self, move):
new_board = self.board.clone_board()
new_board.place_move(move, self.next_player)
return GomokuSearchTree( self, new_board, move, Gomoku.other(self.next_player), exploration_constant=self.exploration_constant)
def rollout(self):
simulation_board = self.board.clone_board()
player = self.next_player
while simulation_board.check_board() == Gomoku.IN_PROGRESS:
move = simulation_board.random_move(player)
simulation_board.place_move(move, player)
player = Gomoku.other(player)
result = simulation_board.check_board()
last_player = Gomoku.other(self.next_player)
if result == last_player:
return 1
if result == Gomoku.DRAW:
return 0
return -1
def get_all_possible_moves(self):
if self.is_terminal():
return []
return [i for i, p in enumerate(self.board.board) if p == Gomoku.EMPTY]
def is_terminal(self):
return self.board.check_board() != Gomoku.IN_PROGRESS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment