首頁  >  文章  >  後端開發  >  用 Python 做個簡單的井字遊戲

用 Python 做個簡單的井字遊戲

巴扎黑
巴扎黑原創
2017-04-05 14:53:581662瀏覽

  在這個教程中,我將展示如何利用Python來做一個井字遊戲。這將包括函數、列表、if語句、while循環、for循環以及錯誤處理等等。

  首先,我們將建立兩個函數,第一個函數將會列印出井字遊戲的背景模板:

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循環,要遍歷一個名為map的列表變數。這個變數是一個二維列表,將保存每個位置的資訊。

由於我會按照小鍵盤的數字來進行對照位置(稍後你會看到),所以第一個值我們把它設為(2-i),然後我們想用 "|"來分割我們的位置,所以在每個位置列印完之後,我們給他印一個"|",我們在這裡print map[2-i] [j],使用了逗號,以保證他們在同一行被印出來。

  現在,這個函數可以列印一個遊戲的背景啦,它看起來是這個樣子滴:

  |   |   
  |   |   
  |   |
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

首先,我們會檢查水平和垂直方向,是否有三格是相同、並且不為空(所以他不會認為連續三個空行是符合條件的),其次,我們以相同的方式來檢查對角線。

這8行如果有一行符合條件,那麼遊戲結束並且印出「Won!!!」並返回True,同時注意turn這個變量,它的作用是判斷現在下棋的是那一方,最終展現出來的消息將會是「X贏了!!」或「O贏了!!」。

  接下來這個函數會判斷假如沒有一個位置是空的,那麼就意味著沒有人能夠贏得比賽(前面判斷過了),那麼就打印出平局,並且返回True

  如果沒有上述兩種情況,那麼遊戲還沒結束,回傳False

  OK,現在我們有了兩個函數,接下來開始我們真正的程序,首先來創建三個變數:

turn = "X"
map = [[" "," "," "],
       [" "," "," "],
       [" "," "," "]]
done = False

  我已經告訴你這三個變數是熟麼意思了,假如你忘了的話,那麼看看下面:

  • turn:該誰走了


  • map:遊戲的背景地圖


  • # done:這個遊戲到底有木有結束

  接下來,這樣寫:

while done != True:
    print_board()

    print turn, "'s turn"
    print

    moved = False
    while moved != True:

  裡面有一個while循環,直到done為True為止,我們印出該輪到誰走了。

  接著創建一個名為moved的變量,檢查玩家是不是移動了,如果沒有移動,則進入下一個循環。

  接下來,我們列印玩家該怎麼去下:

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之間,同時,我們還得增加一個錯誤處理,比如玩家輸入”Hello”,程式不能就這麼退出了。

  現在,我們需要檢查他走的這一步能不能走:

Y = pos/3
X = pos%3
if X != 0:
    X -=1
else:
    X = 2
    Y -=1

hah,睜大眼睛啦,首先我們得到一個X和Y的值,然後用他們來檢查,他要下的那個位置是不是空的,接下來我會向你解釋X和Y他們是腫麼工作的:

  • 位置1:Y = 1/3 = 0, X = 1%3 = 1; x -= 1 = 0


  • ## 位置2:Y = 2/3 = 0, X = 2%3 = 2; X -= 1 = 1


  • ## 位置3:Y = 3/3 = 1, X = 3%3 = 0; X = 2, Y -= 1 = 0

  • ……
  • 下面的自己算啊,我直接上結論(靠,Hexo預設的範本不顯示表格啊,我在mou上面編輯的時候比下面的漂亮多了!):

Y\X# y=2# y=1# y=0#

  aha,这个位置和我们键入的是一样的!

print "7|8|9"
print "4|5|6"
print "1|2|3"

  现在我们完成大部分工作了,但是还有几行代码:

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"

  嗯,我们给moved变量复制为True,并检查是否结束了,木有结束的话变换角色换下一个人走。

  OK,差不多结束了,假如你只是想Ctrl+C 和 Ctrl+V的话,下面是全部的代码,希望你学到了点什么,( ^_^ )/~~拜拜。

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, "&#39;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"

  原文出处: Vswe

x=0 x=1 x=2
7 8 9  
4 5 6  
1 2 3  

以上是用 Python 做個簡單的井字遊戲的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn