finish = False # 游戏是否结束flagNum = 1 # 当前下棋者标记flagch = '*' # 当前下棋者棋子x = 0 # 当前棋子的横坐标y = 0 # 当前棋子的纵坐标print('\033[1;37;41m---------简易五子棋游戏(控制台版)---------\033[0m')# 棋盘初始化checkerboard=[]for i in range(10):checkerboard.append([])for j in range(10):checkerboard[i].append('-')def msg():# 输出最后胜利的棋盘print("\033[1;37;44m--------------------------------")print(" 1 2 3 4 5 6 7 8 9 10")for i in range(len(checkerboard)):print(chr(i + ord('A')) + " ", end=' ')for j in range(len(checkerboard[i])):print(checkerboard[i][j] + " ", end=' ')print()print("--------------------------------\033[0m")# 输出赢家if (flagNum == 1):print('\033[32m*棋胜利!***\033[0m')else:print('\033[32mo棋胜利!***\033[0m')while not finish:# 打印棋盘print("\033[1;30;46m--------------------------------")print(" 1 2 3 4 5 6 7 8 9 10")for i in range(len(checkerboard)):print(chr(i + ord('A')) + " ", end=' ');for j in range(len(checkerboard[i])):print(checkerboard[i][j] + " ", end=' ')print()print("--------------------------------\033[0m")# 判断当前下棋者if flagNum == 1:flagch = '*'print('\033[1;37;40m请*输入棋子坐标(例如A1):\033[0m', end=' ') # 白字黑底else:flagch = 'o'print('\033[1;30;42m请o输入棋子坐标(例如J5):\033[0m', end=' ') # 黑字绿底# 输入棋子坐标str = input()ch = str[0] # 获取第一个字符的大写形式x = ord(ch) - 65y = int(str[1]) - 1# 判断坐标是否在棋盘之内if (x < 0 or x > 9 or y < 0 or y > 9):print('\033[31m***您输入的坐标有误请重新输入!***\033[0m')continue# 判断坐标上是否有棋子if (checkerboard[x][y] == '-'):if (flagNum == 1):checkerboard[x][y] = '*'else:checkerboard[x][y] = 'o'else:print('\033[31m******您输入位置已经有其他棋子,请重新输入!\033[0m')continue# 判断棋子左侧if (y - 4 >= 0):if (checkerboard[x][y - 1] == flagchand checkerboard[x][y - 2] == flagchand checkerboard[x][y - 3] == flagchand checkerboard[x][y - 4] == flagch):finish = Truemsg()# 判断棋子右侧if (y + 4 <= 9):if (checkerboard[x][y + 1] == flagchand checkerboard[x][y + 2] == flagchand checkerboard[x][y + 3] == flagchand checkerboard[x][y + 4] == flagch):finish = Truemsg()# 判断棋子上方if (x - 4 >= 0):if (checkerboard[x - 1][y] == flagchand checkerboard[x - 2][y] == flagchand checkerboard[x - 3][y] == flagchand checkerboard[x - 4][y] == flagch):finish = Truemsg()# 判断棋子下方if (x + 4 <= 9):if (checkerboard[x + 1][y] == flagchand checkerboard[x + 2][y] == flagchand checkerboard[x + 3][y] == flagchand checkerboard[x + 4][y] == flagch):finish = Truemsg()# 判断棋子右上方向if (x - 4 >= 0 and y - 4 >= 0):if (checkerboard[x - 1][y - 1] == flagchand checkerboard[x - 2][y - 2] == flagchand checkerboard[x - 3][y - 3] == flagchand checkerboard[x - 4][y - 4] == flagch):finish = Truemsg()# 判断棋子右下方向if (x + 4 <= 9 and y - 4 >= 0):if (checkerboard[x + 1][y - 1] == flagchand checkerboard[x + 2][y - 2] == flagchand checkerboard[x + 3][y - 3] == flagchand checkerboard[x + 4][y - 4] == flagch):finish = Truemsg()# 判断棋子左上方向if (x - 4 >= 0 and y + 4 <= 9):if (checkerboard[x - 1][y + 1] == flagchand checkerboard[x - 2][y + 2] == flagchand checkerboard[x - 3][y + 3] == flagchand checkerboard[x - 4][y + 4] == flagch):finish = Truemsg()# 判断棋子左下方向if (x + 4 <= 9 and y + 4 <= 9):if (checkerboard[x + 1][y + 1] == flagchand checkerboard[x + 2][y + 2] == flagchand checkerboard[x + 3][y + 3] == flagchand checkerboard[x + 4][y + 4] == flagch):finish = Truemsg()flagNum *= -1; # 更换下棋者标记
如果觉得《用python实现简易控制台五子棋小游戏》对你有帮助,请点赞、收藏,并留下你的观点哦!