Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a python program that will play the game of Nim. The Game of Nim is a well

ID: 3673644 • Letter: W

Question

Write a python program that will play the game of Nim. The Game of Nim is a well-known game with a number of variants. One particularly interesting version plays like this: "Assume you have a large pile of marbles. Two players alternate taking marbles from the pile. In each move, a player is allowed to take between 1 and half of the total marbles. So, for instance, if there are 64 marbles in the pile, any number between and including 1 and 32 is a legal move. Whichever player takes the last marble loses."

Write a program called Nim in which a human player plays against the computer. The program should first ask for how many marbles are in the starting pile. Then the program should ask for a character for who will start the game (‘p’ for player, ‘c’ for computer). The game will then alternate between the computer and the player, asking the player for how many marbles they want to take (making sure they take a positive integer between 1 and half the pile and making them pick another number if they do it wrong). The computer should always take enough marbles to make the size of the pile a power of 2 minus 1 - such as 3, 7, 15, 31, 63, etc. If that’s not possible, the computer will take one marble. You should find that the computer, if it gets to go first, will always win. Similarly, if you go first and know the strategy, you’ll always win!

An example run might be:

The Game of Nim Number of marbles are in the pile: 100 (this number should be inputted by the user, just like the others and not random)

Who will start? (p or c): c

The pile has 100 marbles in it.

The computer takes 37 marbles.

The pile has 63 marbles in it.

How many marbles do you want to take? (1-31): 4

The pile has 59 marbles in it.

The computer takes 28 marbles.

The pile has 31 marbles in it.

How many marbles do you want to take? (1-15): 13

The pile has 18 marbles in it.

The computer takes 3 marbles.

The pile has 15 marbles in it.

How many marbles do you want to take? (1-7): 6

The pile has 9 marbles in it.

The computer takes 2 marbles.

The pile has 7 marbles in it.

How many marbles do you want to take? (1-3): 2

The pile has 5 marbles in it.

The computer takes 2 marbles.

The pile has 3 marbles in it.

How many marbles do you want to take? (1-1): 1

The pile has 2 marbles in it.

The computer takes 1 marbles.

The pile has 1 marbles in it.

How many marbles do you want to take? (1-1): 1

The computer wins!

Another run might be:

The Game of Nim Number of marbles are in the pile: 64

Who will start? (p or c): p

The pile has 64 marbles in it.

How many marbles do you want to take? (1-32): 1

The pile has 63 marbles in it.

The computer takes 1 marbles.

The pile has 62 marbles in it.

How many marbles do you want to take? (1-31): 45

How many marbles do you want to take? (1-31): 90

How many marbles do you want to take? (1-31): 31

The pile has 31 marbles in it.

The computer takes 1 marbles.

The pile has 30 marbles in it.

How many marbles do you want to take? (1-15): 15

The pile has 15 marbles in it.

The computer takes 1 marbles.

The pile has 14 marbles in it.

How many marbles do you want to take? (1-7): 7

The pile has 7 marbles in it.

The computer takes 1 marbles.

The pile has 6 marbles in it.

How many marbles do you want to take? (1-3): 3

The pile has 3 marbles in it.

The computer takes 1 marbles.

The pile has 2 marbles in it.

How many marbles do you want to take? (1-1): 1

The pile has 1 marbles in it.

The computer takes 1 marbles.

The player wins!

Explanation / Answer

import random
print(@@@@@@@@@@@@@@@@@2 NIM GAME@@@@@@@@@@@@@@)
player1=str(input("Please 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():
lobal 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()

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote