CS21 Lab 4: while loops and functions

Due Saturday (Feb 18) before midnight

This lab assignment involves writing three programs, each of which adds functionality to the previous one. The result will be a third program that plays multiple games of Rock-Paper-Scissors with the computer (our computer player won't be as clever as this one).

First run update21. This will create the cs21/labs/04 directory and copy over any starting-point files for your programs. Next, move into your cs21/labs/04 directory and begin working on the Python programs for this lab. The pwd command helps you verify that you are in the correct sub-directory.

$ update21
$ cd cs21/labs/04
$ pwd
/home/[your_username]/cs21/labs/04
We will only grade files submitted by handin21 in this directory, so make sure your programs are in this directory!

Goals

The goals for this lab assignment are:

Programming tips for larger programs

As your programs become larger, it is even more important to develop good habits:

The above are mandatory and you will be graded on them.

We strongly advise you to write your programs incrementally and test them as you go. No one writes interesting programs all at once. As you write, test that your program does what you expect (even if it doesn't yet do what you want). If it surprises you, make sure you understand what's happening before writing more code.

We have provided some example test input and output; make sure you test your program! You should also test your program using some inputs and outputs that were not shown here; after all, we will. Come up with your own test cases and verify that the program is producing the right output on them.



1. Choose an Option
In a file named, getoption.py, write a function named getChoice that asks the user to enter one of "rock", "paper" or "scissors", and returns the string value entered by the user. If the user doesn't enter one of these three valid string values, your function should print out an error message, and prompt the user to try again. The function should only return when the user enters a valid string value.

The getChoice function does not take any input values (i.e. has no parameters) but it should return a string value, one of "rock", "paper" or "scissors". Its function definition will look like:

def getChoice():

Write a main function that makes a call to your getChoice function and prints out the value returned. Here are some sample runs:

$ python getoption.py
Pick one of rock, paper or scissors: paper
getChoice returned  paper

$ python getoption.py
Pick one of rock, paper or scissors: Scissors
Hey, Scissors isn't rock, paper or scissors...try again
Pick one of rock, paper or scissors:  yes it is!
Hey, yes it is! isn't rock, paper or scissors...try again
Pick one of rock, paper or scissors:  okay, okay, okay
Hey, okay, okay, okay isn't rock, paper or scissors...try again
Pick one of rock, paper or scissors:  scissors
getChoice returned  scissors
Remember that python strings can be compared with relational operators (like == or <) just like numeric types. For example:
"hello" != "good bye"   # evaluates to True

2. Calculate the Winner
First, copy your getoption.py solution into a new file named onegame.py. The onegame.py file will contain your solution to Part 1, that you will use as a starting point for Part 2. Use the cp command to copy one file to another:
$ cp  getoption.py  onegame.py

$ vim onegame.py  # contains all contents of getoption.py

Into the onegame.py file, you will add a new function, calculateWinner. This function will take as input two string values, the first representing player one's choice of ("rock", "paper" or "scissors") and the second representing player two's choice. The function should determine the winner of one game of Rock-Paper-Scissors based on player one and player two's choices, and return a value indicating which player won. Your function should return:

Its function definition will look like:
def calculateWinner(player1, player2):
The rules of Rock-Paper-Scissors are the following:

Change the main function to make two calls to your getChoice function from Part 1, one to get player one's choice and the other to get player two's choice. Your main program should next print out both player's choices, and then make a call to your calculateWinner function to compute the winner of the game. Your program should print out the value returned by the function and also a message indicating which player won the game.

Here are some example runs:

$ python onegame.py
Player 1: 
Pick one of rock, paper or scissors: rock
Player 2: 
Pick one of rock, paper or scissors:  scisssors
Hey, scisssors isn't rock, paper or scissors...try again
Pick one of rock, paper or scissors:  scissors
Player 1 picked rock and Player 2 picked scissors
calculateWinner returned 1
  Player 1 wins! 

$ python onegame.py
Player 1: 
Pick one of rock, paper or scissors:  rock
Player 2: 
Pick one of rock, paper or scissors:  paper
Player 1 picked rock and Player 2 picked paper
calculateWinner returned 2
  Player 2 wins! 

$ python onegame.py
Player 1: 
Pick one of rock, paper or scissors:  paper
Player 2: 
Pick one of rock, paper or scissors:  paper
Player 1 picked paper and Player 2 picked paper
calculateWinner returned 0
  A Tie
Be sure to test your function's correctness for different combinations.

3. Rock-Paper-Scissors Against the Computer
For the final part, you are going to start with your solution to Part 2, and add to it code that plays multiple rounds of Rock-Paper-Scissors against the computer.

First, copy your onegame.py solution into a new file named rockpaperscissors.py. The rockpaperscissors.py file will contain your solution to this next part. Here is how to copy one file to another:

$ cp onegame.py rockpaperscissors.py
In the rockpaperscissors.py main function you are going to write a program that:
  1. prompts the user to enter his/her name.
  2. prompts the user to enter the number of total wins to play to (num_wins).
  3. prints out a welcome message to the user, that includes the user's name and the number of total wins entered.
  4. Plays multiple games of Rock-Paper-Scissors against the computer until the first player wins num_wins games.
  5. Prints first player to win the specified number of games, and how many total rounds of play it took to win.
In each round of playing one game of Rock-Paper-Scissors (part of step 4 above), your program should:
  1. get the user's choice for this round.
  2. randomly select one of "rock", "paper" or "scissors" for the computer's choice for this round.
  3. print each player's choice.
  4. calculate who won, and print out who won this round of play.
  5. print out the number of games won so far by the player and by the computer.

Requirements:

Sample Output

Here is sample output from a couple runs of our program: Sample Output Take a look at this to get an idea of what your program needs to do.

Your program's output does not need to be identical to ours, but it should have all the required functionality and show the required information in a easy-to-read way.

4. Answer the Questionnaire; Run handin21

Once you are confident that all three programs work, fill out the questionnaire in README-04.txt.

Then run handin21 a final time to make sure we have access to the most recent versions of the files required for this lab.