Thread: The game of Nim

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    4
    Rep Power
    0

    The game of Nim


    Hello,

    I am trying to modify a game of nim code. The game in the code works as:
    "You need to remove from 1 to 3 straws from the pile.
    The player that removes the final straw is the loser."

    But I would like it to be like the classic game, where you have 4 piles containing 1,3,5 and 7 sticks
    where the drawer of the last stick is the winner. It would be awsome if I could print how
    many sticks there are left in each pile, e.g. : I III IIIII IIIIIII.

    Thanks in advance!

    The code I have:

    Code:
    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)
    game()
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,997
    Rep Power
    481
    Once you've solved this, work on this (linked here) problem.
    Code:
    import random
    
    player1=str(input("Enter your name. "))
    player2="Computer"
    howMany=0
    gameover=False
    ################ here the program sets up the piles
    ################ you would need to make 4 piles
    straws = [1,3,5,7]  ################ like this perhaps, in place of strawsNumber
    
    strawsNumber=random.randint(10,20)
    
    
    if (strawsNumber%4)==1:
        strawsNumber+=1
    
    def removingStrawsComputer():
        ################ choose a pile with more than 0 straws
        ################ and choose a number of straws to extract from that pile
        removedNumber=random.randint(1,3)
        global strawsNumber
        while removedNumber>strawsNumber:
            removedNumber=random.randint(1,3)
        strawsNumber-=removedNumber
        return strawsNumber
    
    def removingStrawsHuman(pile, count):
        ################ rewritten.  This function is silly.
        ################ Note that I passed (some of) the information via parameters instead of global
        global straws
        straws[pile] -= count
    
    def humanLegalMove():
        ################ get a pile number with straws
        ################ get a valid number 
    
        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):
        ################ check each pile, an easy way is  gameover = not sum(straws)
        if strawsNumber==0:
            print(player," wins.")
            global gameover
            gameover=True
            return gameover
    
    def resetGameover():
        ################ reset should initialize the piles and you should call it before each game instead of after each game
        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)
    game()
    Last edited by b49P23TIvg; March 31st, 2013 at 03:44 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    4
    Rep Power
    0
    Thanks for your quick reply, I will try to work on the hints you gave me. I appreciate it!
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    4
    Rep Power
    0
    I'm sorry I don't see how I should define the function that picks a pile and removes straws from that pile?

    Please advice
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    4
    Rep Power
    0
    I'm sorry I don't see how I should define the function that picks a pile and removes straws from that pile? Should it be something like:

    removedNumber=random.randint[1,3,5,7]

    Please advice
  10. #6
  11. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,997
    Rep Power
    481
    Code:
    def get_valid_piles(piles):
        '''
            Given the piles (a list of integers)
            return a list of indexes of non-empty piles
        '''
        return [i for (i,pile,) in enumerate(piles) if pile] # list comprehension
    
        ## "traditional code"
        #return_value = list()
        #for i in range(len(piles)):
        #    if 0 < piles[i]:
        #        return_value.append(i)
        #return return_value
    
    def automation_choose_straw(piles): # stupid variant
        '''
            Given the piles (a list of integers)
            returns the pile index and number of straws to remove
        '''
        pile_index = random.choice(get_valid_piles(piles))
        maximum = min(piles[pile_index], 3)
        take_this_many_straws = random.randint(1, maximum)
        return (pile_index, take_this_many_straws)


    Please, where do you come up with this sort of statement?
    Should it be something like...
    Code:
    >>> import random
    >>> random.randint[1,3,5,7] 
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: 'instancemethod' object has no attribute '__getitem__'
    >>>
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo