def play_game(players, num_samples=100, num_simulations=None, total_simulation_seconds=1): tiles_by_player = deal_tiles() first_player = random.choice([0, 1, 2, 3]) state = DominoState(first_player, { 'tiles_by_player': tiles_by_player, 'suits_at_ends': set() }) game = [state] log(f"Starts player {first_player}") log("Tiles : ") log(pformat(state._tiles_by_player)) while not state.is_terminal(): log("=======================================") log(pformat(state._tiles_by_player[state._current_player])) state = play_with_algo( players[state._current_player], state, game, num_samples, num_simulations=num_simulations, total_simulation_seconds=total_simulation_seconds) game.append(state) print_state(state) log(f"winneeeer {state.calc_reward()}") log(pformat(state._tiles_by_player)) record_winner(state._tiles_by_player) return (game, state.calc_reward())
def test_is_terminal_game_closed(self): state = DominoState( 1,{ 'tiles_by_player':[ [{5,4}], [{3, 5}], [{6} ], [{1,0}] ], 'suits_at_ends':{2} }) self.assertTrue(state.is_terminal())
def test_is_terminal_team1_win(self): state = DominoState( 0,{ 'tiles_by_player':[ [{5,4}], [{3, 5}], [{6} ], [] ], 'suits_at_ends':{2} }) self.assertTrue(state.is_terminal())
class GameOfDomino(TwoPlayersGame): def __init__(self, players): self.players = players self.nplayer = 1 # player 1 starts self._state = DominoState() def possible_moves(self): return self._state.get_possible_actions() def make_move(self, move): # self.pile -= int(move) # remove bones. self._state = self._state.next_state_from_action(move) def win(self): # return self.pile<=0 # opponent took the last bone ? winner = self._state.calc_reward() team_1 = self._state.team_1 team_2 = self._state.team_2 if winner == 0: return False if winner == team_1 and self.nplayer == 1: return True if winner == team_2 and self.nplayer == 2: return True return False def is_over(self): # return self.win() # Game stops when someone wins. return self._state.is_terminal() def show(self): # print ("%d bones left in the pile" % self.pile) print(self._state) def scoring(self): return 100 if self.win() else 0 # For the AI