class GomokuGame:
    def __init__(self, board_size=15):
        self.board_size = board_size
        self.board = [[' ' for _ in range(board_size)] for _ in range(board_size)]
        self.current_player = 'X'
        self.winner = None
    def print_board(self):
        print("  " + " ".join(str(i) for i in range(self.board_size)))
        for i in range(self.board_size):
            print(str(i) + " " + " ".join(self.board[i]))
        print()
    def make_move(self, row, col):
        if self.board[row][col] == ' ':
            self.board[row][col] = self.current_player
            if self.check_winner(row, col):
                self.winner = self.current_player
            self.current_player = 'X' if self.current_player == 'O' else 'O'
            return True
        return False
    def check_winner(self, row, col):
        directions = [(0, 1), (1, 0), (1, 1), (1, -1)]
        for dr, dc in directions:
            count = 1
            for i in range(1, 5):
                r, c = row + i * dr, col + i * dc
                if 0 <= r < self.board_size and 0 <= c < self.board_size and self.board[r][c] == self.current_player:
                    count += 1
                else:
                    break
            for i in range(1, 5):
                r, c = row - i * dr, col - i * dc
                if 0 <= r < self.board_size and 0 <= c < self.board_size and self.board[r][c] == self.current_player:
                    count += 1
                else:
                    break
            if count >= 5:
                return True
        return False
    def play(self):
        while not self.winner:
            self.print_board()
            try:
                row = int(input("玩家{}的回合,请输入你要下的行数: ".format(self.current_player)))
                col = int(input("请输入你要下的列数: "))
                if 0 <= row < self.board_size and 0 <= col < self.board_size:
                    if self.make_move(row, col):
                        if self.winner:
                            self.print_board()
                            print("玩家{},你赢了".format(self.winner))
                            break
                    else:
                        print("无效移动。再试一次。")
                else:
                    print("输入无效。再试一次。")
            except ValueError:
                print("输入无效。输入一个数字。")
if __name__ == "__main__":
    game = GomokuGame()
    game.play()
class GomokuGame:
    def __init__(self, board_size=15):
        self.board_size = board_size
        self.board = [[' ' for _ in range(board_size)] for _ in range(board_size)]
        self.current_player = 'X'
        self.winner = None

    def print_board(self):
        print("  " + " ".join(str(i) for i in range(self.board_size)))
        for i in range(self.board_size):
            print(str(i) + " " + " ".join(self.board[i]))
        print()

    def make_move(self, row, col):
        if self.board[row][col] == ' ':
            self.board[row][col] = self.current_player
            if self.check_winner(row, col):
                self.winner = self.current_player
            self.current_player = 'X' if self.current_player == 'O' else 'O'
            return True
        return False

    def check_winner(self, row, col):
        directions = [(0, 1), (1, 0), (1, 1), (1, -1)]
        for dr, dc in directions:
            count = 1
            for i in range(1, 5):
                r, c = row + i * dr, col + i * dc
                if 0 <= r < self.board_size and 0 <= c < self.board_size and self.board[r][c] == self.current_player:
                    count += 1
                else:
                    break
            for i in range(1, 5):
                r, c = row - i * dr, col - i * dc
                if 0 <= r < self.board_size and 0 <= c < self.board_size and self.board[r][c] == self.current_player:
                    count += 1
                else:
                    break
            if count >= 5:
                return True
        return False

    def play(self):
        while not self.winner:
            self.print_board()
            try:
                row = int(input("玩家{}的回合,请输入你要下的行数: ".format(self.current_player)))
                col = int(input("请输入你要下的列数: "))
                if 0 <= row < self.board_size and 0 <= col < self.board_size:
                    if self.make_move(row, col):
                        if self.winner:
                            self.print_board()
                            print("玩家{},你赢了".format(self.winner))
                            break
                    else:
                        print("无效移动。再试一次。")
                else:
                    print("输入无效。再试一次。")
            except ValueError:
                print("输入无效。输入一个数字。")

if __name__ == "__main__":
    game = GomokuGame()
    game.play()
08-20 18:46