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

The program compiles, runs, produces the correct results and the program contain

ID: 3871829 • Letter: T

Question

The program compiles, runs, produces the correct results and the program contains no if statements, only switch statements
Basically how do I Replace Paper Rock Scissors if statements and compound conditions with switch statements with what I have already
Please help and thank you in advance


// RockPaperScissors

//

#include <ctime>

#include <iostream>

#include <random>

#include <string>

using namespace std;

int main()

{

const int paper = 0, rock = 1, scissors = 2;

const string explanationPR = " wins because paper covers rock";

const string explanationRS = " wins because rock breaks scissors";

const string explanationSP = " wins because scissors cut paper";

// default_random_engine e(static_cast<unsigned>(time(nullptr)));

default_random_engine e;

uniform_int_distribution<int> u(paper, scissors);

top:

int choiceA = u(e), choiceB = u(e);

const string nameA = "Ala", nameB = "Bel";

cout << nameA << " chooses ";

if (choiceA == paper)

cout << "paper";

else if (choiceA == rock)

cout << "rock";

else

cout << "scissors";

cout << endl;

cout << nameB << " chooses ";

if (choiceB == paper)

cout << "paper";

else if (choiceB == rock)

cout << "rock";

else

cout << "scissors";

cout << endl;

if (choiceA == choiceB)

cout << "Tie game";

else if (choiceA == paper && choiceB == rock)

cout << nameA << explanationPR << endl;

else if (choiceA == paper && choiceB == scissors)

cout << nameB << explanationSP << endl;

else if (choiceA == rock && choiceB == paper)

cout << nameB << explanationPR << endl;

else if (choiceA == rock && choiceB == scissors)

cout << nameA << explanationRS << endl;

else if (choiceA == scissors && choiceB == paper)

cout << nameA << explanationSP << endl;

else if (choiceA == scissors && choiceB == rock)

cout << nameB << explanationRS << endl;

else

cout << "something is wrong";

cout << endl;

system("pause");

return 0;

}

Explanation / Answer


Modified c++ code which uses swich case.
#include <ctime>
#include <iostream>
#include <random>
#include <string>
using namespace std;

int main()
{
const int paper = 0, rock = 1, scissors = 2;
const string explanationPR = " wins because paper covers rock";
const string explanationRS = " wins because rock breaks scissors";
const string explanationSP = " wins because scissors cut paper";

// default_random_engine e(static_cast<unsigned>(time(nullptr)));
default_random_engine e;
uniform_int_distribution<int> u(paper, scissors);
top:
int choiceA = u(e), choiceB = u(e);
const string nameA = "Ala", nameB = "Bel";
cout << nameA << " chooses ";
switch(choiceA){
case paper: cout << "paper";
break;
case rock:cout << "rock";
break;
case scissors: cout << "scissors";
break;
}
cout << endl;
cout << nameB << " chooses ";
switch(choiceB){
case paper: cout << "paper";
break;
case rock:cout << "rock";
break;
case scissors: cout << "scissors";
break;
}
cout << endl;

switch(choiceA) {
case paper: switch(choiceB){
case rock: cout << nameA << explanationPR << endl;
case scissors: cout << nameB << explanationSP << endl;
break;
}
case rock: switch(choiceB){
case paper: cout << nameB << explanationSP << endl;
case scissors: cout << nameA << explanationRS << endl;
break;
}
case scissors: switch(choiceB){
case paper: cout << nameA << explanationSP << endl;
case rock: cout << nameB << explanationRS << endl;
break;
}
case choiceB:
cout << "Tie game";
default:
cout << "something is wrong";
cout << endl;
break;
}

system("pause");
return 0;
}