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.
Solutions
Expert Solution
Code:
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:
Flowchart: