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

C++ You will design a program to play a simplified version of war, using dice in

ID: 3782784 • Letter: C

Question

C++

You will design a program to play a simplified version of war, using dice instead of cards. There will be only one user, but 2 “players” for the game. The user will indicate the number of rounds to play. The user will also specify for each “player” the number of sides on the dice used and if that player is using regular or loaded dice. For example, one player could have a loaded 10-sided die while the other has a 4-sided die. No one said it had to be fair!

To play the game, for each round, you roll a die of the appropriate type for each player. The higher result wins. If the results are equal, it is a draw. The winner of the game is the player who won the most rounds. Your program will print out which player won to the user.

Die class: requires an input integer N which determines the number of sides on the individual die. It includes a method to return a random integer between 1 and N as the result of rolling the die for once.

LoadedDie class: it inherits the behavior and elements of Die, but the number it returns is biased such that the average output of rolling it for several times will be higher than for a Die object. You can determine how you want to realize the biased function.

Game class: it will implement the simple dice-rolling game. In the menu function, the user will specify the die sides used by each player (the players can have dice with different number of sides). The user will also indicate if either or both players are using loaded dice, and will enter the number of rounds in the game. The Game class will create the necessary objects, play the game, and display the results to the user. The output results should indicate the side and type (loaded or not) of die used for each player, the number of rounds won by each player (in each round, compare the rolling results, the larger number will win), and the final winner of the game (the player that win more rounds).

Explanation / Answer

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;
class Die
{
   protected:      
       int sides;
       string type;
   public:
      /*Die()
      {
          sides=6; //default 6 faces
       }*/
       Die(int n)
       {
           sides=n;
           type="Not Loaded";
       }
       int roll()
       {
           return rand()%sides+1; //returns a rando number in the range 1 - sides
       }
      
       string getType()
       {
           return type;
       }
       int getSides()
       {
           return sides;
       }
};
class LoadedDie : public Die
{
   int odd;
  
   public:
       /*LoadedDie()
       {
           LoadedDie(6);
       }*/
       LoadedDie(int n) : Die(n)
       {
         
           odd=1;
           type="Loaded";
       }
      
       /*Overriding the roll function to have a biased behaviour of returning the highest number for every alternate roll */
       int roll()
       {
           int val=odd==1?sides:Die::roll();
           odd=(odd+1)%2;
           return val;
       }
};
class Game
{
   Die *d1, *d2; //dice for the players
  
   int rolls[100][2]; //this array will hold the result of each roll for the 2 players. can hold upto 100 rounds. increase size if more is needed
      
   int rounds;
   public :
       Game()
       {
       }
       void menu()
       {
           string response;
           int faces;
           srand(time(NULL)); //calling this to make sure different set of random numbers appear every tim the program runs
          
           cout<<"Enter details for the Player 1:" <<endl;
           cout<<"How many faces ?";
           cin>>faces;
             
           //type yes for loaded
           cout<<"Play with loaded die (yes/no) ? " ;
           cin>>response;
          
      
          
           if(response.compare("yes")==0)
               d1=new LoadedDie(faces);
           else
               d1=new Die(faces);
              
              
           cout<<"Enter details for the Player 2:" <<endl;
           cout<<"How many faces ?";
           cin>>faces;
             
           //type yes for loaded
           cout<<"Play with loaded die (yes/no) ? " ;
           cin>>response;
              
      
          
           if(response.compare("yes")==0)
               d2=new LoadedDie(faces);
           else
               d2=new Die(faces);  
              
           cout << "How many rounds ?";
           cin>>rounds;
          
          
          
       }
       void play()
       {
           for(int i=0;i<rounds;i++)
           {
               rolls[i][0]=d1->roll(); //player1
               rolls[i][1]=d2->roll(); //player2
           }
       }
      
       void displayResults()
       {
           int points1=0,points2=0;
          
           cout<<"Results after "<<rounds<<" rounds "<<endl;
           cout<<"----------------------------------"<<endl;
           cout<<"Player 1 Player 2 "<<endl;
           cout<<"----------------------------------"<<endl;
           for(int i=0;i<rounds;i++)
           {
               cout<<rolls[i][0]<<" "<<rolls[i][1]<<endl;
              
               if(rolls[i][0]>rolls[i][1])
                   points1++;
               else if(rolls[i][0]<rolls[i][1])
                   points2++;
          
           }
           cout<<"Player 1 wins "<<points1<<" times using "<<d1->getType()<< " die with "<<d1->getSides()<<" faces "<<endl;
           cout<<"Player 2 wins "<<points2<<" times using "<<d2->getType()<< " die with "<<d2->getSides()<<" faces "<<endl;
           if(points1>points2)
               cout<<"Player 1 wins !";
           else if (points2>points1)
               cout<<"Player 2 wins !";
           else
               cout<<"Its a draw !!";
       }
};

int main()
{
   Game g;
   g.menu();
   g.play();
   g.displayResults();
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote