Пример #1
0
    def put_stone(self, log: List[Stone]) -> Stone:
        turn = turn_check(log, self.p, self.q)
        if len(log) == 0:
            return Stone(str(10), str(10), turn)

        if len(log) == 1:
            next_x = int(log[0].x) + choice([-1, 1])
            next_y = int(log[0].y) + choice([-1, 1])
            return Stone(str(next_x), str(next_y), turn)

        log_x = [int(stone.x) for stone in log]
        log_y = [int(stone.y) for stone in log]

        left_end = max(min(log_x) - self.k, 1)
        right_end = min(max(log_x) + self.k, self.m)
        upper_end = max(min(log_y) - self.k, 1)
        lower_end = min(max(log_y) + self.k, self.n)

        width = right_end - left_end + 1
        height = lower_end - upper_end + 1

        focus_area = np.zeros((height, width), dtype=int)
        for stone in log:
            normalize_row_index = int(stone.y) - upper_end
            normalize_column_index = int(stone.x) - left_end
            focus_area[
                normalize_row_index,
                normalize_column_index] = 1 if stone.color == "b" else -1

        horizontal = self.find_connection(focus_area, Direction.HORIZONTAL,
                                          left_end, upper_end)
        vertical = self.find_connection(focus_area, Direction.VERTICAL,
                                        left_end, upper_end)
        negative_diagonal = self.find_connection(focus_area,
                                                 Direction.NEGATIVE_DIAGONAL,
                                                 left_end, upper_end)
        positive_diagonal = self.find_connection(focus_area,
                                                 Direction.POSITIVE_DIAGONAL,
                                                 left_end, upper_end)
        candidates = horizontal + vertical + negative_diagonal + positive_diagonal
        if len(candidates) == 0:
            random_empty_spot = choice(self.empty_spots(log))
            return Stone(random_empty_spot.x, random_empty_spot.y, turn)

        max_stone = max(
            [self.stone_sum(candidate) for candidate in candidates])
        if max_stone >= self.k:
            return Stone("1", "1", turn)
        max_stone_candidates = [
            candidate for candidate in candidates
            if self.stone_sum(candidate) == max_stone
        ]
        max_stone_candidate = choice(max_stone_candidates)
        window_index = (sorted(
            [i for i, x in enumerate(max_stone_candidate.pattern) if x == 0],
            key=lambda x: abs(x - self.k / 2),
        )[0])
        position = max_stone_candidate.position[window_index]

        return Stone(str(position[0]), str(position[1]), turn)
Пример #2
0
    def simulate(self, p1: str, p2: str, render_every: bool = False, pause: float = 1.0):
        while True:
            turn = util.turn_check(self.board.log, self.board.config.each_move, self.board.config.first_move)
            if self.first_player_first_move == (turn == "b"):
                order = p1
                stone = self.bot_alias.get(p1, util.exit_by_alias)(self.board.log)
            else:
                order = p2
                stone = self.bot_alias.get(p2, util.exit_by_alias)(self.board.log)

            if not self.referee.valid_check(stone, self.board.log):
                print("invalid")
                continue

            self.board.put_stone(stone)
            if self.debug:
                print(f"{order} stone: {stone.x},{stone.y},{turn}")
            end_check = self.referee.end_check(self.board.log, self.board.config)
            if end_check.is_end:
                self.winner = "no one" if end_check.is_tie else order
                print(f"{self.winner} wins")
                self.board.print_winner(self.winner)
                if self.debug:
                    self.board.render_figure()
                break

            if render_every:
                # self.board.render_figure()
                self.board.draw_board(self.board.log)
                time.sleep(pause)
Пример #3
0
    def play(self, render_every: bool = True):
        while True:
            turn = util.turn_check(self.board.log, self.board.config.each_move, self.board.config.first_move)
            if self.first_player_first_move == (turn == "b"):
                order = "player"
                stone = self.player_input(turn)
            else:
                order = "bot"
                stone = self.bot.random_bot(turn)

            if not self.referee.valid_check(stone, self.board.log):
                print("invalid")
                continue

            self.board.put_stone(stone)
            print(f"{order} stone: {stone.x},{stone.y},{turn}")
            end_check = self.referee.end_check(self.board.log, self.board.config)
            if end_check.is_end:
                winner = "no one" if end_check.is_tie else order
                print(f"{winner} wins")
                self.board.print_winner(winner)
                self.board.render_figure()
                break

            if render_every:
                self.board.render_figure()
Пример #4
0
 def suggest_positions(self, log: List[Stone]) -> List[Stone]:
     turn = turn_check(log, self.p, self.q)
     stone_array = self.stone_to_array(log)
     suggestions = suggest_position_by_connected_element(stone_array, turn) * -1
     suggestions = np.clip(stone_array + suggestions, -1, 0)
     suggestions = self.array_to_stone(suggestions, "s")
     return suggestions
Пример #5
0
 def put_stone(self, log: List[Stone]) -> Stone:
     turn = turn_check(log, self.p, self.q)
     if len(log) == 0:
         x = randint(1, self.n)
         y = randint(1, self.m)
         return Stone(str(x), str(y), turn)
     position = choice(self.suggest_positions(log))
     return Stone(position.x, position.y, turn)
Пример #6
0
def play():
    x = request.args.get("x")
    y = request.args.get("y")
    color = request.args.get("color")

    if regex_digit.match(x) is None:
        return {"code": 400, "message": "illegal input in x"}, 400

    if regex_digit.match(y) is None:
        return {"code": 400, "message": "illegal input in y"}, 400

    if regex_color.match(color) is None:
        return {"code": 400, "message": "illegal input in stone"}, 400

    if color != util.turn_check(board.log, board.config.each_move,
                                board.config.first_move):
        board.print_illegal_turn()
        board.save_figure()
        return render_template("board.html")

    stone = Stone(x, y, color)
    if not referee.valid_check(stone, board.log):
        board.print_illegal_stone()
        board.save_figure()
        return render_template("board.html")

    board.put_stone(stone)
    end_check = referee.end_check(board.log, board.config)
    if end_check.is_end:
        winner = "no one" if end_check.is_tie else color
        board.print_winner(winner)
        board.save_figure()
        return render_template("board.html")

    board.print_on_playing()
    board.save_figure()
    return render_template("board.html")