Question

In: Computer Science

C++ questions, Please make sure to divide your program into functions which perform each major task....

C++ questions, Please make sure to divide your program into functions which perform each major task.

The game of rock paper scissors is a two player game in which each each player picks one of the three selections: rock, paper and scissors. The game is decided using the following logic:

  • ROCK defeats SCISSORS (“smashes”)

  • PAPER defeats ROCK (“wraps”)

  • SCISSORS defeats PAPER (“slices”)

    If both players choose the same selection the game ends in a tie. Write a program that asks the user to select one of the three choices. The computer will randomly choose one of the three options as well. Your program will then display both players' selections as well as the who won the game.

Solutions

Expert Solution

Below is the code:

#include <iostream>
#include <cmath>
#include <time.h>
#include <cstdlib>
using namespace std;
int main() // main function
{
char ch;
int win = 0; //variables declaration
int tie = 0;
int lose = 0;
do
{
int choice;
cout << "Rock, Paper, Scissors Game" << endl;
cout << "=========================="<<endl;
cout << "Enter your choice, Press 1 for Rock, 2 for Paper, 3 for Scissors:" << endl;
cin >> choice;
int ai = rand() % 3 + 1;
cout << "Computer's choice: " << ai << endl;
if(choice == 1 && ai == 1)
{
cout << "Rock meets Rock its a tie!" << endl;
tie++;
}
else if(choice ==1 && ai== 2)
{
cout << "Rock is covered by Paper the computer wins!." << endl;
lose++;
}
else if(choice == 1 && ai == 3)
{
cout << "Rock crushes Scissors you win!" << endl;
win++;
}
else if(choice == 2 && ai == 1)
{
cout << "Paper covers Rock you win!" << endl;
win++;
}
else if(choice == 2 && ai == 2)
{
cout << "Paper meets Paper its a tie!" << endl;
tie++;
}
else if(choice == 2 && ai == 3)
{
cout << "Paper is cut by Scissors the computer wins!" << endl;
lose++;
}
else if( choice == 3 && ai == 1)
{
cout << "Scissors are crushed by Rock computer wins!" << endl;
lose++;
}
else if( choice == 3 && ai == 2)
{
cout << "Scissors cuts Paper you win!" << endl;
win++;
}
else if(choice == 3 && ai == 3)
{
cout << "Scissors meet Scissors its a tie!" << endl;
tie++;
}
else{
cout << "Please select 1, 2 or 3" << endl;
}
cout << "Wins: " << win << endl;
cout << "Ties:" << tie << endl;
cout << "Losses:" << lose << endl;
cout << "Would you like to play again? Y/N" << endl;
cin >> ch;
  
} while(ch == 'Y' || ch == 'y');
return 0;
}

You have to provide your input among 1, 2, or 3 numbers. It pics a random number as a computer number and then starts the game. Once the play is over, this prompts the user to continue the game or not. If the user enters y or Y then the game continues

Below is the output screenshot:

---Adding the version 2 of the code with the required functions-----

#include <iostream>
#include <cmath>
#include <time.h>
#include <cstdlib>
using namespace std;
int win = 0; //variables declaration
int tie = 0;
int lose = 0;
//reading the choice from user
int readChoice()
{
int choice = 0;
cout << "Enter your choice, Press 1 for Rock, 2 for Paper, 3 for Scissors:" << endl;
cin >> choice;
return choice;
}

//generating the computer's choice:
int getComputersChoice()
{
int computerChoice= rand() % 3 + 1;
  
cout << "Computer's choice: " << computerChoice << endl;
return computerChoice;
}
//starts the game
void playTheGame(int choice, int ai)
{
if(choice == 1 && ai == 1)
{
cout << "Rock meets Rock its a tie!" << endl;
tie++;
}
else if(choice ==1 && ai== 2)
{
cout << "Rock is covered by Paper the computer wins!." << endl;
lose++;
}
else if(choice == 1 && ai == 3)
{
cout << "Rock crushes Scissors you win!" << endl;
win++;
}
else if(choice == 2 && ai == 1)
{
cout << "Paper covers Rock you win!" << endl;
win++;
}
else if(choice == 2 && ai == 2)
{
cout << "Paper meets Paper its a tie!" << endl;
tie++;
}
else if(choice == 2 && ai == 3)
{
cout << "Paper is cut by Scissors the computer wins!" << endl;
lose++;
}
else if( choice == 3 && ai == 1)
{
cout << "Scissors are crushed by Rock computer wins!" << endl;
lose++;
}
else if( choice == 3 && ai == 2)
{
cout << "Scissors cuts Paper you win!" << endl;
win++;
}
else if(choice == 3 && ai == 3)
{
cout << "Scissors meet Scissors its a tie!" << endl;
tie++;
}
else{
cout << "Please select 1, 2 or 3" << endl;
}
}
  
int main() // main function
{
char ch;
  
do
{
int choice, ai;
cout << "Rock, Paper, Scissors Game" << endl;
cout << "=========================="<<endl;
choice = readChoice();
ai = getComputersChoice();
playTheGame(choice, ai);
cout << "Wins: " << win << endl;
cout << "Ties:" << tie << endl;
cout << "Losses:" << lose << endl;
cout << "Would you like to play again? Y/N" << endl;
cin >> ch;
  
} while(ch == 'Y' || ch == 'y');
return 0;
}


Related Solutions

C++ questions, please make sure to dividet he program into functions which perform each major task,...
C++ questions, please make sure to dividet he program into functions which perform each major task, A leap year is defined as any calendar year which meets the following criteria: If the year is not divisible by 4, it is a common year If the year is not divisible by 100, it is a leap year If the year is not divisible by 400, it is a common year Otherwise it is a leap year For your program you will...
C++ questions about Monty Hall Problem, please make sure to divide the program into functions which...
C++ questions about Monty Hall Problem, please make sure to divide the program into functions which perform each major task. Imagine yourself on the set of a game show. You're given the choice of three doors. Behind one of the doors is a car you can drive home in if you guess correctly. Behind the other two doors are goats. After you initially guess the door, the host of the game show (who knows the door holding the car) opens...
C++ program to perform each of the area calculations in separate functions. Your program will take...
C++ program to perform each of the area calculations in separate functions. Your program will take in the relevant information in the main (), call the correct function that makes the calculation, return the answer to the main () and then print the answer to the screen. The program will declare a variable called “choice” of type int that is initialized to 0. The program will loop while choice is not equal to 4. In the body of the loop...
(MUST BE DONE IN C (NOT C++)) In this task, you will have to make sure...
(MUST BE DONE IN C (NOT C++)) In this task, you will have to make sure you understood the concept of “the dot” in programming. Go ahead and declare an array of characters with a default length (whichever length you want, it's fine). Next, you want to ask the user for a phrase and store the characters in your array, but before you do that, ask the user for an estimated length of the array. If the length given is...
C++ program, I'm a beginner so please make sure keep it simple Write a program to...
C++ program, I'm a beginner so please make sure keep it simple Write a program to read the input file, shown below and write out the output file shown below. Use only string objects and string functions to process the data. Do not use c-string functions or stringstream (or istringstream or ostringstream) class objects for your solution. Input File.txt: Cincinnati 27, Buffalo 24 Detroit 31, Cleveland 17 Kansas City 24, Oakland 7 Carolina 35, Minnesota 10 Pittsburgh 19, NY Jets...
Task: Write a program that creates a class Apple and a tester to make sure the...
Task: Write a program that creates a class Apple and a tester to make sure the Apple class is crisp and delicious. Instructions: First create a class called Apple  The class Apple DOES NOT HAVE a main method  Some of the attributes of Apple are o Type: A string that describes the apple. It may only be of the following types:  Red Delicious  Golden Delicious  Gala  Granny Smith o Weight: A decimal value representing...
Write a program fragment (not a complete program) which will perform the following task: The int...
Write a program fragment (not a complete program) which will perform the following task: The int variable m currently contains the number of minutes a basketball player played in their last game. Use an IF statement(s) to print out an appropriate message based on the following: If m is from 35 to 48, print the message "very tired" If m is from 10 to 34, print the message "little tired" If m is from 1 to 9, print the message...
Design a program which uses functions to sort a list and perform a binary search. Your...
Design a program which uses functions to sort a list and perform a binary search. Your program should: Iinitialize an unsorted list (using the list provided) Display the unsorted list Sort the list Display the sorted list. Set up a loop to ask the user for a name, perform a binary search, and then report if the name is in the list. Use a sentinel value to end the loop. Do not use the Python built in sort function to...
Please design a PLC program to perform the following task: An LED will be on when...
Please design a PLC program to perform the following task: An LED will be on when it’s activated by an NO push button for an accumulated 6 seconds. In other words, the push button can be on and off, but when it’s accumulated for six seconds, the LED will be on. After six seconds, the LED will be on for four seconds and is then reset itself for another cycle. Post LogixPro image of this programming Cascading timer Assume the...
Your task is to write a C program which performs encryption and decryption of a message...
Your task is to write a C program which performs encryption and decryption of a message using the substitution cipher algorithm. Write a C program which performs encryption and decryption using the substitution cipher algorithm. Your program must be fully automated (ie: it does not take any interactive user input) and either encrypt or decrypt depending on the files which exist in the program’s directory. If message.txt exists your program should read that file, encrypt the contents, and write the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT