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/04We will only grade files submitted by handin21 in this directory, so make sure your programs are in this directory!
The goals for this lab assignment are:
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.
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 scissorsRemember that python strings can be compared with relational operators (like == or <) just like numeric types. For example:
"hello" != "good bye" # evaluates to True
$ 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:
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 TieBe sure to test your function's correctness for different combinations.
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.pyIn the rockpaperscissors.py main function you are going to write a program that:
---------------------------- Jignesh: 3 Computer: 2 ----------------------------Note that this function does NOT return a value (it is called for its side-effect of printing out information). Its function definition will look like:
def printScores(name, user_won, comp_won):
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.
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.