Answer to Question #303157 in C++ for Marie

Question #303157

How to implement this using vector



#ifndef __ROCKPAPERSCISSOR_H__


#define __ROCKPAPERSCISSOR_H__



enum ObjectType { ROCK, PAPER, SCISSOR };



void displayRules();



ObjectType retrievePlay(char selection);



bool validSelection(char selection);



void convertEnum(ObjectType object);



ObjectType winningObject(ObjectType player, ObjectType computer);



void gameResult(ObjectType player, ObjectType computer, int &winner);



void displayResults(int g_count, int w_count1, int w_count2);



#endif



1
Expert's answer
2022-02-27T05:03:46-0500
#ifndef __ROCKPAPERSCISSOR_H__
#define __ROCKPAPERSCISSOR_H__

enum ObjectType { 
  ROCK,
  PAPER,
  SCISSOR
};

void displayRules() {
  cout << "  Welcome to the game of Rock, Paper, "
       << "and Scissors." << endl;
  cout << "This is a game for two players. The second "
       << "player is the computer." << endl;
  cout << "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 << endl;
  cout << "Enter R or r to select Rock, P or p to select "
	   << "Paper, and S or s to select Scissors." << endl;
}

bool validSelection(char selection) {
  switch (selection) {
    case 'R':
    case 'r':
    case 'P':
    case 'p':
    case 'S':
    case 's':
    case '1':
    case '2':
    case '3':
      return true;
    default:
      return false;
  }
}

ObjectType retrievePlay(char selection) {
  ObjectType object;
  switch (selection) {
    case 'R':
    case 'r':
    case '1':
      object = ROCK;
      break;
    case 'P':
    case 'p':
    case '2':
      object PAPER;
      break;
    case 'S':
    case 's':
    case '3':
      object = SCISSORS;
      break;
  }
  return object;
}

void convertEnum(ObjectType object) {
  switch (object) {
    case ROCK:
      cout << "Rock";
      break;
    case PAPER:
      cout << "Paper";
      break;
    case SCISSORS:
      cout << "Scissors";
  }
}

ObjectType winningObject(ObjectType player, ObjectType computer) {
  if ((player == ROCK && computer == SCISSORS) 
     || (player == SCISSORS && customer == ROCK))
    return ROCK;
  else if ((player == ROCK && computer == PAPER) 
     || (player == PAPER && computer == ROCK))
    return PAPER;
  else 
    return SCISSORS;
}

void gameResult(ObjectType player, ObjectType computer, int &winner) {
  ObjectType winnerObject;
  if (player == computer) {
    winner = 0;
    cout << "Both players selected "
         << convertEnum(player)
         << ". This game is a tie.\n";
  } else {
    winnerObject = winningObject(player, computer);

    // Output each player's choice
    cout << "Player 1 selected "
         << convertEnum(player)
         << " and player 2 selected "
         << convertEnum(computer)
         << ".";
    
    // Decide the winner
    if (player == winnerObject)
      winner = 1;
    else if (computer == winnerObject)
      winner = 2;

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

void displayResults(int g_count, int w_count1, int w_count2) {
  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;
}

#endif

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog