前言
在本教程中,我们将使用Python写一个创意五子棋游戏
📝个人主页→数据挖掘博主ZTLJQ的主页
个人推荐python学习系列:
☄️爬虫JS逆向系列专栏 - 爬虫逆向教学
☄️python系列专栏 - 从零开始学python
首先
GomokuGame
类的构造函数__init__
初始化游戏。board_size
参数默认为 15,表示棋盘大小。board
是一个二维列表,代表棋盘上的状态。current_player
记录当前玩家,初始为 'X'。winner
记录胜利者,初始为 None。
class GomokuGame:def __init__(self, board_size=15):self.board_size = board_sizeself.board = [[' ' for _ in range(board_size)] for _ in range(board_size)]self.current_player = 'X'self.winner = None
print_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 'O'return Truereturn False
check_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 False
play
方法是游戏的主循环。它持续运行直到有玩家获胜。在每一轮中,它打印当前棋盘,然后获取玩家输入的行和列,进行落子操作。如果落子无效,则要求玩家重新输入。如果有玩家获胜,则打印胜利信息并结束游戏。
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_size=15):self.board_size = board_sizeself.board = [[' ' for _ in range(board_size)] for _ in range(board_size)]self.current_player = 'X'self.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 'O'return 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()