您好,登錄后才能下訂單哦!
今天小編給大家分享一下python怎么實現簡易五子棋游戲控制臺版的相關知識點,內容詳細,邏輯清晰,相信大部分人都還太了解這方面的知識,所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。
運行效果:
完整代碼+注釋:
finish = False # 游戲是否結束 flagNum = 1 # 當前下棋者標記 flagch = '*' # 當前下棋者棋子 x = 0 # 當前棋子的橫坐標 y = 0 # 當前棋子的縱坐標 print('---------五子棋游戲(控制臺版)---------') # 棋盤初始化 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) - 65 y = 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] == flagch and checkerboard[x][y - 2] == flagch and checkerboard[x][y - 3] == flagch and checkerboard[x][y - 4] == flagch): finish = True msg() # 判斷棋子右側 if (y + 4 <= 9): if (checkerboard[x][y + 1] == flagch and checkerboard[x][y + 2] == flagch and checkerboard[x][y + 3] == flagch and checkerboard[x][y + 4] == flagch): finish = True msg() # 判斷棋子上方 if (x - 4 >= 0): if (checkerboard[x - 1][y] == flagch and checkerboard[x - 2][y] == flagch and checkerboard[x - 3][y] == flagch and checkerboard[x - 4][y] == flagch): finish = True msg() # 判斷棋子下方 if (x + 4 <= 9): if (checkerboard[x + 1][y] == flagch and checkerboard[x + 2][y] == flagch and checkerboard[x + 3][y] == flagch and checkerboard[x + 4][y] == flagch): finish = True msg() # 判斷棋子右上方向 if (x - 4 >= 0 and y - 4 >= 0): if (checkerboard[x - 1][y - 1] == flagch and checkerboard[x - 2][y - 2] == flagch and checkerboard[x - 3][y - 3] == flagch and checkerboard[x - 4][y - 4] == flagch): finish = True msg() # 判斷棋子右下方向 if (x + 4 <= 9 and y - 4 >= 0): if (checkerboard[x + 1][y - 1] == flagch and checkerboard[x + 2][y - 2] == flagch and checkerboard[x + 3][y - 3] == flagch and checkerboard[x + 4][y - 4] == flagch): finish = True msg() # 判斷棋子左上方向 if (x - 4 >= 0 and y + 4 <= 9): if (checkerboard[x - 1][y + 1] == flagch and checkerboard[x - 2][y + 2] == flagch and checkerboard[x - 3][y + 3] == flagch and checkerboard[x - 4][y + 4] == flagch): finish = True msg() # 判斷棋子左下方向 if (x + 4 <= 9 and y + 4 <= 9): if (checkerboard[x + 1][y + 1] == flagch and checkerboard[x + 2][y + 2] == flagch and checkerboard[x + 3][y + 3] == flagch and checkerboard[x + 4][y + 4] == flagch): finish = True msg() flagNum *= -1; # 更換下棋者標記
以上就是“python怎么實現簡易五子棋游戲控制臺版”這篇文章的所有內容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會為大家更新不同的知識,如果還想學習更多的知識,請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。