NIM游戏无法正常运行

时间:2014-05-23 19:42:22

标签: python python-2.7

我是使用python编程的新手,但设法编写小型NIM游戏。游戏是人类与计算机,每个玩家都会移除一些吸管(从1到3)。我有一个问题是,在玩的时候由于某种原因,电脑没有移除任何吸管,转了之后吸管的数量仍然是一样的,毕竟吸管被移除后,也没有显示祝贺消息和消息发挥再次..非常感谢帮助..

player1=raw_input("Enter your name. ")
player2="Computer"
howMany=0
gameover=False
strawsNumber=random.randint(10,20)

if (strawsNumber%4)==1:
    strawsNumber+=1

def removingStrawsComputer():
    removedNumber=random.randint(1,3)
    global strawsNumber
    while removedNumber>strawsNumber:
         removedNumber=random.randint(1,3)
         strawsNumber-=removedNumber
    return strawsNumber

def removingStrawsHuman():
    global strawsNumber
    strawsNumber-=howMany
    return strawsNumber

def humanLegalMove():
    global howMany
    legalMove=False
    while not legalMove:
        print("It's your turn, ",player1)
        howMany=int(input("How many straws do you want to remove?(from 1 to 3) "))
        if  howMany>3 or howMany<1:
            print("Enter a number between 1 and 3.")
        else:
            legalMove=True
    while howMany>strawsNumber:
        print("The entered number is greater than a number of straws remained.")
        howMany=int(input("How many straws do you want to remove?"))
    return howMany

def checkWinner(player):
    if strawsNumber==0:
        print(player," wins.")
        global gameover
        gameover=True
        return gameover

def resetGameover():
    global gameover
    gameover=False
    return gameover

def game():
    global strawsNumber
    strawsNumber=random.randint(10,20)
    while gameover==False:
        print("It's ",player2,"turn. The number of straws left: ",removingStrawsComputer())
        checkWinner(player1)
        if gameover==True:
            break
        humanLegalMove()        
        print("The number of straws left: ",removingStrawsHuman())
        checkWinner(player2)


def playAgain():
    answer=input("Do you want to play again?(y/n)")
    resetGameover()
    while answer=="y":
        game()
    else:
        print("Thanks for playing the game")

game()
playAgain()

1 个答案:

答案 0 :(得分:0)

你只需修复你的缩进

def removingStrawsComputer():
    removedNumber=random.randint(1,3)
    global strawsNumber
    while removedNumber>strawsNumber:
         removedNumber=random.randint(1,3) 
         strawsNumber-=removedNumber
    return strawsNumber

只会从strawNumber if removedNumber > strawsNumber

中删除吸管

修复它来纠正你的缩进

def removingStrawsComputer():
    removedNumber=random.randint(1,3)
    global strawsNumber
    while removedNumber>strawsNumber:
         removedNumber=random.randint(1,3)
    strawsNumber-=removedNumber #now always remove the straws
    return strawsNumber