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
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 thats 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, youll 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!
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started