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

RPS Enumeration // it must be in c++ Use the given enumerations to write a funct

ID: 642779 • Letter: R

Question

RPS Enumeration // it must be in c++

Use the given enumerations to write a function Result getResult(Choice player1, Choice player2) . It should determine the winner of a round of Rock, Paper, Scissors between two players (Rock beats Scissors, Scissors beats Paper and Paper beats Rock). Return WIN if player1 wins, LOSE if player1 loses or DRAW if there is no winner

// use the following code

#include <iostream>

using namespace std;

//Lose =-1, Draw = 0, Win = 1
enum Result { LOSE = -1, DRAW, WIN };

enum Choice { ROCK, PAPER, SCISSORS };
//Do not modify anything on or above the line below this
//START_PROVIDED

    Result getResult(Choice player1, Choice player2) {
       
        Choice player1 = ROCK;
        Choice player2 = PAPER;
        Result = LOSE;
       
    }

//END_PROVIDED
//Do not modify anything on or below the line above this
int main()
{
Result result1 = getResult( ROCK, PAPER );
cout << result1 << endl;
Result result2 = getResult( ROCK, SCISSORS );
cout << result2 << endl;
Result result3 = getResult( ROCK, ROCK );
cout << result3 << endl;
Result result4 = getResult( PAPER, PAPER );
cout << result4 << endl;
Result result5 = getResult( PAPER, SCISSORS );
cout << result5 << endl;
Result result6 = getResult( PAPER, ROCK );
cout << result6 << endl;
Result result7 = getResult( SCISSORS, PAPER );
cout << result7 << endl;
Result result8 = getResult( SCISSORS, SCISSORS );
cout << result8 << endl;
Result result9 = getResult( SCISSORS, ROCK );
cout << result9 << endl;
}

Explanation / Answer

Program Code:

The code added is highlighted in bold letters.

// use the following code

#include <iostream>

using namespace std;

//Lose =-1, Draw = 0, Win = 1

enum Result { LOSE = -1, DRAW, WIN };

enum Choice { ROCK, PAPER, SCISSORS };

//Do not modify anything on or above the line below this

//START_PROVIDED

Result getResult(Choice player1, Choice player2)

{

     Result r;

     Choice play1 = player1;

     Choice play2 = player2;

     if(player1< player2)

          r= LOSE;

     else if(player1>player2)

          r = WIN;

     else

          r = DRAW;

     return r;

}

//END_PROVIDED

//Do not modify anything on or below the line above this

int main()

{

     Result result1 = getResult( ROCK, PAPER );

     cout << result1 << endl;

     Result result2 = getResult( ROCK, SCISSORS );

     cout << result2 << endl;

     Result result3 = getResult( ROCK, ROCK );

     cout << result3 << endl;

     Result result4 = getResult( PAPER, PAPER );

     cout << result4 << endl;

     Result result5 = getResult( PAPER, SCISSORS );

     cout << result5 << endl;

     Result result6 = getResult( PAPER, ROCK );

     cout << result6 << endl;

     Result result7 = getResult( SCISSORS, PAPER );

     cout << result7 << endl;

     Result result8 = getResult( SCISSORS, SCISSORS );

     cout << result8 << endl;

     Result result9 = getResult( SCISSORS, ROCK );

     cout << result9 << endl;

     system("pause");

     return 0;

}

Note:

In the above code, you have been assigning the values to the enumeration type directly. So, it is unable to handle the value.

Just create a reference to the enumeration Result and store the values respectively.

Similarly, for the enumeration Choice, need to create a reference and then store the values of parameters.

Thus, by adding the code, the output produced is,

Sample output:

-1

-1

0

0

-1

1

1

0

1

Press any key to continue . . .