CSE 161 Assignment #4: Design Lab

Due date extended to Oct 10

This assignment should be completed individually, not in pairs. It should be turned in using Blackboard no later than 10:00am Saturday October 10th. Your will turn in 3 files for this assignment. Note that Blackboard does not currently support turning in an assignment in stages, so you will need to complete all parts, saving your files on your USB drive (or by emailing them to yourself), and turn in all the files at once.

Part I: Top-Down Program Design

Your task is to use the top-down design method to create a program that plays a tournament of the game Rock-Paper-Scissors. A tournament consists of a series of games until one of the players (the human or the computer) wins a total of 5 times. In each game, the computer chooses one of "rock", "paper", or "sissors", but keeps that choice secret. It then asks the human to type in his choice, and determines who wins, using the usual rule, paper beats rock, scissors beats paper, rock beats scissors. In order to keep your program as simple as possible, you will use the numbers 0, 1, and 2 instead of the words rock, paper, and scissors respectively. Therefore, your program will only need to read numbers, rather than strings, from the terminal.

In top-down design, you do not start writing Python code immediately. Instead, you start with a high-level statement of the task, and create a series of more and more detailed versions of it until you reach the level of Python code. For this assignment, you will need to save copies of 7 versions of your program, corresponding to these design steps:

  1. State the task
  2. Determine beginning (initialization), middle (computation), and end (results) section
  3. Identify main loop and stopping condition for middle section
  4. Identify the subtasks to be performed inside the main loop
  5. Identify "non-obvious" subtasks and break them down further
  6. Turn steps into Python
  7. Read code carefully to check correctness and completeness, and modify as necessary

You should save your work in one long document. The easiest way to do this is to write the first version (#1: State the task), select, copy, and paste to make two copies of it; edit one of the copies to create the second version (#2: Determining beginning, middle, and end), and repeat, until you have all 7 versions. Note that you do not need to actually run your program in order to complete this part of the assignment, although you may wish to do so in order to be confident it is correct!

If you use Microsoft Word to create the document, save it in a file named rps-design.doc. If you use a different word processor (or the IDLE editor itself), save it as a plain text file in a file named rps-design.txt. Turn in this file.

You can study the slides from class on September 28 to see how I used top-down design for a program to play the game Nim. Follow this link:

http://www.cs.rochester.edu/u/kautz/Courses/csc161fall2009/lectures/161-06-28sept-top-down-design.pdf

Part II: Short Exercises about Lists and Strings

Each of the following programming tasks can be accomplished with just a few lines of Python, maybe even a single line. Solve each, and save your work in a file named exercises.txt. You can do this by solving them directly in the Python Shell window in IDLE, and then selecting the menu option "Save As" to save the contents of the Shell window into a file.

1. Suppose the variable S contains a string. Write a statement that always prints the LAST character of S, no matter how long or short S is. Show that it works using two different values for S.

2. Suppose the variable S contains a string. Write an expression that evaluates to True if and only if the first three characters of S are the same as the last three characters of S. You may assume that S is at least 3 characters long, but do not assume anything else about how long it is. Show that it works using two different values for S.

3. Let W be a list of the days of the weeks, W = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']. Suppose the variable N contains an integer between 0 and 6. Write a statement that prints the day of the week that corresponds to the value stored in X, where 0 corresponds to Sun, 1 corresponds to Mon, etc. Show that it works using two different values for X.

4. Define W as in the previous question. Now suppose S contains a string naming a day of the week. Write a series of statements that computes the number N that corresponds to the value in S, where as before 0 corresponds to Sun, 1 to Mon, etc. Your first statement should be a for statement. Show that your little program works by using it for two different values of S. Note that when your code runs the number of the weekday needs to be stored in N, it is not enough to simply print the number.

Part III: Improving Input and Output for Rock-Paper-Scissors

Your Rock-Paper-Scissors game from Part I could be made more "user-friendly" by having it use the words "rock", "paper", and "scissors" when interacting with the user, rather than the number 0, 1, and 2.

Modify your final design from Part I so that the program inputs and outputs words rather than numbers to refer to rock, paper, and scissors. The rest of your program should NOT change: it will still use numbers for making its own choice and for comparing choices. Save your program in a file named rps.py and test it to make sure it works. Turn in this file.

Use the function raw_input to read the player's choice, for example,

S = raw_input('Your choice: ')

See your answers to Part II (3) and (4) for a clue as to how you can convert the words rock, paper, scissors to the numbers 0, 1, 2 and vice-versa.