使用Python編寫一個簡單的tic-tac-toe遊戲的教程

來源:互聯網
上載者:User
這個教程,我們將展示如何用python建立一個井字遊戲。 其中我們將使用函數、數組、if條件陳述式、while迴圈語句和錯誤捕獲等。

首先我們需要建立兩個函數,第一個函數用來顯示遊戲台:

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,該map是一個包含了位置資訊的二維數組。

遊戲台看起來是這樣的:

|  | |  | |  | X | X | O | X | O | O | X X | X | XX | X | XX | 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!!!”。請注意檢查變數改變,它用來標記當前是哪一位玩家。

同時我們需要檢查當前遊戲台是否被填滿且沒有人獲勝,遊戲平局。

有了上面的兩個函數,下面我們建立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迴圈直到遊戲結束並返回true.在這個迴圈裡面,使用了另外一個while迴圈來檢查玩家是否移動,如果玩家沒有移動,則程式會跳到下一次迴圈。

下一步告訴玩家怎麼玩:

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"
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.