Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

CS 1410 Program 5 Spring 2018 Dr. Oberhoff DUE: April 15 Value: 100 points This

ID: 3700292 • Letter: C

Question

CS 1410 Program 5 Spring 2018 Dr. Oberhoff DUE: April 15 Value: 100 points This program uses the material from chapters1-6. The .cpp file should be submitted by the due date and a hard copy should be brought to class on April 16 (due at start of class). The general instructions should be followed as before. In addition, a flowchart should be constructed and also turned in April 16. See problem 24 on page 378 of the text for the rules of the game. All input will come from the monitor and output will go to the screen. The results (see below) will go to the screen and to an output file (named with your last name). The program will let the user play the game rock-paper-scissors with the computer. Here is a sketch of the program: - display a message that the game is rock-paper- scissors. - ask the user if they want to play – if no, then stop. - if yes, continue the game and loop through the steps until the user chooses “no”. - call on a function named userPick that will display a message to pick one of the items: 1 for rock, 2 for paper or 3 for scissors and will return your pick. - call on a function computerPick to get the computer’s choice. This function will use a random number generator to produce a 1, 2, or 3 result which will be returned. - a function displayResult should receive your pick and the computer’s pick and will display the results. - go back to the step that asks the user if they want to play. Here is possible output from two games played:

Here are the results from the game Rock-Paper-Scissors

*********************************************

The computer picked rock

The user picked paper

The winner is the user

***********************************************

The computer picked paper

The user picked paper.

The result is a tie.

***********************************************

Done. Game created by Dr. Oberhoff

Explanation / Answer

// File Name: RockPaperScissors.cpp
#include<iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

// Function to display the game result
void displayResult(string userPlay, string computerPlay)
{

// Checks for tie. If user choice and computer choice is same
if (userPlay.compare(computerPlay) == 0)
cout<<" It's a tie!";

// If user choice is "Rock"
else if (userPlay.compare("Rock") == 0)
{
// Then checks if computer choice is "Scissors" then user wins
if (computerPlay.compare("Scissors") == 0)
cout<<" Rock crushes scissors. The winner is the user.";

// Otherwise, checks if computer choice is "Paper" then computer wins
else if (computerPlay.compare("Paper") == 0)
cout<<" Paper eats rock. The winner is the computer.";

}// End of else if

// Otherwise, if user choice is "Paper"
else if (userPlay.compare("Paper") == 0)
{
// Then checks if computer choice is "Scissors" then computer wins
if (computerPlay.compare("Scissors") == 0)
cout<<" Scissor cuts paper. The winner is the computer.";

// Otherwise, checks if computer choice is "Rock" then user wins
else if (computerPlay.compare("Rock") == 0)
cout<<" Paper eats rock. The winner is the user.";
}// End of else if

// Otherwise, if user choice is "Scissors"
else if (userPlay.compare("Scissors") == 0)
{
// Then checks if computer choice is "Paper" then user wins
if (computerPlay.compare("Paper") == 0)
cout<<" Scissor cuts paper. The winner is the user.";

// Otherwise, checks if computer choice is "Rock" then computer wins
else if (computerPlay.compare("Rock") == 0)
cout<<" Rock breaks scissors. The winner is the computer.";
}// End of else if
}// End of function


// main function definition
int main()
{
// To store random number generated for computer
int computerSelection;
// To store random number generated for user
int userSelection;
// To store string message for user
string userPlay = "";
// To store string message for computer
string computerPlay = "";
// To store user choice for play
char choice;


// Loops till user choice is not 'N' or 'n'
do
{
srand((unsigned)time(0));
// Generate computer's play (0,1,2)
computerSelection = (rand() % 3) + 1;

// Translate computer's randomly generated play to string message
// Checks if the computer selection is one then store "Rock" as compute play selection
if (computerSelection == 1)
computerPlay = "Rock";

// Otherwise, checks if the computer selection is two then store "Paper" as compute play selection
else if (computerSelection == 2)
computerPlay = "Paper";

// Otherwise, checks if the computer selection is three then store "Scissors" as compute play selection
else if (computerSelection == 3)
computerPlay = "Scissors";

// Generate user's play (0,1,2)
userSelection = (rand() % 3) + 1;

// Translate computer's randomly generated play to string message
// Checks if the user selection is one then store "Rock" as user play selection
if (userSelection == 1)
userPlay = "Rock";

// Otherwise, checks if the user selection is two then store "Paper" as user play selection
else if (userSelection == 2)
userPlay = "Paper";

// Otherwise, checks if the user selection is three then store "Scissors" as user play selection
else if (userSelection == 3)
userPlay = "Scissors";

// Print computer's and user's play string
cout<<" **************************************************";
cout<<" The computer picked "<<computerPlay;
cout<<" The user picked "<<userPlay;

// Calls the function to check the winner or tie
displayResult(userPlay, computerPlay);
cout<<" **************************************************";
cout<<" Want to play again (Y / N)";

// Accepts user choice to continue or not
cin>>choice;

// Checks if user choice is 'N' or 'n' then stop
if(choice == 'N' || choice == 'n')
break;
}while(true);// End of do - while loop
}// End of main function

Sample Output:


**************************************************
The computer picked Scissors
The user picked Rock
Rock crushes scissors. The winner is the user.
**************************************************
Want to play again (Y / N)Y

**************************************************
The computer picked Rock
The user picked Scissors
Rock breaks scissors. The winner is the computer.
**************************************************
Want to play again (Y / N)y

**************************************************
The computer picked Paper
The user picked Rock
Paper eats rock. The winner is the computer.
**************************************************
Want to play again (Y / N)y

**************************************************
The computer picked Scissors
The user picked Scissors
It's a tie!
**************************************************
Want to play again (Y / N)y

**************************************************
The computer picked Scissors
The user picked Rock
Rock crushes scissors. The winner is the user.
**************************************************
Want to play again (Y / N)y

**************************************************
The computer picked Scissors
The user picked Rock
Rock crushes scissors. The winner is the user.
**************************************************
Want to play again (Y / N)y

**************************************************
The computer picked Rock
The user picked Scissors
Rock breaks scissors. The winner is the computer.
**************************************************
Want to play again (Y / N)y

**************************************************
The computer picked Rock
The user picked Paper
Paper eats rock. The winner is the user.
**************************************************
Want to play again (Y / N)n