Question

In: Computer Science

problem 3-1 You are going to build a C++ program which runs a single game of...

problem 3-1

You are going to build a C++ program which runs a single game of Rock, Paper, Scissors. Two players (a human player and a computer player) will compete and individually choose Rock, Paper, or Scissors. They will then simultaneously declare their choices and the winner is determined by comparing the players’ choices. Rock beats Scissors. Scissors beats Paper. Paper beats Rock.

The learning objectives of this task is to help develop your understanding of abstract classes, inheritance, and polymorphism.

Your task is to produce a set of classes that will allow a human player to type instructions from the keyboard and interact with a computer player.

Your submission needs to contain the following files, along with their header files:

  • main-3-1.cpp
  • Player.cpp
  • Person.cpp
  • Computer.cpp

Part 1: Abstract Classes
Define and implement an abstract class named Player that has the following behaviours:

void move();
string getMoves();
char getMove(); //returns the most recent move made
bool win(Player * opponent); //compares players’ moves to see who wins

Declare the move() and getMoves() functions as pure virtual and set proper access modifiers for the attributes and methods.

If no one wins, the game should output “draw! go again”, and the game continues until a winner is determined.

Part 2: Polymorphism

Computer Class:

Define and implement a class named Computer that inherits from Player. By default, Computer will use Rock for every turn. If it is constructed with another value (Paper or Scissors), it will instead make that move every turn.

The Computer class has the following constructor and behaviours:

Computer(string letter); //set what move the computer will
//make (rock, paper, or scissors)
//if the input is not r, R, p, P, s, S or
//a string starting with one of these letters,
//set the move to the default ‘r’

string getMoves(); //returns all moves stored in a string

void move(); //increments number of moves made

To explain, if the computer was constructed with Computer(‘s’), and it made 3 moves, getMoves() should return:

sss
For advice about testing, please use the debugging manual (Links to an external site.).

Person Class:

Define and implement a class named Person that inherits from Player. The Person can choose Rock, Paper, or Scissors based on the user’s input.

The Player class has the following behaviours:

void move(); //allow user to type in a single character to
//represent their move. If a move is impossible,
//“Move unavailable” is outputted and the user is
//asked to input a character again.
//Otherwise, their input is stored

string getMoves();   //returns all moves stored in a string

Write a main function that uses Computer and Person to play Rock, Paper, Scissors. The Computer can be made with either constructors, but should set the default move to ‘r’. The player should be asked to input a move which is then compared against the computer’s move to determine who wins.

All the Player’s previous moves should be outputted, followed by all the Computer’s moves outputted on a new line.

Example Test Cases

Solutions

Expert Solution

Source Code:



#include <iostream>
using namespace std;


class humanClass{
public:
   char* moves;
   int totalMoves;
   int noOfMove;
   humanClass(){
       cout << "Enter your input: ";
       cin >> totalMoves;
       moves = new char[totalMoves];
       for( int i = 0; i < totalMoves; i++ ){
           cin >> moves[i];
       }
       noOfMove = 0;
   }

   char move(){
       if( noOfMove >= totalMoves ){
           cout << "Error" << endl;
           return '-';
       }
       char returnV = moves[noOfMove];
       noOfMove = noOfMove + 1;
       return returnV;
   }
};

class computerClass{
public:
   char move(){
       return 'R';
   }
};

class refereeClass{
public:
   void competition( humanClass H, computerClass C ){
       int totalMoves = H.totalMoves;
       for(int i = 0; i < totalMoves; i++){
           char winner = 'T';
           char humanMove = H.move();
           char compMove = C.move();
           if( humanMove=='R'){
               if(compMove=='S'){
                   winner='W'; }
               if(compMove=='P'){
                   winner='L';
               }
           }
           if( humanMove=='S'){
               if(compMove=='R'){
                   winner='L'; }
               if(compMove=='P'){
                   winner='W';
               }
           }
           if( humanMove=='P'){
               if(compMove=='S'){
                   winner='L'; }
               if(compMove=='R'){
                   winner='W';
               }
           }
           cout << winner << " ";          
       }
       cout << endl;
   }
};


int main(){
   humanClass H;
   computerClass C;
   refereeClass R;

   R.competition( H, C );

   return 0;
}

Note: If you have any related doubts, queries, feel free to ask by commenting down below.

And if my answer suffice your requirements, then kindly upvote.

Happy Learning


Related Solutions

C Program and pseudocode for this problem. Write a C program that plays the game of...
C Program and pseudocode for this problem. Write a C program that plays the game of "Guess the number" as the following: Your program choose the number to be guessed by selecting an integer at random in the rang of 1 to 1000. The program then asks the use to guess the number. If the player's guess is incorrect, your program should loop until the player finally gets the number right. Your program keeps telling the player "Too High" or...
In this python program , you are to build a trivia game. The game should present...
In this python program , you are to build a trivia game. The game should present each question – either in order or randomly – to the player, and display up to four possible answers. The player is to input what they believe to be the correct answer.   The game will tell the player if they got it right or wrong and will display their score. If they got it right, their score will go up. If they got it...
For this problem, you will write a program in which the computer plays a dice game...
For this problem, you will write a program in which the computer plays a dice game called Craps. You might need to explore some Python documentation on random() for this homework assignment. PROVIDE A GRAPHICAL FLOWCHART as part of your submission for this solution Rules: The player rolls two six-sided dice. After rolling, the sum of what the dice show is calculated. If the sum is 7 or 11 on the first roll, the player wins. If the sum is...
Code in C++ please You are going to write a program for Computer test which will...
Code in C++ please You are going to write a program for Computer test which will read 10 multiple choice questions from a file, order them randomly and provide the test to the user. When the user done the program must give the user his final score
Programming Language: C++ Overview For this assignment, write a program that will simulate a single game...
Programming Language: C++ Overview For this assignment, write a program that will simulate a single game of Craps. Craps is a game of chance where a player (the shooter) will roll 2 six-sided dice. The sum of the dice will determine whether the player (and anyone that has placed a bet) wins immediately, loses immediately, or if the game continues. If the sum of the first roll of the dice is equal to 7 or 11, the player wins immediately....
Design a c++ program to simulate the BlackJack game. Rules: 1) single deck, 52 cards -both...
Design a c++ program to simulate the BlackJack game. Rules: 1) single deck, 52 cards -both player and dealer are taking cards off the same deck (the same 52 values). 2) display BOTH cards of the players, and ONE card of the computer. 3) Check for blackjack (starting value of 21 for computer or player) -if either side has a blackjack end of the game, next hand of blackjack 4) Computer must hit if 16 or below. 5) Computer must...
Tunnel Problem You are going to build a tunnel 10 miles long with a shaft at...
Tunnel Problem You are going to build a tunnel 10 miles long with a shaft at one end and a portal at the other. It is part of the first phase of the gateway tunnel to New York. * The shaft is 185 ft. deep to the invert of the tunnel and 22 ft. excavated diameter. * The tunnel is 26 ft. excavated diameter in competent sandstone and limestone. * The profile of the shaft is as follows: From the...
Build a circuit for arcade game. You have 3 LEDS and 1 button. the LEDs should...
Build a circuit for arcade game. You have 3 LEDS and 1 button. the LEDs should recycle through 000,100,110,111,011, 001(back to 000). The user needs to hit the button during the 111 cycle 3 times in a row to win. The win is shown as an LED which is HIGH if the user won the last attempt of 3 presses. The win LED will be LOW otherwise
in C++ For this program, you are going to implement a stack using an array and...
in C++ For this program, you are going to implement a stack using an array and dynamic memory allocation. A stack is a special type of data structure that takes in values (in our case integers) one at a time and processes them in a special order. Specifically, a stack is what's called a first-in-last-out (FILO) data structure. That is to say, the first integer inserted into the stack is the last value to be processed. The last value in...
in c++ please In this program you are going to have several files to turn in...
in c++ please In this program you are going to have several files to turn in (NOT JUST ONE!!) hangman.h – this is your header file – I will give you a partially complete header file to start with. hangman.cpp – this is your source file that contains your main function functions.cpp – this is your source file that contains all your other functions wordBank.txt – this is the file with words for the game to use.  You should put 10...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT