Ejemplo n.º 1
0
    def choose_move(self, game_state: GameState) -> int:
        """ Choose a move for the given board.

        :param game_state: the current state of the game.
        :return: the chosen move's index in the list of valid moves.
        """
        self.search_manager.search(game_state, self.iteration_count)
        if game_state.get_move_count() < 15:
            return self.search_manager.choose_weighted_move()
        return self.search_manager.get_best_move()
Ejemplo n.º 2
0
 def on_game_ended(self, game_state: GameState):
     if (self.is_history_dirty or self.display is None
             or self.ui.searches_lock1.isChecked()):
         return
     db_session = self.db_session
     if db_session is None:
         return
     game_record = GameRecord.find_or_create(db_session, game_state)
     game_end_time = datetime.now()
     game_duration = game_end_time - self.game_start_time
     match_record = MatchRecord(game=game_record,
                                start_time=self.game_start_time,
                                total_seconds=round(
                                    game_duration.total_seconds()),
                                move_count=game_state.get_move_count())
     db_session.add(match_record)
     winner = game_state.get_winner()
     mcts_player: typing.Optional[MctsPlayer]
     for player_number in game_state.get_players():
         mcts_player = self.display.get_player(player_number)
         if mcts_player is None:
             player_record = db_session.query(PlayerRecord).filter_by(
                 type=PlayerRecord.HUMAN_TYPE).one_or_none()
             if player_record is None:
                 player_record = PlayerRecord(type=PlayerRecord.HUMAN_TYPE)
                 db_session.add(player_record)
         else:
             player_record = db_session.query(PlayerRecord).filter_by(
                 type=PlayerRecord.PLAYOUT_TYPE,
                 iterations=mcts_player.iteration_count).one_or_none()
             if player_record is None:
                 player_record = PlayerRecord(
                     type=PlayerRecord.PLAYOUT_TYPE,
                     iterations=mcts_player.iteration_count)
                 db_session.add(player_record)
         if player_number == winner:
             result = 1
         elif winner == game_state.NO_PLAYER:
             result = 0
         else:
             result = -1
         match_player = MatchPlayerRecord(match=match_record,
                                          player=player_record,
                                          player_number=player_number,
                                          result=result)
         db_session.add(match_player)
     db_session.commit()
     try:
         mcts_player, = self.display.mcts_players
     except ValueError:
         # Didn't have exactly one MCTS player
         return
     assert mcts_player is not None
     winning_player = game_state.get_winner()
     if winning_player == mcts_player.player_number:
         score = -1
     elif winning_player == GameState.NO_PLAYER:
         score = 0
     else:
         score = 1
     settings = get_settings(self.start_state)
     strength_adjuster = StrengthAdjuster(
         strength=mcts_player.iteration_count,
         game_count=settings.value('game_count', 0, int),
         last_score=settings.value('last_score', 0, int),
         streak_length=settings.value('streak_length', 1, int))
     strength_adjuster.record_score(score)
     settings.setValue('searches', strength_adjuster.strength)
     settings.setValue('game_count', strength_adjuster.game_count)
     settings.setValue('last_score', strength_adjuster.last_score)
     settings.setValue('streak_length', strength_adjuster.streak_length)