使用Python編寫一個簡單的tic-tac-toe游戲的教程
這個教程,我們將展示如何用python創(chuàng)建一個井字游戲。 其中我們將使用函數、數組、if條件語句、while循環(huán)語句和錯誤捕獲等。
首先我們需要創(chuàng)建兩個函數,第一個函數用來顯示游戲板:
def print_board(): for i in range(0,3): for j in range(0,3): print map[2-i][j], if j != 2: print "|", print ""
這我們使用兩個for循環(huán)來遍歷map,該map是一個包含了位置信息的二維數組。
游戲板看起來是這樣的:
| | | | | | X | X | O | X | O | O | X X | X | X X | X | X X | X | X
下面我們需要一個函數check_done()來檢查游戲是否結束。如果結束,則返回True并打印消息。
def check_done(): for i in range(0,3): if map[i][0] == map[i][1] == map[i][2] != " " \ or map[0][i] == map[1][i] == map[2][i] != " ": print turn, "won!!!" return True if map[0][0] == map[1][1] == map[2][2] != " " \ or map[0][2] == map[1][1] == map[2][0] != " ": print turn, "won!!!" return True if " " not in map[0] and " " not in map[1] and " " not in map[2]: print "Draw" return True return False
有幾個地方需要檢查,首先檢查水平和垂直方向,是否有一行或一列不為空且包含有三個相同的符號,然后我們再檢查斜方向。如果上面有一個方向滿足,游戲結束并打印“Won!!!”。請注意檢查變量改變,它用來標記當前是哪一位玩家。
同時我們需要檢查當前游戲板是否被填滿且沒有人獲勝,游戲平局。
有了上面的兩個函數,下面我們創(chuàng)建3個變量:
turn = "X" map = [[" "," "," "], [" "," "," "], [" "," "," "]] done = False
turn : 輪到誰
map : 游戲板
done : 游戲是否結束
現在啟動游戲:
while done != True: print_board() print turn, "'s turn" print moved = False while moved != True:
這里使用了while循環(huán)直到游戲結束并返回true.在這個循環(huán)里面,使用了另外一個while循環(huán)來檢查玩家是否移動,如果玩家沒有移動,則程序會跳到下一次循環(huán)。
下一步告訴玩家怎么玩:
print "Please select position by typing in a number between 1 and 9, see below for which number that is which position..." print "7|8|9" print "4|5|6" print "1|2|3" print try: pos = input("Select: ") if pos <=9 and pos >=1:
我們期望玩家輸入一個數字,檢查該數字是否是在1到9之間。另外,我們這里需要一段錯誤處理邏輯,我們還需要需要檢查玩家是否能移動到一個位置:
Y = pos/3 X = pos%3 if X != 0: X -=1 else: X = 2 Y -=1
以下是全部的代碼:
def print_board(): for i in range(0,3): for j in range(0,3): print map[2-i][j], if j != 2: print "|", print "" def check_done(): for i in range(0,3): if map[i][0] == map[i][1] == map[i][2] != " " \ or map[0][i] == map[1][i] == map[2][i] != " ": print turn, "won!!!" return True if map[0][0] == map[1][1] == map[2][2] != " " \ or map[0][2] == map[1][1] == map[2][0] != " ": print turn, "won!!!" return True if " " not in map[0] and " " not in map[1] and " " not in map[2]: print "Draw" return True return False turn = "X" map = [[" "," "," "], [" "," "," "], [" "," "," "]] done = False while done != True: print_board() print turn, "'s turn" print moved = False while moved != True: print "Please select position by typing in a number between 1 and 9,\ see below for which number that is which position..." print "7|8|9" print "4|5|6" print "1|2|3" print try: pos = input("Select: ") if pos <=9 and pos >=1: Y = pos/3 X = pos%3 if X != 0: X -=1 else: X = 2 Y -=1 if map[Y][X] == " ": map[Y][X] = turn moved = True done = check_done() if done == False: if turn == "X": turn = "O" else: turn = "X" except: print "You need to add a numeric value"
相關文章
使用python搭建Django應用程序步驟及版本沖突問題解決
這篇文章主要介紹了使用python搭建Django應用程序的步驟,最近還解決了因版本沖突出現的錯誤2013-11-11Django網絡框架之創(chuàng)建虛擬開發(fā)環(huán)境操作示例
這篇文章主要介紹了Django網絡框架之創(chuàng)建虛擬開發(fā)環(huán)境操作,簡單描述了虛擬開發(fā)環(huán)境的概念、功能,并分析了使用venv與virtualenv安裝虛擬環(huán)境相關操作技巧,需要的朋友可以參考下2019-06-06使用Python的package機制如何簡化utils包設計詳解
這篇文章主要給大家介紹了關于使用Python的package機制如何簡化utils包設計的相關資料,文中通過示例代碼的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面來一起看看吧。2017-12-12python用selenium打開chrome瀏覽器保持登錄方式
大家好,本篇文章主要講的是python用selenium打開chrome瀏覽器保持登錄方式,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下2022-02-02python繞過圖片滑動驗證碼實現爬取PTA所有題目功能 附源碼
這篇文章主要介紹了python繞過圖片滑動驗證碼實現爬取PTA所有題目 附源碼,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01