全局变量和初始设置
import osboard = [[0 for _ in range(9)] for _ in range(9)]
player_row = 0
player_col = 0
cpu_row = 0
cpu_col = 0
board
是一个9x9的二维列表,用于表示棋盘。0
表示空位,1
表示CPU的棋子,2
表示玩家的棋子。player_row
和player_col
记录玩家最近一次落子的行和列。cpu_row
和cpu_col
记录CPU最近一次落子的行和列。
工具函数
def isBlack(row, col):return board[row][col] == 2
isBlack()
函数检查给定位置是否是玩家的棋子(黑子)。
胜利检查函数
def checkWin(row, col, player):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 < 9 and 0 <= c < 9 and board[r][c] == player:count += 1else:breakfor i in range(1, 5):r, c = row - i * dr, col - i * dcif 0 <= r < 9 and 0 <= c < 9 and board[r][c] == player:count += 1else:breakif count >= 5:return Truereturn False
checkWin()
函数用于检查某个玩家在给定位置是否获胜。- 它检查四个方向(水平、垂直、两个对角线)是否有连续5个相同的棋子。
dr
和dc
分别表示行和列的增量,用于方向控制。
玩家和CPU胜利检查
def isPlayerWin():return checkWin(player_row, player_col, 2)def isCPUWin():return checkWin(cpu_row, cpu_col, 1)
isPlayerWin()
和isCPUWin()
使用checkWin()
函数来检查玩家和CPU是否获胜。
落子函数
def down(row, col, is_player):global cpu_row, cpu_colif is_player:board[row][col] = 2else:board[row][col] = 1cpu_row, cpu_col = row, col
down()
函数用于在指定位置落子。- 根据
is_player
参数决定落下的是玩家的棋子还是CPU的棋子。
显示棋盘
def showBoard():os.system('cls' if os.name == 'nt' else 'clear')print(' ' + ' '.join(str(i+1) for i in range(9)))print(' +' + '---+' * 9)for r in range(len(board)):data = f'{r+1} |'for c in range(len(board[r])):if board[r][c] == 0:data += ' |'elif board[r][c] == 1:data += ' w |'else:data += ' y |'print(data)print(' +' + '---+' * 9)
showBoard()
函数在控制台上绘制棋盘。- 使用不同的符号表示空位、CPU的棋子和玩家的棋子。
显示棋盘改进点:
-
边框和分隔线:
- 每行元素之间和列之间添加了
|
和+---+
作为分隔符,使棋盘看起来更像一个表格。 - 给顶行和每行添加了列号,方便用户识别位置。
- 每行元素之间和列之间添加了
-
输出格式:
- 使用f-string格式化输出,使代码更简洁和易读。
- 增加了行号和列号的对齐,确保数字和符号输出在同一水平线上。
玩家回合
def playerRound():global player_row, player_colwhile True:player_input = input('你是黑子,请你输入行列来落子,例如:18 表示1行8列:')if len(player_input) != 2 or not player_input.isdigit():print("输入格式错误,请输入两个数字,如:18")continueplayer_row = int(player_input[0]) - 1player_col = int(player_input[1]) - 1if 0 <= player_row < 9 and 0 <= player_col < 9 and board[player_row][player_col] == 0:down(player_row, player_col, is_player=True)breakelse:print("位置不合法,请重新输入。")
playerRound()
函数处理玩家的回合。- 检查用户输入是否合法,并在合法位置落子。
- 错误输入会提示玩家重新输入。
CPU回合
def cpuRound():directions = [(0, 1), (1, 0), (1, 1), (1, -1)]max_continue_count = 0final_row = Nonefinal_col = Nonefor row in range(9):for col in range(9):if board[row][col] != 0:continuefor dr, dc in directions:continue_count = 1for i in range(1, 5):r, c = row + i * dr, col + i * dcif 0 <= r < 9 and 0 <= c < 9 and board[r][c] == 2:continue_count += 1else:breakfor i in range(1, 5):r, c = row - i * dr, col - i * dcif 0 <= r < 9 and 0 <= c < 9 and board[r][c] == 2:continue_count += 1else:breakif continue_count > max_continue_count:max_continue_count = continue_countfinal_row = rowfinal_col = colif final_row is not None and final_col is not None:down(final_row, final_col, is_player=False)else:for row in range(9):for col in range(9):if board[row][col] == 0:down(row, col, is_player=False)return
cpuRound()
函数处理CPU的回合。- 方法是检查所有空位置,计算每个位置在四个方向上与玩家棋子连成一线的最大连续数。
- 优先阻止玩家形成五子连珠。
主游戏循环
while True:showBoard()playerRound()if isPlayerWin():showBoard()print('恭喜!你赢了!')breakcpuRound()if isCPUWin():showBoard()print('哎!你输了!')break
- 主循环不断交替进行玩家和CPU的回合。
- 每回合结束后检查是否有一方获胜,如果有,显示结果并结束游戏。
这段代码实现了基本的五子棋游戏,其中包括输入验证、胜利检查、棋盘显示等功能。游戏通过回合制的方式进行,直到一方获胜为止。