文件结构
1 finish = False # 游戏是否结束 2 flagNum = 1 # 当前下棋者标记 3 flagch = '*' # 当前下棋者棋子 4 x = 0 # 当前棋子的横坐标 5 y = 0 # 当前棋子的纵坐标 6 print('\033[1;37;41m---------简易五子棋游戏(控制台版)---------\033[0m') 7 # 棋盘初始化 8 checkerboard=[] 9 for i in range(10): 10 checkerboard.append([]) 11 for j in range(10): 12 checkerboard[i].append('-') 13 def msg(): 14 # 输出最后胜利的棋盘 15 print("\033[1;37;44m--------------------------------") 16 print(" 1 2 3 4 5 6 7 8 9 10") 17 for i in range(len(checkerboard)): 18 print(chr(i + ord('A')) + " ", end=' ') 19 for j in range(len(checkerboard[i])): 20 print(checkerboard[i][j] + " ", end=' ') 21 print() 22 print("--------------------------------\033[0m") 23 # 输出赢家 24 if (flagNum == 1): 25 print('\033[32m*棋胜利!***\033[0m') 26 else: 27 print('\033[32mo棋胜利!***\033[0m') 28 29 while not finish: 30 # 打印棋盘 31 print("\033[1;30;46m--------------------------------") 32 print(" 1 2 3 4 5 6 7 8 9 10") 33 for i in range(len(checkerboard)): 34 print(chr(i + ord('A')) + " ", end=' '); 35 for j in range(len(checkerboard[i])): 36 print(checkerboard[i][j] + " ", end=' ') 37 print() 38 print("--------------------------------\033[0m") 39 # 判断当前下棋者 40 if flagNum == 1: 41 flagch = '*' 42 print('\033[1;37;40m请*输入棋子坐标(例如A1):\033[0m', end=' ') # 白字黑底 43 else: 44 flagch = 'o' 45 print('\033[1;30;42m请o输入棋子坐标(例如J5):\033[0m', end=' ') # 黑字绿底 46 # 输入棋子坐标 47 str = input() 48 ch = str[0] # 获取第一个字符的大写形式 49 x = ord(ch) - 65 50 y = int(str[1]) - 1 51 # 判断坐标是否在棋盘之内 52 if (x < 0 or x > 9 or y < 0 or y > 9): 53 print('\033[31m***您输入的坐标有误请重新输入!***\033[0m') 54 continue 55 # 判断坐标上是否有棋子 56 if (checkerboard[x][y] == '-'): 57 if (flagNum == 1): 58 checkerboard[x][y] = '*' 59 else: 60 checkerboard[x][y] = 'o' 61 else: 62 print('\033[31m******您输入位置已经有其他棋子,请重新输入!\033[0m') 63 continue 64 # 判断棋子左侧 65 if (y - 4 >= 0): 66 if (checkerboard[x][y - 1] == flagch 67 and checkerboard[x][y - 2] == flagch 68 and checkerboard[x][y - 3] == flagch 69 and checkerboard[x][y - 4] == flagch): 70 finish = True 71 msg() 72 73 # 判断棋子右侧 74 if (y + 4 <= 9): 75 if (checkerboard[x][y + 1] == flagch 76 and checkerboard[x][y + 2] == flagch 77 and checkerboard[x][y + 3] == flagch 78 and checkerboard[x][y + 4] == flagch): 79 finish = True 80 msg() 81 82 # 判断棋子上方 83 if (x - 4 >= 0): 84 if (checkerboard[x - 1][y] == flagch 85 and checkerboard[x - 2][y] == flagch 86 and checkerboard[x - 3][y] == flagch 87 and checkerboard[x - 4][y] == flagch): 88 finish = True 89 msg() 90 91 # 判断棋子下方 92 if (x + 4 <= 9): 93 if (checkerboard[x + 1][y] == flagch 94 and checkerboard[x + 2][y] == flagch 95 and checkerboard[x + 3][y] == flagch 96 and checkerboard[x + 4][y] == flagch): 97 finish = True 98 msg() 99 100 # 判断棋子右上方向 101 if (x - 4 >= 0 and y - 4 >= 0): 102 if (checkerboard[x - 1][y - 1] == flagch 103 and checkerboard[x - 2][y - 2] == flagch 104 and checkerboard[x - 3][y - 3] == flagch 105 and checkerboard[x - 4][y - 4] == flagch): 106 finish = True 107 msg() 108 109 # 判断棋子右下方向 110 if (x + 4 <= 9 and y - 4 >= 0): 111 if (checkerboard[x + 1][y - 1] == flagch 112 and checkerboard[x + 2][y - 2] == flagch 113 and checkerboard[x + 3][y - 3] == flagch 114 and checkerboard[x + 4][y - 4] == flagch): 115 finish = True 116 msg() 117 118 # 判断棋子左上方向 119 if (x - 4 >= 0 and y + 4 <= 9): 120 if (checkerboard[x - 1][y + 1] == flagch 121 and checkerboard[x - 2][y + 2] == flagch 122 and checkerboard[x - 3][y + 3] == flagch 123 and checkerboard[x - 4][y + 4] == flagch): 124 finish = True 125 msg() 126 127 # 判断棋子左下方向 128 if (x + 4 <= 9 and y + 4 <= 9): 129 if (checkerboard[x + 1][y + 1] == flagch 130 and checkerboard[x + 2][y + 2] == flagch 131 and checkerboard[x + 3][y + 3] == flagch 132 and checkerboard[x + 4][y + 4] == flagch): 133 finish = True 134 msg() 135 flagNum *= -1; # 更换下棋者标记