使用Python編寫一個(gè)簡(jiǎn)單的tic-tac-toe游戲的教程
這個(gè)教程,我們將展示如何用python創(chuàng)建一個(gè)井字游戲。 其中我們將使用函數(shù)、數(shù)組、if條件語(yǔ)句、while循環(huán)語(yǔ)句和錯(cuò)誤捕獲等。
首先我們需要?jiǎng)?chuàng)建兩個(gè)函數(shù),第一個(gè)函數(shù)用來(lái)顯示游戲板:
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 ""
這我們使用兩個(gè)for循環(huán)來(lái)遍歷map,該map是一個(gè)包含了位置信息的二維數(shù)組。
游戲板看起來(lái)是這樣的:
| | | | | | X | X | O | X | O | O | X X | X | X X | X | X X | X | X
下面我們需要一個(gè)函數(shù)check_done()來(lái)檢查游戲是否結(jié)束。如果結(jié)束,則返回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
有幾個(gè)地方需要檢查,首先檢查水平和垂直方向,是否有一行或一列不為空且包含有三個(gè)相同的符號(hào),然后我們?cè)贆z查斜方向。如果上面有一個(gè)方向滿足,游戲結(jié)束并打印“Won!!!”。請(qǐng)注意檢查變量改變,它用來(lái)標(biāo)記當(dāng)前是哪一位玩家。
同時(shí)我們需要檢查當(dāng)前游戲板是否被填滿且沒(méi)有人獲勝,游戲平局。
有了上面的兩個(gè)函數(shù),下面我們創(chuàng)建3個(gè)變量:
turn = "X"
map = [[" "," "," "],
[" "," "," "],
[" "," "," "]]
done = False
turn : 輪到誰(shuí)
map : 游戲板
done : 游戲是否結(jié)束
現(xiàn)在啟動(dòng)游戲:
while done != True: print_board() print turn, "'s turn" print moved = False while moved != True:
這里使用了while循環(huán)直到游戲結(jié)束并返回true.在這個(gè)循環(huán)里面,使用了另外一個(gè)while循環(huán)來(lái)檢查玩家是否移動(dòng),如果玩家沒(méi)有移動(dòng),則程序會(huì)跳到下一次循環(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:
我們期望玩家輸入一個(gè)數(shù)字,檢查該數(shù)字是否是在1到9之間。另外,我們這里需要一段錯(cuò)誤處理邏輯,我們還需要需要檢查玩家是否能移動(dòng)到一個(gè)位置:
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"
相關(guān)文章
python使用if語(yǔ)句實(shí)現(xiàn)一個(gè)猜拳游戲詳解
這篇文章主要介紹了python使用if語(yǔ)句實(shí)現(xiàn)一個(gè)猜拳游戲詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08
Python處理字符串的常用函數(shù)實(shí)例總結(jié)
在數(shù)據(jù)分析中,特別是文本分析中,字符處理需要耗費(fèi)極大的精力,因而了解字符處理對(duì)于數(shù)據(jù)分析而言,也是一項(xiàng)很重要的能力,這篇文章主要給大家介紹了關(guān)于Python處理字符串的常用函數(shù),需要的朋友可以參考下2021-11-11
Python圖片轉(zhuǎn)換成矩陣,矩陣數(shù)據(jù)轉(zhuǎn)換成圖片的實(shí)例
今天小編就為大家分享一篇Python圖片轉(zhuǎn)換成矩陣,矩陣數(shù)據(jù)轉(zhuǎn)換成圖片的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
python軟件測(cè)試Jmeter性能測(cè)試JDBC Request(結(jié)合數(shù)據(jù)庫(kù))的使用詳解
這篇文章主要介紹了python軟件測(cè)試Jmeter性能測(cè)試JDBC Request(結(jié)合數(shù)據(jù)庫(kù))的使用詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
python?import?引用上上上級(jí)包的三種方法
這篇文章主要介紹了python?import?引用上上上級(jí)包的三種方法包的三種方法,需要的朋友可以參考下2023-02-02
numpy創(chuàng)建神經(jīng)網(wǎng)絡(luò)框架
本文介紹了使用numpy從零搭建了一個(gè)類似于pytorch的深度學(xué)習(xí)框架,可以用在很多地方,有需要的朋友可以自行參考一下2021-08-08

