CS414 Lab 4: Nim

February 22nd, 2017

Due: February 22nd, 2017, before midnight

Late penalties: Thu -5%, Fri -10%, Sat, Sun 20%.

Getting started

Modifications

Implement each of the following changes. After each change, run the program you have, to see the effect of your changes, and to look for bugs.
  1. A (input validation): you can't take stones that aren't there. After getting stones_taken from the user, check: if it's more than pile_size,
              print 'Too many!  There are only', pile_size, 'stones'
              continue
              
    The statement continue will skip the rest of the actions in the loop, and go back to the top of the while loop.

  2. B (check if done): the player loses when he or she takes the last stone. Add a line to check if there are 0 stones. If so,
                print 'You lose!'
                break
              

  3. C (add a player): The player will always lose, eventually. Let's have two players. Add another variable at the top:
                player_ID = 1
              
    and, just after while True:,
                print 'Player', player_ID, 'moves next'
              
    At the bottom, change player_ID: if it is 1, make it 2, and vice versa.

    Finally, change 'You lose!' to something that prints one of these two :

                  'Player 1 loses!'
                  'Player 2 loses!'
                
    depending on who just took the last stone

  4. D (visibility): It's a bit hard to keep track of the moves. Print a long string 10 or 20 '-':
                print '-' * 20
              
    before the line
                print 'Player', player_ID, 'moves next'
              

  5. E (multiple piles): In a real game of nim, there are several piles. Replace the initialization pile_size = 5 with three initializations:
                pile1_size = 5
                pile2_size = 5
                pile3_size = 5
              
    Now, the line print 'Pile has', pile_size, 'stones' has to be replaced with three print statements.

    Also, you have to ask the user which pile to take from:

                  pile_number = int(raw_input('Which pile to take from? '))
                
    Also, you have to check if the pile chosen has enough stones, so you will need to change the input validation:
                  if (pile_ID == 1 and stones_taken > pile1_size) or (pile_ID == 1 and ...)
                
    Another consequence: The game is over when all three piles are empty. So, modify the code that checks if some user has lost.

  6. All of this is so much easier with lists! Close the file nim.py . Download the other file nim_list.py . I have written some of the code for you.

    CAREFUL! The user will type a pile_ID from 1 to 3, but the list indexes go from 0 to 2. So, always write pile_sizes[pile_ID - 1] to compensate.