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

Please only do it as you understand the problem, don\'t coppy from other and pas

ID: 3681644 • Letter: P

Question

Please only do it as you understand the problem, don't coppy from other and paste. Thank in advance!

Game of Nim.

Two players alternately take marbles from a pile. In each move, a player chooses how many marbles to take. The player must take at least one but at most half of the marbles. The the other player takes a turn. The player who takes the last marble loses.

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

from random import randint

# utility function to print out the winner
def print_winner(turn):
if turn:
print "Computer takes the last remaining marble"
print "Congratulations you win"
else:
print "You will have to take the last remaining marble"
print "Sorry you lose. Better luck next time"

# function to find optimal number of marbles to remove
def find_optimal_number(n):
current = 1
while current<=n:
current = current*2

current = current/2
return (n - current + 1)

# Generate intial size of pile
size_of_pile = randint(10,100)
print "intial size of pile: "+ str(size_of_pile)
# generate random number to decide who takes the first turn
# computer moves first if first_turn == 1
first_turn = randint(0,1)

if first_turn:
print "The computer will take the first turn"
else:
print "You will take the first turn"

# generate random number to decide if the computer plays stupid or smart
# computer plays smart if smart_play == 1
smart_play = randint(0,1)
if smart_play:
print "The computer will play smartly"
else:
print "The computer will play stupid"

# variable to store who has the current turn
current_turn = first_turn

while size_of_pile>0:
if size_of_pile==1:
print_winner(current_turn)
break
if current_turn:
if smart_play:
x = find_optimal_number(size_of_pile)
else:
x= randint(1,size_of_pile/2)
print "the computer chose: "+str(x)
else:
while True:
print "Enter the number of marbles you want to remove"
x = int(raw_input())
if x<1 or x>(size_of_pile/2):
print "You cannot remove the specified number of marbles. enter a number within 1 and "+str(size_of_pile/2)
continue
else:
break
size_of_pile = size_of_pile - x
current_turn = (current_turn+1)%2