Exemplo n.º 1
0
    def step(self, action):
        """

        :param int action: move pos=0 ~ 63 (0=top left, 7 top right, 63 bottom right)
        :return:
        """
        assert 0 <= action <= 63, f"Illegal action={action}"

        own, enemy = self.get_own_and_enemy()

        flipped = calc_flip(action, own, enemy)
        if bit_count(flipped) == 0:
            self.illegal_move_to_lose(action)
            return self.board, {}
        own ^= flipped
        own |= 1 << action
        enemy ^= flipped

        self.set_own_and_enemy(own, enemy)
        self.turn += 1

        if bit_count(find_correct_moves(
                enemy, own)) > 0:  # there are legal moves for enemy.
            self.change_to_next_player()
        elif bit_count(find_correct_moves(
                own, enemy)) > 0:  # there are legal moves for me but enemy.
            pass
        else:  # there is no legal moves for me and enemy.
            self._game_over()

        return self.board, {}
Exemplo n.º 2
0
    def _should_game_over(self, ):
        own, enemy = self.get_own_and_enemy()

        if bit_count(enemy) + bit_count(own) >= 64:
            return True
        else:
            return bit_count(find_correct_moves(enemy, own)) == 0 and \
                   bit_count(find_correct_moves(own, enemy)) == 0
def test_dirichlet_noise_of_mask():
    legal_moves = 47289423
    bc = bit_count(legal_moves)
    noise = dirichlet_noise_of_mask(legal_moves, 0.5)
    assert_almost_equal(1, np.sum(noise))
    eq_(bc, np.sum(noise > 0))
    ary = bit_to_array(legal_moves, 64)
    eq_(list(noise), list(noise * ary))
Exemplo n.º 4
0
    def step(self, action):
        """

        :param int action: move pos=0 ~ 64 (0=top left, 7 top right, 63 bottom right, 64 pass)
        :return:
        """
        assert 0 <= action <= 64, f"Illegal action={action}"
        action_pass = 64

        if action != action_pass:
            own, enemy = self.get_own_and_enemy()

            assert own & (1 << action) == 0, f'{own}, {action}'
            assert enemy & (1 << action) == 0, f'{enemy}, {action}'

            flipped = calc_flip(action, own, enemy)
            if bit_count(flipped) == 0:
                raise Exception(
                    f'{self.next_player} played illegal move to lose in turn {self.turn}!'
                )
                print()
                self._board_history.append(self.board.copy())
                self.turn += 1
                self.change_to_next_player()
                self.illegal_move_to_lose(action)
                return self.board, {}
            own ^= flipped
            own |= 1 << action
            enemy ^= flipped

            self.set_own_and_enemy(own, enemy)

        self._board_history.append(self.board.copy())
        self.turn += 1
        self.change_to_next_player()

        if self._should_game_over():
            self._game_over()

        return self.board, {}
Exemplo n.º 5
0
 def number_of_black_and_white(self):
     return bit_count(self.black), bit_count(self.white)