MCS-177 Project 3: Child's Play (Spring 2016)

Start: Tuesday, 3/1; Due: Wednesday, 3/9, by the beginning of class


Overview

This project is designed to reinforce the concepts we have learned from lecture 7 - 10. You should read the corresponding lecture notes and Section 2.6, 3.1 - 3.8 of the textbook before getting started.

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.

Task 1: Rock-paper-scissors

Rock-paper-scissors is traditionally a hand game played by two people. During a round, both players simultaneously select one of the three weapons: rock, paper, or scissors. The "rock" beats "scissors", the "scissors" beat "paper", and the "paper" beats "rock". If both players throw the same shape, that round of the game is tied. The following graph illustrates the rule of rock-paper-scissors:

rock paper scissor

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:

  1. Write the contract, docstring and implementation for a function called 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.
  2. Write the contract, docstring and implementation for a function called 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.
After you have finished the functions 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:

  1. You can not make any changes to the function RPSgame. The two extra functions you wrote should be able to work with the function we had given you.
  2. You should make sure that the selection statements you use are not unnecessarily complex. For example, you will loose points for making too many nested if statements (in fact, my solution for this task does not contain any nested if statements at all).
  3. You should write your two functions in the same file (rockPaperScissors.py) as the function we had provided for you.

Task 2: Pig Latin

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:

EnglishPig Latin
thisis-thay
isis-ay
Pigig-Pay
Latinatin-Lay
shh-shhay
shee-shay

You are to accomplish the following:

  1. 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.

  2. 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.

  3. 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

Submitting your work

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:

Grading

You will earn one point for each of the following accomplishments:

  1. You have written a correct contract and docstring for the isLegal function.

  2. Your isLegal function correctly checks if the weapon chosen by a player is either "rock", "paper", or "scissors".

  3. Your isLegal function returns a boolean answer.

  4. You have written a correct contract and docstring for the beats function.

  5. Your beats function correctly identifies the cases in which the first player would win in a round of rock-paper-scissors.

  6. Your beats function returns a boolean answer.

  7. Your isLegal and beats functions do not contain overly complex nested if statements.

  8. You have written a correct contract and docstring for the unPigWord function.

  9. Your unPigWord function correctly locates the hyphen. In locating the hyphen, you make good use of Python's string-handling features, avoiding excess complexity.

  10. 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.

  11. 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.

  12. Your unPigWord function correctly forms its return value from the identified substrings.

  13. You have written a correct contract and docstring for the findVowel function.
  14. 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.

  15. Your findVowel function returns the specified value when given a string with no vowel.

  16. You have written a correct contract and docstring for the pigWord function.
  17. Your pigWord function finds the dividing position within the given string.

  18. 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.

  19. 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.

  20. Your pigWord function correctly forms its return value.