Chapter 07 Rock-Paper-Scissors(-Lizard-Spock)

I have a program and I know there's probably a lot of mistakes so it may not be an easy fix but it's the thought that couts...

Format: Your task is to modify it to play Rock-Paper-Scissors-Lizard-Spock.

Start by playing the game a few times. Then, analyze the existing program to gain an understanding of how it works, what the functions do, and how the code is organized. After you have finished your analysis, make the appropriate changes.

Your comment block at the top of the modified program should include the information provided by the original author, with additional comments to indicate changes were made, by who, and a description of the added functionality.

The rules of Rock-Paper-Scissors-Lizard-Spock are as follows:

Scissors cuts Paper
Paper covers Rock
Rock crushes Lizard
Lizard poisons Spock
Spock smashes Scissors
Scissors decapitates Lizard
Lizard eats Paper
Paper disproves Spock
Spock vaporizes Rock, (and as it always has…)
Rock crushes Scissors
Run your program using all the combinations possible for two people playing the game. Your program should correctly determine the winner every time, including a tie-game.

Put the code you need help with here.
/*Changes made by: Jude Cerra
Changes made: Add Spock and lizard to further advance tic-tac-toe

//*************************************************************
// Author: D.S. Malik
//
// Program: Rock, Paper, and Scissors
// This program plays the game of rock, paper, and scissors.
//*************************************************************
*/
#include <iostream>

using namespace std;

enum objectType{ROCK, PAPER, SCISSORS, LIZARD, SPOCK};

//Function prototypes
void displayRules();

objectType retrievePlay(char selection);

bool validSelection(char selection);

void convertEnum(objectType object);

objectType winningObject(objectType play1, objectType play2);

void gameResult(objectType play1, objectType play2, int& winner);

void displayResults(int gCount, int wCount1, int wCount2);

int main()
{
int gameCount; //variable to store the number of games played
int winCount1; //variable to store the number of games won by player 1
int winCount2; //variable to store the number of games won by player 2
int gamewinner;
char response; //variable to get the user's response to play the game
char selection1;
char selection2;
objectType play1; //player1's selection
objectType play2; //player2's selection

//Initialize variables
gameCount = 0;
winCount1 = 0;
winCount2 = 0;

displayRules();

cout << "Enter Y/y to play the game: ";
cin >> response;
cout << endl;

while (response == 'Y' || response == 'y')
{
cout << "Player 1 enter your choice: ";
cin >> selection1;
cout << endl;

cout << "Player 2 enter your choice: ";
cin >> selection2;
cout << endl;

if (validSelection(selection1) && validSelection(selection2))
{
play1 = retrievePlay(selection1);
play2 = retrievePlay(selection2);
gameCount++;
gameResult(play1, play2, gamewinner);

if (gamewinner == 1)
winCount1++;
else if (gamewinner == 2)
winCount2++;
}//end if

cout << "Enter Y/y to play the game: ";
cin >> response;
cout << endl;
}//end while

displayResults(gameCount, winCount1, winCount2);

return 0;
}//end main

void displayRules()
{
cout << "Welcome to the game of Rock, Paper, and Scissors." << endl;
cout << "This is a game for two players. For each game, each" << endl;
cout << "player selects one of the objects, Rock, Paper or Scissors." << endl;
cout << "The rules for winning the game are: " << endl;
cout << "1. If both players selects the same object, it is a tie." << endl;
cout << "2. Rock breaks Scissors: So player who selects Rock wins." << endl;
cout << "3. Paper covers Rock: So player who selects Paper wins." << endl;
cout << "4. Scissors cuts Paper: So player who selects Scissors wins." << endl;
cout << "5. Lizard poisons Spock: So player who selects Lizard wins." << endl;
cout << "6. Spock smashes Scissors: So player who selects Spock wins." << endl;
cout << "7. Scissors decapitates Lizard: So player who selects Scissors wins." << endl;
cout << "8. Lizard eats Paper: So player who selects Lizard wins." << endl;
cout << "9. Paper disproves Spock: So player who selects Paper wins." << endl;
cout << "10. Spock vaporizes Rock: So player who selects Spock wins." << endl;
cout << "Enter R or r to select Rock, P or p to select Paper, S or s to select Scissors, L or l to select Lizard, and O or o to select Spock" << endl;
}

bool validSelection(char selection)
{
switch (selection)
{
case 'R':
case 'r':
case 'P':
case 'p':
case 'S':
case 's':
case 'L':
case 'l':
case 'O':
case 'o':
return true;
default:
return false;
}
}

objectType retrievePlay(char selection)
{
objectType object;

switch (selection)
{
case 'R':
case 'r':
object = ROCK;
break;
case 'P':
case 'p':
object = PAPER;
break;
case 'S':
case 's':
object = SCISSORS;
break;
case 'L':
case 'l':
object = LIZARD;
break;
case 'O':
case 'o':
object = SPOCK;
}

return object;
}

void convertEnum(objectType object)
{
switch (object)
{
case ROCK:
cout << "Rock";
break;
case PAPER:
cout << "Paper";
break;
case SCISSORS:
cout << "Scissors";\
break;
case LIZARD:
cout << "Lizard";
break;
case SPOCK:
cout << "Spock";
}
}

objectType winningObject(objectType play1, objectType play2)
{
if ((play1 == ROCK && play2 == SCISSORS) || (play2 == ROCK && play1 == SCISSORS))
return ROCK;
else if ((play1 == ROCK && play2 == PAPER) || (play2 == ROCK && play1 == PAPER))
return PAPER;
else
return SCISSORS;
if ((play1 == ROCK && play2 == LIZARD) || (play2 == ROCK && play1 == LIZARD))
return ROCK;
else if ((play1 == SPOCK && play2 == ROCK) || (play2 == SPOCK && play1 == ROCK))
return SPOCK;
else
return LIZARD;
if ((play1 == LIZARD && play2 == SPOCK) || (play2 == LIZARD && play1 == SPOCK))
return LIZARD;
else if ((play1 == LIZARD && play2 == PAPER) || (play2 == LIZARD && play1 == PAPER))
return LIZARD;
else
return SPOCK;
if ((play1 == SPOCK && play2 == SCISSORS) || (play2 == SPOCK && play1 == SCISSORS))
return SPOCK;
else if ((play1 == PAPER && play2 == SPOCK) || (play2 == PAPER && play1 == SPOCK))
return PAPER;
else
return SCISSORS;

}

void gameResult(objectType play1, objectType play2,
int& winner)
{
objectType winnerObject;

if (play1 == play2)
{
winner = 0;
cout << "Both players selected ";
convertEnum(play1);
cout << ". This game is a tie." << endl;
}
else
{
winnerObject = winningObject(play1, play2);

//Output each player's choice
cout << "Player 1 selected ";
convertEnum(play1);
cout << " and player 2 selected ";
convertEnum(play2);
cout << ". ";

//Decide the winner
if (play1 == winnerObject)
winner = 1;
else if (play2 == winnerObject)
winner = 2;

//Output the winner
cout << "Player " << winner << " wins this game." << endl;
}
}

void displayResults(int gCount, int wCount1, int wCount2)
{
cout << "The total number of plays: " << gCount << endl;
cout << "The number of plays won by player 1: " << wCount1 << endl;
cout << "The number of plays won by player 2: " << wCount2 << endl;
}

but it's the thought that couts...
Was that an intentional pun? If so, I approve.

Anyway, what is your question, exactly? The more precisely you can explain your issue, the more likely you are to get good feedback.
Last edited on
The important change will be in the WinningObject () function. It can be done with multiple if statements - but another method is to use a 2d look-up table with say player1 along the 'top' and player 2 down the 'sides'. Then the result of the play of player1 and player2 is given by the result in the given array element. Every element would have a value of either win, loose or draw.

@seeplus: Very elegant using a 2d look-up table.

@OP: PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.


but another method is to use a 2d look-up table with say player1 along the 'top' and player 2 down the 'sides'. Then the result of the play of player1 and player2 is given by the result in the given array element. Every element would have a value of either win, loose or draw.

Long time ago when I started learning C++11 I implemented it like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
enum Choice {PAPER, SCISSOR, ROCK, LIZARD, SPOCK, QUIT};

enum Result {PLAYER_WON, COMPUTER_WON, DRAW};

struct Rule
{
  Choice player;
  Choice comp;
  Result result;
};

const std::vector<Rule> Rules = 
{
  { PAPER, SCISSOR, COMPUTER_WON},
  { PAPER, ROCK, PLAYER_WON},
  { PAPER, PAPER, DRAW},
  { PAPER, SPOCK, PLAYER_WON},
  { PAPER, LIZARD, COMPUTER_WON},

  { ROCK, PAPER, COMPUTER_WON},
  { ROCK, SCISSOR, PLAYER_WON},
  { ROCK, ROCK, DRAW},
  { ROCK, SPOCK, COMPUTER_WON},
  { ROCK, LIZARD, PLAYER_WON},

  { SCISSOR, ROCK, COMPUTER_WON},
  { SCISSOR, PAPER, PLAYER_WON},
  { SCISSOR, SCISSOR, DRAW},
  { SCISSOR, LIZARD, PLAYER_WON},
  { SCISSOR, SPOCK, PLAYER_WON},

  { LIZARD, ROCK, COMPUTER_WON},
  { LIZARD, PAPER, PLAYER_WON},
  { LIZARD, SCISSOR, PLAYER_WON},
  { LIZARD, SPOCK, PLAYER_WON},
  { LIZARD, SPOCK, DRAW},  { LIZARD, LIZARD, DRAW},

  { SPOCK, LIZARD, COMPUTER_WON},
  { SPOCK, ROCK, PLAYER_WON},
  { SPOCK, PAPER, COMPUTER_WON},
  { SPOCK, SPOCK, DRAW},
  { SPOCK, SCISSOR, PLAYER_WON},
};

// Finding the winner:
Result getResult(Choice player, Choice comp)
{
	auto result = std::find_if(Rules.begin(), Rules.end(), [player, comp](const Rule& rule)
	{
		return rule.comp == comp && rule.player == player;
	});

	if (result == Rules.end())
		throw std::runtime_error("No result found");

	return (*result).result;
}
Last edited on
You have a duplicate entry for LIZARD vs SPOCK at line 36.
Thanks AbstractionAnon, I corrected it.
Err. No. That line should be:
 
{LIZARD, LIZARD, DRAW},


Each 'set' should have 5 unique entries.
You are right seeplus, thanks.
@thmn - Nice solution that avoids the messy if statements.
I would be inclined to add a std:string to Rule that gives a text that can be displayed.
1
2
3
4
5
6
7
8
9
10
11
12
13
struct Rule
{
  Choice player;
  Choice comp;
  Result result;
  std:string text;
};

const std::vector<Rule> Rules = 
{
  { PAPER, SCISSOR, COMPUTER_WON. "Scissors cuts Paper" },
  { PAPER, ROCK, PLAYER_WON, "Paper covers Rock" },
//  ... etc 



@AbstractionAnon,
that sounds like a good idea. Thanks.
https://github.com/PragmaticWays/cplusplus1/blob/master/rock-paper-scissors-lizard-spock-game.cpp: Here is a git link to the type of casual game you are looking to code. Hope this helps you out.
Topic archived. No new replies allowed.