Question

In: Computer Science

C++ question. Need all cpp and header files. Part 1 - Polymorphism problem 3-1 You are...

C++ question. Need all cpp and header files.

Part 1 - Polymorphism

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.

Solutions

Expert Solution

cloud plz check the code below

#include <iostream>

#include <string>

using namespace std;

enum choices { ROCK, SPOCK, PAPER, LIZARD, SCISSORS, MX_C

};

enum indexes { PLAYER, COMPUTER, DRAW

};

class stats

{

public:

    stats() : _draw( 0 )

    {

        ZeroMemory( _moves, sizeof( _moves ) );

               ZeroMemory( _win, sizeof( _win ) );

    }

    void draw()                          

{ _draw++;

}

    void win( int p )                 

{

_win[p]++;

}

    void move( int p, int m )   { _moves[p][m]++;

}

    int getMove( int p, int m ) { return _moves[p][m];

}

    string format( int a )

    {

               char t[32];

               wsprintf( t, "%.3d", a );

               string d( t );

               return d;

    }

    void print()

    {

        string d = format( _draw ),

                      pw = format( _win[PLAYER] ),                            cw = format( _win[COMPUTER] ),

                      pr = format( _moves[PLAYER][ROCK] ),      cr=format( _moves[COMPUTER][ROCK] ),

               pp = format( _moves[PLAYER][PAPER] ),         cp=format( _moves[COMPUTER][PAPER] ),

                    ps=format( _moves[PLAYER][SCISSORS] ),cs=format( _moves[COMPUTER][SCISSORS] ),

                      pl = format( _moves[PLAYER][LIZARD] ), cl=format( _moves[COMPUTER][LIZARD] ),

                      pk = format( _moves[PLAYER][SPOCK] ),ck=format( _moves[COMPUTER][SPOCK] );

               system( "cls" );

               cout << endl;

               cout << "+----------+-------+--------+--------+---------+----------+--------+---------+" << endl;

cout << "|          | WON | DRAW | ROCK | PAPER | SCISSORS | LIZARD | SPOCK |"<< endl;

               cout << "+----------+" << endl;

               cout << "| PLAYER | " << pw << " |        |   " << pr << " |   " << pp << "   |   " << ps << "    | " << pl << "   |   " << pk << "   |" << endl;

               cout << "+----------+-------+   " << d << " +--------+---------+----------+--------+---------+" << endl;

               cout << "| COMPUTER | " << cw << " |        |   " << cr << " |   " << cp << "   |   " << cs << "    | " << cl << "   |   " << ck << "   |" << endl;

               cout << "+------+" << endl;

               cout << endl << endl;

               system( "pause" );

    }

private:

    int _moves[2][MX_C], _win[2], _draw;

};

class rps

{

private:

    int makeMove()

    {

               int total = 0, r, s;

               for( int i = 0; i < MX_C; total += statistics.getMove( PLAYER, i++ ) );

               r = rand() % total;

               for( int i = ROCK; i < SCISSORS; i++ )

               {

                   s = statistics.getMove( PLAYER, i );

                   if( r < s ) return ( i + 1 );

                   r -= s;

               }

               return ROCK;

    }

    void printMove( int p, int m )

    {

               if( p == COMPUTER ) cout << "My move: ";

               else cout << "Your move: ";

               switch( m )

               {

                   case ROCK: cout << "ROCK\n"; break;

                   case PAPER: cout << "PAPER\n"; break;

                   case SCISSORS: cout << "SCISSORS\n"; break;

                   case LIZARD: cout << "LIZARD\n"; break;

                   case SPOCK: cout << "SPOCK\n";

               }

    }

public:

    rps()

    {

checker[ROCK][ROCK] = 2;

checker[ROCK][PAPER] = 1;

checker[ROCK][SCISSORS] = 0;

checker[ROCK][LIZARD] = 0;

checker[ROCK][SPOCK] = 1;

checker[PAPER][ROCK] = 0;

checker[PAPER][PAPER] = 2;

checker[PAPER][SCISSORS] = 1;

checker[PAPER][LIZARD] = 1;

checker[PAPER][SPOCK] = 0;

checker[SCISSORS][ROCK] = 1;

checker[SCISSORS][PAPER] = 0;

checker[SCISSORS][SCISSORS] = 2;

checker[SCISSORS][LIZARD] = 0;

checker[SCISSORS][SPOCK] = 1;

checker[LIZARD][ROCK] = 1;

checker[LIZARD][PAPER] = 0;

checker[LIZARD][SCISSORS] = 1;

checker[LIZARD][LIZARD] = 2;

checker[LIZARD][SPOCK] = 0;

checker[SPOCK][ROCK] = 0;

checker[SPOCK][PAPER] = 1;

checker[SPOCK][SCISSORS] = 0;

checker[SPOCK][LIZARD] = 1;

checker[SPOCK][SPOCK] = 2;

    }

    void play()

    {

               int p, r, m;

               while( true )

               {

                   cout << "What is your move (1)ROCK (2)SPOCK (3)PAPER (4)LIZARD (5)SCISSORS (0)Quit ? ";

                   cin >> p;

                   if( !p || p < 0 ) break;

                   if( p > 0 && p < 6 )

                   {

                              p--;

                              cout << endl;

                              printMove( PLAYER, p );

                              statistics.move( PLAYER, p );

                              m = makeMove();

                              statistics.move( COMPUTER, m );

                              printMove( COMPUTER, m );

                              r = checker[p][m];

                              switch( r )

                              {

                                  case DRAW:

                                      cout << endl << "DRAW!" << endl << endl;

                                      statistics.draw();

                                  break;

                                  case COMPUTER:

                                             cout << endl << "I WIN!" << endl << endl;

                                             statistics.win( COMPUTER );

                                  break;

                                  case PLAYER:

                                             cout << endl << "YOU WIN!" << endl << endl;

                                             statistics.win( PLAYER );

                              }

                              system( "pause" );

                   }

                   system( "cls" );

               }

               statistics.print();

    }

private:

    stats statistics;

    int checker[MX_C][MX_C];

};

int main( int argc, char* argv[] )

{

    srand( GetTickCount() );

    rps game;

    game.play();

    return 0;

}


#include <iostream>
#include <string>

using namespace std;
enum choices { ROCK, SPOCK, PAPER, LIZARD, SCISSORS, MX_C
};
enum indexes { PLAYER, COMPUTER, DRAW
};

class stats
{
public:
stats() : _draw( 0 )
{
ZeroMemory( _moves, sizeof( _moves ) );
   ZeroMemory( _win, sizeof( _win ) );
}
void draw()         
{ _draw++;
}
void win( int p )  
{
_win[p]++;
}
void move( int p, int m ) { _moves[p][m]++;
}
int getMove( int p, int m ) { return _moves[p][m];
}
string format( int a )
{
   char t[32];
   wsprintf( t, "%.3d", a );
   string d( t );
   return d;
}

void print()
{
string d = format( _draw ),
   pw = format( _win[PLAYER] ),       cw = format( _win[COMPUTER] ),

   pr = format( _moves[PLAYER][ROCK] ), cr=format( _moves[COMPUTER][ROCK] ),
pp = format( _moves[PLAYER][PAPER] ), cp=format( _moves[COMPUTER][PAPER] ),
   ps=format( _moves[PLAYER][SCISSORS] ),cs=format( _moves[COMPUTER][SCISSORS] ),
   pl = format( _moves[PLAYER][LIZARD] ), cl=format( _moves[COMPUTER][LIZARD] ),
   pk = format( _moves[PLAYER][SPOCK] ),ck=format( _moves[COMPUTER][SPOCK] );

   system( "cls" );
   cout << endl;
   cout << "+----------+-------+--------+--------+---------+----------+--------+---------+" << endl;
cout << "| | WON | DRAW | ROCK | PAPER | SCISSORS | LIZARD | SPOCK |"<< endl;
   cout << "+----------+" << endl;
   cout << "| PLAYER | " << pw << " | | " << pr << " | " << pp << " | " << ps << " | " << pl << " | " << pk << " |" << endl;
   cout << "+----------+-------+ " << d << " +--------+---------+----------+--------+---------+" << endl;
   cout << "| COMPUTER | " << cw << " | | " << cr << " | " << cp << " | " << cs << " | " << cl << " | " << ck << " |" << endl;
   cout << "+------+" << endl;
   cout << endl << endl;

   system( "pause" );

}

private:
int _moves[2][MX_C], _win[2], _draw;
};
class rps
{
private:
int makeMove()
{
   int total = 0, r, s;
   for( int i = 0; i < MX_C; total += statistics.getMove( PLAYER, i++ ) );
   r = rand() % total;

   for( int i = ROCK; i < SCISSORS; i++ )
   {
   s = statistics.getMove( PLAYER, i );
   if( r < s ) return ( i + 1 );
   r -= s;
   }
   return ROCK;
}
void printMove( int p, int m )
{
   if( p == COMPUTER ) cout << "My move: ";
   else cout << "Your move: ";
   switch( m )
   {
   case ROCK: cout << "ROCK\n"; break;
   case PAPER: cout << "PAPER\n"; break;
   case SCISSORS: cout << "SCISSORS\n"; break;
   case LIZARD: cout << "LIZARD\n"; break;
   case SPOCK: cout << "SPOCK\n";
   }
}

public:
rps()
{
checker[ROCK][ROCK] = 2;
checker[ROCK][PAPER] = 1;
checker[ROCK][SCISSORS] = 0;
checker[ROCK][LIZARD] = 0;
checker[ROCK][SPOCK] = 1;
checker[PAPER][ROCK] = 0;
checker[PAPER][PAPER] = 2;
checker[PAPER][SCISSORS] = 1;
checker[PAPER][LIZARD] = 1;
checker[PAPER][SPOCK] = 0;
checker[SCISSORS][ROCK] = 1;
checker[SCISSORS][PAPER] = 0;
checker[SCISSORS][SCISSORS] = 2;
checker[SCISSORS][LIZARD] = 0;
checker[SCISSORS][SPOCK] = 1;
checker[LIZARD][ROCK] = 1;
checker[LIZARD][PAPER] = 0;
checker[LIZARD][SCISSORS] = 1;
checker[LIZARD][LIZARD] = 2;
checker[LIZARD][SPOCK] = 0;
checker[SPOCK][ROCK] = 0;
checker[SPOCK][PAPER] = 1;
checker[SPOCK][SCISSORS] = 0;
checker[SPOCK][LIZARD] = 1;
checker[SPOCK][SPOCK] = 2;
}
void play()
{
   int p, r, m;
   while( true )
   {
   cout << "What is your move (1)ROCK (2)SPOCK (3)PAPER (4)LIZARD (5)SCISSORS (0)Quit ? ";
   cin >> p;
   if( !p || p < 0 ) break;
   if( p > 0 && p < 6 )
   {
       p--;
       cout << endl;
       printMove( PLAYER, p );
       statistics.move( PLAYER, p );

       m = makeMove();
       statistics.move( COMPUTER, m );
       printMove( COMPUTER, m );

       r = checker[p][m];
       switch( r )
       {
       case DRAW:
       cout << endl << "DRAW!" << endl << endl;
       statistics.draw();
       break;
       case COMPUTER:
           cout << endl << "I WIN!" << endl << endl;
           statistics.win( COMPUTER );
       break;
       case PLAYER:
           cout << endl << "YOU WIN!" << endl << endl;
           statistics.win( PLAYER );
       }
       system( "pause" );
   }
   system( "cls" );
   }
   statistics.print();
}
private:
stats statistics;
int checker[MX_C][MX_C];
};
int main( int argc, char* argv[] )
{
srand( GetTickCount() );
rps game;
game.play();
return 0;
}


plz understand the above if you queries let me know i will help you in that


Related Solutions

Complete the following task in C++. Separate your class into header and cpp files. You can...
Complete the following task in C++. Separate your class into header and cpp files. You can only useiostream, string and sstream. Create a main.cpp file to test your code thoroughly. Given : commodity.h and commodity.cpp here -> https://www.chegg.com/homework-help/questions-and-answers/31-commodity-request-presented-two-diagrams-depicting-commodity-request-classes-form-basic-q39578118?trackid=uF_YZqoK Create : locomotive.h, locomotive.cpp, main.cpp Locomotive Class Hierarchy Presented here will be a class diagram depicting the nature of the class hierarchy formed between a parent locomotive class and its children, the two kinds of specic trains operated. The relationship is a...
Complete the following task in C++. Separate your class into header and cpp files. You can...
Complete the following task in C++. Separate your class into header and cpp files. You can only use iostream, string and sstream. Create a main.cpp file to test your code thoroughly. Given : commodity.h and commodity.cpp here -> https://www.chegg.com/homework-help/questions-and-answers/31-commodity-request-presented-two-diagrams-depicting-commodity-request-classes-form-basic-q39578118?trackid=uF_YZqoK Given : locomotive.h, locomotive.cpp, main.cpp -> https://www.chegg.com/homework-help/questions-and-answers/complete-following-task-c--separate-class-header-cpp-files-useiostream-string-sstream-crea-q39733428 Create : DieselElectric.cpp DieselElectric.h DieselElectric dieselElectric -fuelSupply:int --------------------------- +getSupply():int +setSupply(s:int):void +dieselElectric(f:int) +~dieselElectric() +generateID():string +calculateRange():double The class variables are as follows: fuelSuppply: The fuel supply of the train in terms of a number of kilolitres...
WRITE ONLY THE TO DO'S   in FINAL program IN C++ (necessary cpp and header files are...
WRITE ONLY THE TO DO'S   in FINAL program IN C++ (necessary cpp and header files are witten below) "KINGSOM.h " program below: #ifndef WESTEROS_kINGDOM_H_INCLUDED #define WESTEROS_KINGDOM_H_INCLUDED #include <iostream> namespace westeros { class Kingdom{         public:                 char m_name[32];                 int m_population; };         void display(Kingdom&);                      } #endif } Kingdom.cpp Program below: #include <iostream> #include "kingdom.h" using namespace std; namespace westeros {         void display(Kingdom& pKingdom) {                 cout << pKingdom.m_name << ", population " << pKingdom.m_population << endl;                                                               FINAL:...
Write the header and the implementation files (.h and .cpp separatelu) for a class called Course,...
Write the header and the implementation files (.h and .cpp separatelu) for a class called Course, and a simple program to test it, according to the following specifications:                    Your class has 3 member data: an integer pointer that will be used to create a dynamic variable to represent the number of students, an integer pointer that will be used to create a dynamic array representing students’ ids, and a double pointer that will be used to create a dynamic array...
Write the header and the implementation files (.h and .cpp separately) for a class called Course,...
Write the header and the implementation files (.h and .cpp separately) for a class called Course, and a simple program to test it (C++), according to the following specifications:                    Your class has 3 member data: an integer pointer that will be used to create a dynamic variable to represent the number of students, an integer pointer that will be used to create a dynamic array representing students’ ids, and a double pointer that will be used to create a dynamic...
i need an example of a program that uses muliple .h and .cpp files in c++...
i need an example of a program that uses muliple .h and .cpp files in c++ if possible.
Using C++ Create the UML, the header, the cpp, and the test file for an ArtWork...
Using C++ Create the UML, the header, the cpp, and the test file for an ArtWork class. The features of an ArtWork are: Artist name (e.g. Vincent vanGogh or Stan Getz) Medium (e.g. oil painting or music composition) Name of piece (e.g. Starry Night or Late night blues) Year (e.g. 1837 or 1958)
Write C++ program (submit the .cpp,.h, .sln and .vcxproj files) Problem 1. Generate 100 random numbers...
Write C++ program (submit the .cpp,.h, .sln and .vcxproj files) Problem 1. Generate 100 random numbers of the values 1-20 in an input.txt. Now create a binary search tree using the numbers of the sequence. The tree set should not have any nodes with same values and all repeated numbers of the random sequence must be stored in the node as a counter variable. For example, if there are five 20s’ in the random sequence then the tree node having...
How do you use header files on a program? I need to separate my program into...
How do you use header files on a program? I need to separate my program into header files/need to use header files for this program.Their needs to be 2-3 files one of which is the menu. thanks! #include #include #include using namespace std; const int maxrecs = 5; struct Teletype { string name; string phoneNo; Teletype *nextaddr; }; void display(Teletype *); void populate(Teletype *); void modify(Teletype *head, string name); void insertAtMid(Teletype *, string, string); void deleteAtMid(Teletype *, string); int find(Teletype...
Please write code in C++ and include all header files used not bits/stdc: (Same number subsequence)...
Please write code in C++ and include all header files used not bits/stdc: (Same number subsequence) Write an O(n) program that prompts the user to enter a sequence of integers ending with 0 and finds longest subsequence with the same number. Sample Run Enter a series of numbers ending with 0: 2 4 4 8 8 8 8 2 4 4 0 The longest same number sequence starts at index 3 with 4 values of 8
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT