Python Assignement: Nim game reflected as pancakes problem.

 


At the table are you, your roommate, and two stacks of pancakes, of height m and n. You and she, in turn, must eat from the larger stack a non-zero multiple of the number of pancakes in the smaller stack. The bottom pancake of each stack is soggy, and thus whoever first finishes a stack is the loser.
For which pairs (m,n) do you, who happen to be playing first, have a winning strategy?

Program:

import random

PancakesLeft = int(input("Size of the first pancake stack:"))
Pancake_stack2 = int(input("Size of the second pancake stack:"))
PancakesToRemove = 0
userTurn = True

print("This is a game where players take turns taking Pancakes from a pile of Pancakes. The player who takes the last pancake loses. The current pancake count is: ", PancakesLeft)

while PancakesLeft > 0:
    while userTurn == True and PancakesLeft > 0:
        PancakesToRemove = int(input("How many Pancakes do you want to remove?"))
        if ( PancakesToRemove % Pancake_stack2 ) == 0:
            PancakesLeft -= PancakesToRemove
            print( "You removed " + str(PancakesToRemove) + 
                " pancake(s)! The current pancake count is: " + str(PancakesLeft) )   
            userTurn = False  
        elif PancakesLeft - PancakesToRemove < 0:
            print("There aren't that many Pancakes left!") #Give user error!  
        elif ( PancakesToRemove % Pancake_stack2 ) != 0:
            print( "You can't remove more than three Pancakes at a time! The current pancake count is: " + str(PancakesLeft) )                      
    while userTurn == False and PancakesLeft > 0:
        aiRemoves = random.randint( 1, min(3, PancakesLeft) )* Pancake_stack2#Take smaller value between 3 and the Pancakes that are left
        PancakesLeft -= aiRemoves
        print( "The A.I. removed " + str(aiRemoves) + 
            " pancake(s)! The current pancake count is: " + str(PancakesLeft) )    
        userTurn = True 

if userTurn == True:
    print("The A.I took the last pancake, it lost. You won the game!")
else:
    print("You took the last pancake, you lost. The A.I. won the game!")

Output:


Comments