Please be accurate and clear with your answer. Thank in advance! Game of Nim. Cr
ID: 3681615 • Letter: P
Question
Please be accurate and clear with your answer. Thank in advance!
Game of Nim.
Create an algorithm design and write a Python program in which computer plays against a human opponent. Generate a random integer between 10 and 100 to denote the initial size of the pile. Generate a random integer between 0 and 1 to decide whether the computer or the human takes the first turn. Generate a random integer between 0 and 1 to decide whether the computer plays smart or stupid. In stupid model, the computer simply takes a random legal value (between 1 and n/2) from the pile whenever it has a turn. In smart model the computer takes off enough marbles to make the size of the pile a power of 2 minus 1 - that is, 3, 7, 15, 31, or 63. This is always a legal move, except if the size of the pile is currently one less than a power of 2. In that case, the computer makes a random legal move.
Note that the computer cannot be beaten in smart mode when it has the first move, unless the pile size happens to be 15, 31, or 63. Of course, a human player who has the first turn and knows the winning strategy can win against the computer.
Explanation / Answer
import math
def playNovice(marbles):
import random
rand = random.randint(1,math.floor(0.5*marbles))
print('Computer takes: ', rand)
return rand
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
import random
marbles = random.randint(10,100)
print("Size of Marble Pile: ", marbles)
> 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')