Exemple #1
0
    def select_move(self, game_state):
        """
            眼をつぶさないように,あとは禁じ手にならなければランダム.
            考えうる限り最弱のボット,30級程度
        """
        candidates = []
        for r in range(1, game_state.board.num_rows + 1):
            for c in range(1, game_state.board.num_cols + 1):
                candidate = Point(row=r, col=c)
                # 自殺手,劫なら次の手の候補ではない
                if not game_state.is_valid_move(Move.play(candidate)):
                    continue
                # 眼になっているところなら次の手の候補ではない
                if is_point_an_eye(game_state.board, candidate,
                                   game_state.next_player):
                    continue
                # 候補として追加
                candidates.append(candidate)

        # 打てるところが無ければパス
        if not candidates:
            return Move.pass_turn()

        move = Move.play(random.choice(candidates))
        return move
Exemple #2
0
 def select_move(self, game_state):
     candidates = []
     for r in range(1, game_state.board.num_rows + 1):
         for c in range(1, game_state.board.num_cols + 1):
             candidate = Point(row=r, col=c)
             if game_state.is_valid_move(Move.play(candidate)) and \
                     not is_point_an_eye(game_state.board, candidate, game_state.next_player):
                 candidates.append(candidate)
     if not candidates:
         return Move.pass_turn()
     return Move.play(random.choice(candidates))
Exemple #3
0
 def select_move(self, game_state):
     """Choose a random valid move that preserves our own eyes."""
     candidates = []
     for r in range(1, game_state.board.num_rows + 1):
         for c in range(1, game_state.board.num_cols + 1):
             candidate = Point(row=r, col=c)
             if (game_state.is_valid_move(Move.play(candidate))
                     and not is_point_an_eye(game_state.board, candidate,
                                             game_state.next_player)):
                 candidates.append(candidate)
     if not candidates:
         return Move.pass_turn()
     return Move.play(random.choice(candidates))
Exemple #4
0
    def test_new_game(self):
        start = GameState.new_game(19)
        next_state = start.apply_move(Move.play(Point(16, 16)))

        self.assertEqual(start, next_state.previous_state)
        self.assertEqual(Player.white, next_state.next_player)
        self.assertEqual(Player.black, next_state.board.get(Point(16, 16)))
Exemple #5
0
    def select_move(self, game_state):
        # Choose a random valid move that preserves our eyes
        candidates = []
        for r in range(1, game_state.board.num_rows + 1):
            for c in range(1, game_state.board.num_cols + 1):
                candidate = Point(row=r, col=c)
                # If a point is empty, not a self-capture, doesn't violate ko and preserve eyes
                if game_state.is_valid_move(Move.play(candidate)) and \
                    not is_point_an_eye(game_state.board,candidate,game_state.next_player):
                    candidates.append(candidate)

        # No valid moves then pass
        if not candidates:
            return Move.pass_turn()
        # Play a random move from the candidates
        return Move.play(random.choice(candidates))
Exemple #6
0
    def select_move(self, game_state):
        # choose a move that preserves own eyes
        dim = (game_state.board.num_rows, game_state.board.num_cols)

        if dim != self.dim:
            self._update_cache(dim)

        idx = np.arange(len(self.point_cache))
        np.random.shuffle(idx)

        for i in idx:
            p = self.point_cache[i]
            if game_state.is_valid_move(Move.play(p)) and \
                    not is_point_an_eye(game_state.board, p, game_state.next_player):
                return Move.play(p)

        return Move.pass_turn()  # can't make a move that doesn't ruin own eyes
Exemple #7
0
 def select_move(self, game_state):
     """Choose a random valid  move that preserves our own eyes"""
     # Get points that are a candidate for placing a stone
     candidates = []
     for r in range(1, game_state.board.num_rows + 1):
         for c in range(1, game_state.board.num_cols + 1):
             candidate = Point(row = r, col = c)
             # If a point within the board is a valid move, save it as a
             # candidate
             if game_state.is_valid_move(Move.play(candidate)) and \
                 not is_point_an_eye(game_state.board,
                                     candidate,
                                     game_state.next_player):
                 candidates.append(candidate)
     # If there are no candidates, pass
     if not candidates:
         return Move.pass_turn()
     # Else choose randomly among all the candidate
     return Move.play(random.choice(candidates))
Exemple #8
0
    def encode(self, game_state):
        global ZerosTime, TotalCount, TotalTime, GoStringTime, ViolateTime, DeepTime
        StartTime = time.time()
        board_tensor = np.zeros(self.shape())
        ZerosTime += time.time() - StartTime
        base_plane = {
            game_state.next_player: 0,  # what ??
            game_state.next_player.other: 3
        }
        for row in range(self.board_height):
            for col in range(self.board_width):
                p = Point(row=row + 1, col=col + 1)
                GoStringStartTime = time.time()
                go_string = game_state.board.get_go_string(p)
                GoStringTime += time.time() - GoStringStartTime
                if go_string is None:
                    ViolateStartTime = time.time()
                    if game_state.does_move_violate_ko(game_state.next_player,
                                                       Move.play(p), DeepTime):
                        board_tensor[6][row][
                            col] = 1  # 7th plane is invalid moves (KO RULE)
                    ViolateTime += time.time() - ViolateStartTime
                else:
                    liberty_plane = min(
                        3, go_string.num_liberties
                    ) - 1  # 1st plane (1 liberty) and so on
                    liberty_plane += base_plane[go_string.color]
                    board_tensor[liberty_plane][row][col] = 1
        TotalTime += time.time() - StartTime
        TotalCount += 1

        if TotalCount == 100:
            # print('encode', TotalTime)
            # print('zeros', ZerosTime)
            # print('GoString', GoStringTime)
            # print('violate', ViolateTime)
            # print('DeepTime', DeepTime['time'])
            # print()
            TotalCount = 0
            TotalTime = 0
            GoStringTime = 0
            ZerosTime = 0
            ViolateTime = 0
            DeepTime = {'time': 0}
        return board_tensor