4 Replies - 1104 Views - Last Post: 20 November 2012 - 05:10 AM Rate Topic: -----

#1 art1210  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 15-November 12

NIM game - problem with re-runing the game

Posted 15 November 2012 - 02:00 PM

Hello,
i have to do a small game called NIM. The game is a human vs. computer game where each player removes a number of straws (1,2 or 3) and the player who removes the last straw looses. I got the game to work properly but the problem is that it doesn't want to re-run if the player wants to play again. Any help would be apprecieted. :)/>

import random
print("""************ NIM GAME   ***********
************ Game Start ***********
************ The rules  ***********
-----------------------------------------------------
You need to remove from 1 to 3 straws from the pile.
The player that removes the final straw is the loser.
-----------------------------------------------------""")

player1=str(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():
    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() 
 
        

        








Is This A Good Question/Topic? 1
  • +

Replies To: NIM game - problem with re-runing the game

#2 Zoquo  Icon User is offline

  • New D.I.C Head

Reputation: 20
  • View blog
  • Posts: 45
  • Joined: 16-May 12

Re: NIM game - problem with re-runing the game

Posted 15 November 2012 - 05:27 PM

The problem is in the playAgain method. It is getting to the while loop and screwing up. I would suggest taking the playAgain method out completely and putting it around the call to the game method.
Was This Post Helpful? 0
  • +
  • -

#3 art1210  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 15-November 12

Re: NIM game - problem with re-runing the game

Posted 15 November 2012 - 05:56 PM

Thanks for the reply :) I've changed a few lines and it's working now.
Was This Post Helpful? 0
  • +
  • -

#4 mukc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 20-November 12

Re: NIM game - problem with re-runing the game

Posted 20 November 2012 - 04:53 AM

View PostZoquo, on 15 November 2012 - 05:27 PM, said:

The problem is in the playAgain method. It is getting to the while loop and screwing up. I would suggest taking the playAgain method out completely and putting it around the call to the game method.


View PostZoquo, on 15 November 2012 - 05:27 PM, said:

The problem is in the playAgain method. It is getting to the while loop and screwing up. I would suggest taking the playAgain method out completely and putting it around the call to the game method.


hello,
i just saw the nim game and i was asking if you could please help mi design the program. i just want to get a clear explanation of it.
thank you
Was This Post Helpful? 0
  • +
  • -

#5 mukc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 20-November 12

Re: NIM game - problem with re-runing the game

Posted 20 November 2012 - 05:10 AM

View PostZoquo, on 15 November 2012 - 05:27 PM, said:

The problem is in the playAgain method. It is getting to the while loop and screwing up. I would suggest taking the playAgain method out completely and putting it around the call to the game method.


hey,
how does some one explain the design of this program?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1