< Back

Lab 21: Test Review

As usual, create a directory to hold today's files. All programs that you write today should be stored in this directory.

$ cd ~/cs120/labs
$ mkdir lab21
$ cd lab21


Programming Exercises

For review for Monday, answer the following programming questions in a file called test_review.py:

  1. Write a function weird_divisible(test_number), which returns True if the specified integer test_number is divisible by 10 or 7.
  2. Write a function odd_characters(my_string), which returns a string, the characters at odd indicies from my_string.
  3. Write a function sum_vowels(), which returns a floating point number. This number is the average ASCII value for the vowels in the English language.

Solutions


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.