2014-05-11 108 views
0

我在python中相當新,所以在這裏裸露在這裏。我正在製作一個遊戲,在這個遊戲中用戶輸入「x」數量的籌碼放入樁1和樁2中。 (例如,用戶輸入:12;堆1:12,堆2:12),然後從第一堆或第二堆中取出「z」量的籌碼,然後在計算機之後從對面拿走相同數量的籌碼用戶拿走的那一堆。所以基本上電腦總是贏得勝利。但我遇到了一些麻煩更新樁,直到兩個樁達到0.1如何在用戶在python中更改變量後繼續更新變量?

def initGame(): 
    pile1= chips 
    pile2= chips 
    finished = False 
    while chips<=0: 
     if chips <= 0: 
      print("Please input a number greater than 0") 
     finished= True 
    else: 
     finished= True 
    return pile1, pile2 

def displayPiles(pile1, pile2): 
    print("It is your turn human.") 
    print("Here are the piles: ") 
    print("pile 1: "+ str(pile1)) 
    print("pile 2: "+ str(pile2)) 

    return pile1, pile2 

def getHumanMove(x,y): 
    finished = False 
    while not finished: 
     x=int(input("Which pile would you like to take from?(1 or 2)")) 
     y=int(input("How many would you like from pile "+ str(x)+ "? ")) 
     if pile1 and pile2<y: 
      print("pile " +str(x)+ " does not have that many chips. Try again.") 
     elif y==0: 
      print("You must take at least one chip. Try again.") 
     else: 
      print("That was a legal move. Thank You.") 
     finished = True 
    return x,y 

def getPiles(pile1, pile2, move): 
    print("Here are the piles: ") 
    x,y = move 
    if x == 1: 
     pile1= pile1- y 
     print("pile 1: ", str(pile1)) 
     print("pile 2: ", str(pile2)) 
    elif x == 2: 
     pile2= pile1- y 
     print("pile 1: ", str(pile1)) 
     print("pile 2: ", str(pile2)) 
     return pile1, pile2 

def getCompMove(x,y, pile1, pile2): 
    print("Now it's my turn.") 
    pile1, pile2= pile1, pile2 
    x= x 
    y= y 
    if x==1: 
     print("I, the champion chips computer will take "+str(y)+ " chips from pile 2") 
     pile2= pile2 - y 
     pile1= pile1 
    elif x==2: 
     print("I, the champion chips computer will take "+str(y)+ " chips from pile 1") 
     pile1= pile1 - y 
     pile2= pile2 
    if pile1==0 and pile2==0: 
     print("The game is over because I took the last chip.") 
     print("Thanks for playing. Let's wager next time.")   
    return x,y, pile1, pile2 

def compPiles(pile1, pile2): 
    print("Here are the piles: ") 
    pile1, pile2= pile1, pile2 
    print("pile 1: ", str(pile1)) 
    print("pile 2: ", str(pile2)) 
    return pile1, pile2 

主要

chips = int(input("How many chips would you like to start with? ")) 

pile1, pile2= initGame() 

display = displayPiles(pile1, pile2) 

move= getHumanMove(pile1, pile2) 

pile= getPiles(pile1, pile2, move) 

x,y = move 

_,_,pile1, pile2 = getCompMove(x,y, pile1, pile2) 

pile1, pile2 =pile1, pile2 

compi= compPiles(pile1, pile2) 

當我運行它,它正確地帶走芯片要刪除的號碼,但隨後它不當計算機移動時不更新它。

Here are the piles: 
pile 1: 9 
pile 2: 12 
Now it's my turn. 
I, the champion chips computer will take 3 chips from pile 2 
Here are the piles: 
pile 1: 12 
pile 2: 9 

很抱歉的長碼:/

+0

最簡單的方法是使用列表或字典。 – OneOfOne

+0

你爲什麼要在你的函數中使用'pile1 = pile1'? – Lafexlos

+0

@lafexlos只是一個錯誤。剛摘下它。 – TheAznHawk

回答

0

有用於更新的損失並不奇怪:你把getCompMove的結果變量pile後來從來沒有使用它。我認爲,如果你添加

pile1, pile2 = pile 

pile1, pile2 = getCompMove(...) 

它會表現得更好。順便說一下,當你學習Python時,你應該嘗試將所有你的單獨函數轉換成一個類中的方法,該方法包含兩個堆:它會更清晰,並且你將有更少的變量傳入和傳出。除非在註釋中聲明,否則系統地隱藏全局變量與局部同名是對讀者的痛苦。