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: 3672129 • 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

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 math #Question 1 def playNovice(marbles): import random rand = random.randint(1,math.floor(0.5*marbles)) print('Computer takes: ', rand) #Question 2 def userPlay(marbles): userAmount = marbles while userAmount > (marbles * 0.5): userAmount = int(input("Input Number of Marbles you want to take from pile: ")) return userAmount #Question 4 import random marbles = random.randint(10,100) print("Size of Marble Pile: ", marbles) OneOrZero = random.randint(0,1) starter = int(input("Enter a Number between 0 and 1 to decide who starts: ")) while marbles > 1: if starter == OneOrZero: while marbles > 1: print("Your Turn!") rand = userPlay(marbles) marbles = (marbles - rand) print(marbles, 'Marbles Remaining') print("Computers Turn!") rand = playNovice(marbles) marbles = (marbles - rand) print(marbles, 'Marbles Remaining') elif starter != OneOrZero: while marbles > 1: print("Computers Turn!") rand = playNovice(marbles) marbles = (marbles - rand) print(marbles, 'Marbles Remaining') print("Your Turn!") rand = userPlay(marbles) marbles = (marbles - rand) print(marbles, 'Marbles Remaining')

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