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 rockPaperScissors.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.
rockPaperScissors.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. There are many different variants of Pig Latin in use, so if you think you already know Pig Latin, you should still pay careful attention to the specific rules given in this assignment; they may be a bit different from the ones you know. Also, 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 your
function works on the examples from the earlier table and is not unnecessarily complex.
Save the python file containing the functions you wrote for Task 2 as piglatin.py
You will be submitting 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 files:
isLegal
and beats
) along with the function we had given you (RPSgame
). The grader will
run your program with different input to see if your program works.
You will earn one point for each of the following accomplishments:
You have written a correct contract and docstring for the isLegal
function.
Your isLegal
function correctly checks if the weapon chosen by a player is either "rock", "paper", or "scissors".
Your isLegal
function returns a boolean answer.
You have written a correct contract and docstring for the beats
function.
Your beats
function correctly identifies the cases in which the first player would win in a round of rock-paper-scissors.
Your beats
function returns a boolean answer.
Your isLegal
and beats
functions do not contain overly complex nested if statements.
You have written a correct contract and docstring for the unPigWord
function.
Your unPigWord
function correctly locates the hyphen. In locating the hyphen, you make good use of Python's string-handling features, avoiding excess complexity.
Your unPigWord
function correctly extracts the substring preceding the hyphen. In extracting the substring preceding the hyphen, you make good use of Python's string-handling features, avoiding excess complexity.
Your unPigWord
function correctly extracts the substring between the hyphen and the ending. In extracting the substring succeeding the hyphen, you make good use of Python's string-handling features, avoiding excess complexity.
Your unPigWord
function correctly forms its return value from the identified substrings.
findVowel
function.Your findVowel
function correctly finds the first vowel within the string. Your findVowel
function makes good use of Python's string-handling features to avoid excess complexity.
Your findVowel
function returns the specified value when given a string with no vowel.
pigWord
function.Your pigWord
function finds the dividing position within the given string.
Your pigWord
function correctly extracts the substring prior to the dividing position. In extracting the substring prior to the dividing position, you make good use of Python's string-handling features, avoiding excess complexity.
Your pigWord
function correctly extracts the substring starting at the dividing position. In extracting the substring starting at the dividing position, you make good use of Python's string-handling features, avoiding excess complexity.
Your pigWord
function correctly forms its return value.