CPSC120A
Fundamentals of Computer Science I

Lab 21

Review

Use the command line to create a new directory called lab21 in your labs directory. Make sure all of the .py files that you create for this activity are in that directory.

Pick-up Sticks

Nim is an ancient game where players take turns picking up stones. The stones are arranged in some fashion and each turn a player must remove 1, 2, or 3 stones from the collection. The player that picks up the last stone loses. In this activity you will write a command line version of the game, using sticks instead of stones.

Details

Write a function called play_nim(num_sticks) in a file called nim.py. The function should play a two-player game of nim with num_sticks initially in a row. The function should prompt the player for the number of sticks to remove. If the player does not enter a number of sticks in the range \([1, 3]\), the user should be prompted to enter a new value. The removed sticks should be displayed with underscore characters. When player 1 removes sticks, they should be removed from the left side of the collection. When player 2 removes sticks, they should be removed from the right side of the collection. The winner of the game should be displayed when there are no sticks left.

It is difficult to test a program that uses user input like this one. Make an additional function that plays one turn of nim without using user input, for testing purposes.

Example

>>> nim(10)
||||||||||
Player 1, enter a number of sticks (1-3): 3
___|||||||
Player 2, enter a number of sticks (1-3): 3
___||||___
Player 1, enter a number of sticks (1-3): 4
Player 1, enter a number of sticks (1-3): 3
______|___
Player 2, enter a number of sticks (1-3): 1
__________
Player 1 wins!

Hint

  • The collection of sticks should be stored as a string. Recall that you can use the * operator to create multiple copies of a specified string.

  • The program will be easier to write and easier to test if there is a function remove_sticks(stick_string, number, side). The function should return a string that is equivalent to stick_string with the necessary number of sticks replaced with underscores. Side should be an integer, either 1 or 2. Let 1 specify the left side of the string, and 2 specify the right side of the string.

    Note that this can be done with a single slice if you find the location of the first "|" character from the necessary side of the string.

    Test this function thoroughly before proceeding!

  • With the above function, writing the game is simple. Create a loop that runs while there are still sticks in the stick string. Use the string count method to determine the number of sticks remaining.

    For each iteration of the loop, print the stick string and prompt the user to enter a number of sticks. Call the remove_sticks function with the numbers the user entered. The remove_sticks function also needs an integer variable that represents whose turn it is. Every iteration of the loop, this variable should flip-flop between 1 and 2.

  • Verify that the user's input is valid by adding a while loop after getting the user's input that runs while the user's input is invalid. Inside the loop re-prompt the user for input.

  • Finally, print who won after the main loop. Use the integer variable that keeps track of who's turn it is to print the appropriate winner.

Challenge

Create a 1-player version of the game that uses a very simple artificial intelligence. If the computer can win by leaving the player with only 1 stick, it should. Otherwise, the computer should remove a random number of sticks between 1 and 3.


Challenge

Create a 1-player version of the game that uses an intelligent artificial intelligence. The computer should use the optimal game strategy. If one player is using the optimal strategy and the other is not, the optimal strategy will always win. If both players are using the optimal strategy then whoever goes first will win. It is up to you to figure out what the optimal strategy is.

Submission

Please show your source code and run your programs for the instructor or lab assistant. Only a programs that have perfect style and flawless functionality will be accepted as complete.