Question

Your task in to design a game of Nim in Python. In this game “Two players take turns removing objects from distinct heaps or piles. On each turn, a player must remove at least one object, and may remo...

Your task in to design a game of Nim in Python. In this game “Two players take turns removing objects from distinct heaps or piles. On each turn, a player must remove at least one object, and may remove any number of objects provided they all come from the same heap/pile. The goal of the game is to avoid taking the last object.” (Wikipedia)

In your implementation of the game, there will be 3 piles of objects. At the start of the game, each of the three piles will be initialized by a random number of objects. Random number should be between 5 and 15. The players will take turn picking up objects from the piles. A player can even pick up all items from a single pile. The player that picks up last object (across all piles) loses.

You should use functions in the program. Functions must follow a good programming style, that is, any well-defined task which needs to be executed often, can be moved into its own function.

=====================PLEASE PROVIDE A CLEAR FLOWCHART FOR THE PROGRAM.=============================

SAMPLE OF THE PROGRAM ==================

===== Welcome to Nim Game =====

Here are the three piles
A: *******
B: *************
C: *********
(7, 13, 9)

Player 1, choose your pile (A, B, C): C
Choose how many objects to remove: 5
OK

A: *******
B: *************
C: ****
(7, 13, 4)

Player 2, choose your pile (A, B, C): krypton
ERR: Sorry, that is not a valid pile.
Player 2, choose your pile (A, B, C): A
Choose how many objects to remove: 7
OK
A:
B: *************
C: ****
(0, 13, 4)

Player 1, choose your pile (A, B, C): B
Choose how many objects to remove: alpha
ERR: Sorry, that is not a valid number.
Choose how many objects to remove: 10
OK

A:
B: ***
C: ****
(0, 3, 4)

Player 2, choose your pile (A, B, C): C
Choose how many objects to remove: 8
ERR: Sorry, there is not enough objects to remove in this pile.
Choose how many objects to remove: 4
OK

A:
B: ***
C:
(0, 3, 0)

Player 1, choose your pile (A, B, C): C
ERR: Sorry, this pile is already empty.
Player 1, choose your pile (A, B, C): B
Choose how many objects to remove: 1
OK

A:
B: **
C:
(0, 2, 0)

Player 2, choose your pile (A, B, C): B
Choose how many objects to remove: 2
OK

A:
B:
C:
(0, 0, 0)

Player 2 picked up the last object. <<< Player 1 >>> is the WINNER.

Thanks for playing. Goodbye.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:

#for generating random number import random #method for printing the pile object def printobjects (objectA, objectB, objectc)

#keep running the loop till player does not select valid no of piles to be removed while (check 0) number=input (Choose how

def main) #initialize object with random number between 5 and 15 objectA-random.randint (5,15) obj ectB-random. randint ( 5 ,

Code:

#for generating random number
import random

#method for printing the pile objects
def printObjects(objectA,objectB,objectC):
    print("A: ",end="")

    for a in range(0,objectA):
        print("*",end="")

    print("\nB: ",end="")
    for b in range(0,objectB):
        print("*",end="")

    print("\nC: ",end="")
    for c in range(0,objectC):
        print("*",end="")

    print("\n(",objectA,",",objectB,",",objectC,")\n")

#method for taking input from user      
def takeInput(player,objectA,objectB,objectC):
    pile=""
    check=0
    #keep running the loop till player does not enter either A or B or C
    while(pile!='A' and pile!='B' and pile!='C'):
        pile=input("{} ,choose your pile(A,B,C): ".format(player))
        if(pile!='A' and pile!='B' and pile!='C'):
            print("ERR: Sorry that is not a valid pile.")
        elif(pile=='A' and objectA==0):
            print("Sorry, this pile is already empty.")
            pile=""
        elif(pile=='B' and objectB==0):
            print("Sorry, this pile is already empty.")
            pile=""
        elif(pile=='C' and objectC==0):
            print("Sorry, this pile is already empty.")
            pile=""

    #keep running the loop till player does not select valid no of piles to be removed
    while(check==0):
        number=input("Choose how many object to remove:")
        if(not number.isdigit() or int(number)<=0):
             print("ERR: Sorry, that is not a valid number.")
        elif(pile=='A' and int(number)>objectA):
             print("ERR: Sorry, there is not enough objects to remove in this pile.")
        elif(pile=='B' and int(number)>objectB):
             print("ERR: Sorry, there is not enough objects to remove in this pile.")
        elif(pile=='C' and int(number)>objectC):
             print("ERR: Sorry, there is not enough objects to remove in this pile.")
      
        else:
            #remove the piles from the object selected by the player
            if(pile=='A'):
                objectA=objectA-int(number)
                break
            elif(pile=='B'):
                objectB=objectB-int(number)
              
            else:
                objectC=objectC-int(number)

            check=1
    print("OK\n")
    #reurn the updated objects value
    return objectA,objectB,objectC
      
  
def main():
    #initialize object with random number between 5 and 15
    objectA=random.randint(5,15)
    objectB=random.randint(5,15)
    objectC=random.randint(5,15)

    print("Welcome to Nim Game\n")

    #keep running the while loop till any of objects have pile in them
    while(objectA!=0 or objectB!=0 or objectC!=0):  
        #player 1 turn
        turn=1
        #display the objects by calling method
        printObjects(objectA,objectB,objectC)
        #take input from player1
        (objectA,objectB,objectC)=takeInput("Player 1",objectA,objectB,objectC)

        #if after removing piles there are objects with piles in them
        #give turn to playaer 2
        if(objectA!=0 or objectB!=0 or objectC!=0):
            turn=2
            printObjects(objectA,objectB,objectC)  
            (objectA,objectB,objectC)=takeInput("Player 2",objectA,objectB,objectC)

    #display the objects   
    printObjects(objectA,objectB,objectC)
    #if last turn was of player1
    if(turn==1):
        print("Player 1 picked up the last object. <<<Player 2>>> is the winner.")

    else:
        print("Player 2 picked up the last object. <<<Player 1>>> is the winner.")

    print("\nThanks for playing. Goodbye.")


if __name__=='__main__':
    main()

Output:

Welcome to Nim Game 8,5,14) Player 1,choose your pile (A, B, C): A Choose how many object to remove : 4 OK (4,5,14) Player 2

B: Player 2 , choose your pile (A, B, C) : C Choose how many object to remove:1 OK B: C: Player 2 picked up the last object.

Flowchart:

START LB ai es Wem ex to Tes

Add a comment
Know the answer?
Add Answer to:
Your task in to design a game of Nim in Python. In this game “Two players take turns removing objects from distinct heaps or piles. On each turn, a player must remove at least one object, and may remo...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Task 1 Draw a flowchart that presents the steps of the algorithm required to perform the task spe...

    Task 1 Draw a flowchart that presents the steps of the algorithm required to perform the task specified. You can draw the flowcharts with a pen/pencil on a piece of paper and scan it for submission. Please ensure that the scanned file and your handwriting are clear and legible. However, it is preferable to draw flowcharts using a drawing software. Here are links to some free drawing tools https://www.draw.io/ www.lucidchart.com http://dia-installer.de/ https://pencil.evolus.vn/ ---------------------------------DON'T NEED THE PYTHON CODE... JUST THE ALGORITHM...

  • Task 1 Draw a flowchart that presents the steps of the algorithm required to perform the...

    Task 1 Draw a flowchart that presents the steps of the algorithm required to perform the task specified. You can draw the flowcharts with a pen/pencil on a piece of paper and scan it for submission. Please ensure that the scanned file and your handwriting are clear and legible. However, it is preferable to draw flowcharts using a drawing software. Here are links to some free drawing tools https://www.draw.io/ www.lucidchart.com http://dia-installer.de/ https://pencil.evolus.vn/ ---------------------------------DON'T NEED THE PYTHON CODE... JUST THE ALGORITHM...

  • 1. NIM game is a game with multiple piles of match sticks with the two players...

    1. NIM game is a game with multiple piles of match sticks with the two players taking turns to remove 1 pile or part of 1 pile. The person getting the last stick wins. In the class I had shown how (1, 1) and (2, 2) are losing configurations (i.e. the person facing two piles of 2 and 2 sticks will lose). a) Prove that (3, 4) is a winning configuration. Show how the 1s player can win if she...

  • 1. NIM game. This is a different version or easier version of NIM game Consider a pile of 5 matchsticks. Two people take turns removing 1 or 2 sticks each time from this pile. Suppose both players pl...

    1. NIM game. This is a different version or easier version of NIM game Consider a pile of 5 matchsticks. Two people take turns removing 1 or 2 sticks each time from this pile. Suppose both players play smartly (nobody plays a fool move trying to let the opponent wins. But there is only one winner anyway) a)If the person getting the last stick wins, will the first player win? Why? Show the steps the first and second player will...

  • Developing an optimal strategy for a variant of the game Nim Nim is a subtraction game...

    Developing an optimal strategy for a variant of the game Nim Nim is a subtraction game that is played with sticks. The subtraction game variant is simple. A pile of sticks is placed in front of a pair of participants. The players take turns removing either 1, 2, 3, or 4 sticks from the pile. The player who removes that last stick from the pile loses the game. It turns out that there is an optimal strategy for playing this...

  • Answer the following Nim game style questions. (Robert's Game) In this game, two players take turns removing stones from a pile that begins with n stones. The player who takes the last stone w...

    Answer the following Nim game style questions. (Robert's Game) In this game, two players take turns removing stones from a pile that begins with n stones. The player who takes the last stone wins. A player removes either one stone or p stones, where p is a prime dividing the number of stones in the pile at the start of the turn For which n does the First Player have a winning strategy? A winning strategy for the First Player...

  • Consider a game in which two players, Fred and Barney, take turns removing matchsticks from a pile. They start with 21 m...

    Consider a game in which two players, Fred and Barney, take turns removing matchsticks from a pile. They start with 21 matchsticks, and Fred goes first. On each turn, each player may remove either one, two, or three matchsticks. The player to remove the last matchstick wins the game. (a) Suppose there are only 5 matchsticks left, and it is Fred’s turn. What move should Fred make to guarantee himself victory? Explain your reasoning. (b) Suppose there are 10 matchsticks...

  • A subtraction game Subtraction games are two-player games in which there is a pile of objects,...

    A subtraction game Subtraction games are two-player games in which there is a pile of objects, say coins. There are two players, Alice and Bob, who alternate turns subtracting 4.9. A SUBTRACTION GAME 19 from the pile some number of coins belonging to a set S (the subtraction set). Alice goes first. The first player who is unable to make a legal move loses. For example, suppose the initial pile contains 5 coins, and each player can, on his turn,...

  • Need Help with homework problem writing the game of nim in Python IDLE. Its a well...

    Need Help with homework problem writing the game of nim in Python IDLE. Its a well known game with a number of variants. The following variant has an interesting winning strategy. Two players alternately take marbles from a pile. In each move, a player chooses how many marbles to take. The player must take at least one but at most half of the marbles. Then the other player takes a turn. The player who takes the last marble loses. Instructions...

  • Please show ALL STEPS, NEAT HANDWRITNG ONLY and answer ALL PARTS please :) 1. Consider the...

    Please show ALL STEPS, NEAT HANDWRITNG ONLY and answer ALL PARTS please :) 1. Consider the following game: suppose there are three piles of stones starting with 3, 5, and 7 in each pile. Two players take turn playing this game. During their turn, a player can choose one of the three piles that are currently nonempty and remove any positive number of stonescurrently presented in that pile. Whoever takes the last stone loses the game. (a) Describe how to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT