佛山建设工程交易中心网站,网站推广的方式包括哪些,wordpress局限性,天津网站建设需要多少钱前言 在本教程中#xff0c;我们将使用Python写一个创意五子棋游戏 #x1f4dd;个人主页→数据挖掘博主ZTLJQ的主页 个人推荐python学习系列#xff1a; ☄️爬虫JS逆向系列专栏 - 爬虫逆向教学 ☄️python系列专栏 - 从零开始学python 首先 GomokuGame 类的构造函数 __ini… 前言 在本教程中我们将使用Python写一个创意五子棋游戏 个人主页→数据挖掘博主ZTLJQ的主页 个人推荐python学习系列 ☄️爬虫JS逆向系列专栏 - 爬虫逆向教学 ☄️python系列专栏 - 从零开始学python 首先 GomokuGame 类的构造函数 __init__ 初始化游戏。board_size 参数默认为 15表示棋盘大小。board 是一个二维列表代表棋盘上的状态。current_player 记录当前玩家初始为 X。winner 记录胜利者初始为 None。 class GomokuGame:def __init__(self, board_size15):self.board_size board_sizeself.board [[ for _ in range(board_size)] for _ in range(board_size)]self.current_player Xself.winner Noneprint_board 方法用于打印当前棋盘的状态。首先它打印列索引。然后遍历每一行打印出当前行的棋子状态。 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()make_move 方法用于玩家落子。如果指定位置为空将当前玩家的标记放置在该位置。然后调用 check_winner 方法检查是否有胜利者。最后切换当前玩家。 def make_move(self, row, col):if self.board[row][col] :self.board[row][col] self.current_playerif self.check_winner(row, col):self.winner self.current_playerself.current_player X if self.current_player O else Oreturn Truereturn Falsecheck_winner 方法用于检查是否有玩家获胜。它通过检查当前位置的四个方向来判断是否有五个连续相同的棋子。如果存在则返回 True表示有玩家获胜。 def check_winner(self, row, col):directions [(0, 1), (1, 0), (1, 1), (1, -1)]for dr, dc in directions:count 1for i in range(1, 5):r, c row i * dr, col i * dcif 0 r self.board_size and 0 c self.board_size and self.board[r][c] self.current_player:count 1else:breakfor i in range(1, 5):r, c row - i * dr, col - i * dcif 0 r self.board_size and 0 c self.board_size and self.board[r][c] self.current_player:count 1else:breakif count 5:return Truereturn Falseplay 方法是游戏的主循环。它持续运行直到有玩家获胜。在每一轮中它打印当前棋盘然后获取玩家输入的行和列进行落子操作。如果落子无效则要求玩家重新输入。如果有玩家获胜则打印胜利信息并结束游戏。 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))breakelse:print(无效移动。再试一次。)else:print(输入无效。再试一次。)except ValueError:print(输入无效。输入一个数字。)在代码的末尾通过这个条件判断确保仅在直接运行该脚本时才会执行创建游戏对象并开始游戏。 if __name__ __main__:game GomokuGame()game.play()下面是完全代码可以直接复制运行 class GomokuGame:def __init__(self, board_size15):self.board_size board_sizeself.board [[ for _ in range(board_size)] for _ in range(board_size)]self.current_player Xself.winner Nonedef 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_playerif self.check_winner(row, col):self.winner self.current_playerself.current_player X if self.current_player O else Oreturn Truereturn Falsedef check_winner(self, row, col):directions [(0, 1), (1, 0), (1, 1), (1, -1)]for dr, dc in directions:count 1for i in range(1, 5):r, c row i * dr, col i * dcif 0 r self.board_size and 0 c self.board_size and self.board[r][c] self.current_player:count 1else:breakfor i in range(1, 5):r, c row - i * dr, col - i * dcif 0 r self.board_size and 0 c self.board_size and self.board[r][c] self.current_player:count 1else:breakif count 5:return Truereturn Falsedef 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))breakelse:print(无效移动。再试一次。)else:print(输入无效。再试一次。)except ValueError:print(输入无效。输入一个数字。)if __name__ __main__:game GomokuGame()game.play()