CS 160 - Programming Concepts and Applications

Summer II 2018 - Lab 4B - RPS: Multifile

Quick Links: Canvas | John Cabot | Piazza | zyBooks

|   Home |  Contact |  Syllabus |  Assignments |  Schedule |  Resources   |

This lab is due by Tuesday, July 17, 2018, 11:59 PM.

Make a copy of Lab3D's main.cpp and place it as the starting point for Lab4B.


Rock Paper Scissors Part V



At the end of Lab3D, we had a fully functioning rock-paper-scissors game that allowed the user to play multiple games. Now we want to reorganize our code structure to make use of functions to simplify our code.


Concepts



The focus of this lab is on one concept: FUNctions.


Working with Data



Since we are now using functions, our main() function becomes much simpler and more descriptive of the steps that are occurring. The specific details are abstracted away into the individual function implementations. We will use the following main() as our starting template. Your task will be to create the functions to make the following program work.

int main() {
  char playAgain = 'Y';
  int numWins = 0, numTies = 0, numLosses = 0;
  do {
    char userChoice = getUserChoice();
    char compChoice = generateComputerChoice();
    int gameResult = determineResult( userChoice, compChoice );
    cout << choiceToText( userChoice ) << " "
         << gameOutcomeAsText( gameResult ) << " "
         << choiceToText( compChoice ) << endl;
    incrementTotals( gameResult, numWins, numTies, numLosses );
    cout << "Do you want to play again? (Y or N) ";
    cin >> playAgain;
  } while( playAgain == 'Y' );
  cout << "You won  " << numWins << " game(s)." << endl;
  cout << "You lost " << numLosses << " game(s)." << endl;
  cout << "You tied " << numTies << " game(s)." << endl;
  return 0;
}


Instructions



In order to follow incremental development and have our program be able to compile, you should first create "function stubs" that have the input/output specified but no logic inside the function. Below are descriptions of each function that you need to implement stating the name, input, and output of each function.

  1. getUserChoice
    • Name: getUserChoice
    • Input: None
    • Output: A character
    • Description: Prompts the user to enter a choice of (r)ock, (p)aper, (s)cissors. The function returns the user's choice.
  2. generateComputerChoice
    • Name: generateComputerChoice
    • Input: None
    • Output: A character
    • Description: Randomly generates a choice for the computer corresponding to (r)ock, (p)aper, (s)cissors. The function returns the computer's choice.
  3. determineResult
    • Name: determineResult
    • Input: Two characters passed by value corresponding to the user's and computer's choices
    • Output: An integer
    • Description: Based on the two inputs, determines the result as win/lose/draw. If the result is a win for the user, return 1. If the result is a loss for the user, return -1. If the result is a draw, return 0.
  4. choiceToText
    • Name: choiceToText
    • Input: A character passed by value
    • Output: A string
    • Description: Converts the inputted character to the long form word. 'r' to "Rock". 'p' to "Paper". 's' to "Scissors". The function returns the corresponding string.
  5. gameOutcomeAsText
    • Name: gameOutcomeAsText
    • Input: An integer passed by value
    • Output: A string
    • Description: Converts the inputted integer to the long form result word. 1 to "beats". 0 to "draws". -1 to "loses to". The function returns the corresponding string.
  6. incrementTotals
    • Name: incrementTotals
    • Input: An integer passed by value and three integers all passed by reference
    • Output: Nothing
    • Description: Based on the first parameter, which corresponds to the game result, increment the corresponding total parameter.


Lab Submission



You will submit your solution to this lab with the rest of Set4. Detailed instructions for doing this are posted in Assignment 4.


This lab is due by Tuesday, July 17, 2018, 11:59 PM.

Last Updated: 07/05/18 11:27


Valid HTML 4.01 Strict Valid CSS! Level Triple-A conformance, W3C WAI Web Content Accessibility Guidelines 2.0