For this project, we are sticking to a theme of children's games. For Task 1, you are to complete a program that simulate a round of rock-paper-scissors. For Task 2, you are to create functions that translate English sentences to Pig Latin, and vice versa.
You are to do this project individually.
In our version of the game, the players do not use hand gestures to indicate their weapons of choice, instead the weapons are expressed as input strings.
For this task, you are to complete a program that simulates a round of rock-paper-scissors.
We have given you a starter file called project3.py
.
The file already had one function
called RPSgame
,
you need to write two more functions to make your program work.
When your program is finished,
you should be able to call RPSgame
in the Python shell window by providing two parameters
representing the weapons chosen by two players: player 1 and player 2 (in that exact order).
Given the two input strings, each time the function is called it
should print out the decision for the corresponding round of rock-paper-scissors.
For your reference, the following are some example input/output when we call RPSgame
(after the program is finished):
>>> RPSgame("rock", "paper")
Player 2 wins
>>> RPSgame("scissors", "paper")
Player 1 wins
>>> RPSgame("rock", "rock")
It's a tie
>>> RPSgame("lizard", "rock")
Both players must select from rock, paper, or scissors
>>> RPSgame("flower", "lighting bolt")
Both players must select from rock, paper, or scissors
In the first example, player 1 chose "rock" and player 2 chose "paper". According to the rule of rock-paper-scissors, paper beats rock; thus player 2 wins, and the message "Player 2 wins" is printed. In the second example, player 1 chose "scissors" and player 2 chose "paper". According to the rule of rock-paper-scissors, scissor beats paper; thus player 1 wins, and the message "Player 1 wins" is printed. In the third example, both player 1 and player 2 chose the same weapon. According to the rule of rock-paper-scissors, it's a tie; and the message "It's a tie" is printed.
Finally, in our program, both player 1 and player 2 must choose from either "rock", "paper", or "scissors".
If either player 1 or player 2 (or both) chose any other weapons, RPSgame
will print
a message that says "Both players must select from rock, paper, or scissors". The last two examples illustrate this.
Let's look at the RPSgame
function from the starter file we had given you.
We can see that the RPSgame
function uses another function called isLegal
to check if weapons
chosen by both players are either "rock", "paper", or "scissors". Furthermore, the RPSgame
function
uses a function called beats
that takes in weapons from two players and determine if the weapon chosen by the first players beats
the weapon chosen by the second player.
Your job for this task is to complete the isLegal
function and the
beats
function. Here are the specifics of what you need to do:
isLegal
.
The function should have one parameter, the weapon chosen by a player (expressed as a string). The function should return
True
if the weapon is either "rock", "paper", or "scissors".
The function should return False
otherwise.
beats
.
The function should have two parameters representing the weapons chosen by two players (both expressed as strings).
You may assume that the weapons chosen by both players are legal.
The function should return True
if the weapon chosen by the first player (the first parameter string) beats the weapon
chosen by the second player (the second parameter string) according to the rule of rock-paper-scissors. The function should return
False
otherwise.isLegal
and beats
, you should be able to call the RPSgame
function we had given you in the Python shell window. Try running RPSgame
with the input from the previous examples and make sure that you have gotten the same output.
Please be cautious of three things when completing this task:
RPSgame
. The two extra functions you wrote
should be able to work with the function we had given you.
project3.py
) as the function we had provided for you. Sometimes when adults don't want small children to understand what they are saying, they disguise each of the English words they are speaking by transforming it into a corresponding "Pig Latin" word. Note that, because Pig Latin is fundamentally a spoken language and the connection between English spelling and pronunciation is so weird, no simple set of rules can possibly get all cases right. Just follow the rules I state and don't worry if their behavior on some weird cases seems wrong. You will be writing functions for translating individual words from Pig Latin to English and from English to Pig Latin. The following table gives some examples. One of your functions will translate a word from the right-hand column into the corresponding word on the left; your other function will do the reverse:
English | Pig Latin |
---|---|
this | is-thay |
is | is-ay |
Pig | ig-Pay |
Latin | atin-Lay |
shh | -shhay |
she | e-shay |
You are to accomplish the following:
Write the contract, docstring and implementation for a function called unPigWord
. The function should have one parameter, the Pig Latin word that
should be translated to English. You may assume that every time your function is used, it is passed a string that
has exactly one hyphen ("-"
) in it and ends with the letters "ay"
. Your function should
start by finding the position of the hyphen. Your function should then extract from the string two substrings: the
portion before the hyphen and the portion between the hyphen and the "ay"
ending. For example, when
given the string "is-thay"
, the two substrings are "is"
and "th"
. Finally,
your function should combine these two substrings in the reverse order and return the result. In the preceding
example, this would be "this"
. Make sure your function works on the examples from the earlier table
and is not unnecessarily complex.
Write the contract, docstring and implementation for a function called findVowel
that takes a single parameter, a string, and finds the first position
within that string where any of the characters "aeiouAEIOU"
appears. For example, given the string
"is"
, your function should return 0, and given the string "Pig"
, your function should
return 1. If the string doesn't have any of the listed vowels in it at all, such as with the string "shh"
,
then your function should return the length of the string, which would be 3 in this example. Make sure your function
works correctly and is not unnecessarily complex.
Write the contract, docstring and implementation for a function, pigWord
, which returns the translation of a single word from English to Pig Latin.
The translation consists of everything from the first vowel onward (as determined by findVowel
), then a
hyphen, the portion of the word that preceded the first vowel, and the letters "ay"
.
Make sure to use the findVowel
function.
Make sure your
function works on the examples from the earlier table and is not unnecessarily complex.
Put the functions you wrote for Task 2 in the same file project3.py
Submit your code using Moodle; click on the following link for instructions on submitting code using Moodle. For this project, you will need to submit the following Python file:
isLegal
and beats
) along with the function we had given you (RPSgame
),
The grader will test your program with different input values. Check your work against the grading rubric project3gradesheet.pdf which the grader will use when grading your lab.