I am working on an assignment for a game of dice with two player. There are two
ID: 3592220 • Letter: I
Question
I am working on an assignment for a game of dice with two player. There are two classes of dice: Die and LoadedDie. LoadedDie must inherit from Die. The LoadedDie should return a higher average number. My problem appears to be that the LoadedDie class function rollDie does not get called and the score for player 2 is erroneous no matter where it is regular Die or a LoadedDie. Here is the code.
#ifndef DICE_HPP
#define DICE_HPP
class Die
{
public:
int N; // N represents the number of sides on the die
public:
Die();
Die(int N );
virtual int rollDie() const;
};
#endif
#include "Dice.hpp"
#include <iostream>
#include <cstdlib>
using std::cout;
using std::endl;
Die::Die()
{
N = 6;
}
Die::Die(int numSides)
{
cout << "Dice is running." << endl;
N = numSides;
}
int Die::rollDie() const
{
cout << "the Die random number is " << rand() % N + 1 << endl;
return rand() % N + 1; //randon number
}
#ifndef LOADEDDIE_HPP
#define LOADEDDIE_HPP
#include "Dice.hpp"
class LoadedDie : public Die
{
public:
LoadedDie();
LoadedDie(int);
int rollDie(int)const; //override base class function
~LoadedDie();
};
#endif
#include "LoadedDie.hpp"
#include <iostream>
#include <cstdlib>
#include <ctime>
using std::cout;
using std::endl;
LoadedDie::LoadedDie()
{
N = 6;
srand(time(NULL) ) ;
}
LoadedDie::LoadedDie(int sides ) : Die( N )
{
cout << "LoadedDie is running." << endl;
N = sides;
//this->N = N;
srand(time(NULL));
}
/***********************************************************************************
* LoadedDie::rollDie
* This function simulates a roll of the LoadedDie.
* ********************************************************************************/
int LoadedDie::rollDie(int N ) const
{
if(rand() %2)
{
cout << "Loaded Die if value is " << N << endl;
return N;
}
else
{
cout << "Loaded Due else value is " << (rand() % N ) + 1 << endl;
return (rand() % N) + 1;
}
//return rand() % N + 1 ; // random number
}
Here is the part of the code where I use the classes and their functions.
Die *player1;
player1 = NULL;
Die *player2;
player2 = NULL;
if( player1DieType == 1)
{
player1 = new Die(player1numSides);
}
else if ( player1DieType == 2)
{
player1 = new LoadedDie(player1numSides);
}
if ( player2DieType == 1)
{
player2 = new Die(player2numSides);
}
else if( player2DieType == 2)
{
player2 = new LoadedDie(player2numSides);
}
for (int i = 0; i < turns; i++ )
{
player1score = player1->rollDie();
player2score = player2->rollDie();
player1Total += player1score;
player2Total += player2score;
cout << "Player 1 rolled a " << player1score << endl;
cout << "Player 2 rolled a " << player2score << endl;
cout << "Player 1 total score " << player1Total << endl;
cout << "Player 2 total score " << player2Total << endl;
}
if (player1Total > player2Total)
{
cout << "Player 1 wins. Final score: Player 1: " << player1Total << " Player 2: " << player2Total << endl;
}
else if (player1Total < player2Total)
{
cout << "Player 2 wins. Final score: Player 2: " << player2Total << " Player 1: " << player1Total << endl;
}
else if (player1Total == player2Total)
{
cout << "It is a tie. Final score: Player 1: " << player1Total << " Player 2: " << player2Total << endl;
}
}
Explanation / Answer
In the LoadedDie header file specified as Pubic Dice instead of Die.
// header file Die.h
#ifndef DIE_H
#define DIE_H
class Die
{
public:
int N; // N represents the number of sides on the die
public:
Die();
Die(int N );
virtual int rollDie() const;
};
#endif /* DICE_H */
Die.cpp
#include "Die.h"
#include <iostream>
#include <cstdlib>
using std::cout;
using std::endl;
Die::Die()
{
N = 6;
}
Die::Die(int numSides)
{
cout << "Dice is running." << endl;
N = numSides;
}
int Die::rollDie() const
{
cout << "the Die random number is " << rand() % N + 1 << endl;
return rand() % N + 1; //random number
}
//LoadedDie.h
#ifndef LOADEDDIE_H
#define LOADEDDIE_H
#include "Die.h"
class LoadedDie : public Die
{
public:
LoadedDie();
LoadedDie(int);
int rollDie(int)const; //override base class function
~LoadedDie();
};
#endif /* LOADEDDIE_H */
//LoadedDie.cpp
#include "LoadedDie.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
using std::cout;
using std::endl;
LoadedDie::LoadedDie()
{
N = 6;
srand(time(NULL) ) ;
}
LoadedDie::LoadedDie(int sides ) : Die( N )
{
cout << "LoadedDie is running." << endl;
N = sides;
//this->N = N;
srand(time(NULL));
}
int LoadedDie::rollDie(int N ) const
{
if(rand() %2)
{
cout << "Loaded Die if value is " << N << endl;
return N;
}
else
{
cout << "Loaded Due else value is " << (rand() % N ) + 1 << endl;
return (rand() % N) + 1;
}
//return rand() % N + 1 ; // random number
}
//driver-main.cpp
#include <cstdlib>
# include "Die.h"
# include "LoadedDie.h"
#include<iostream>
#include<iomanip>
using namespace std;
int main(int argc, char** argv) {
int player1numSides,player2numSides,player1DieType,player2DieType;
int turns;
int player1score =0,player2score=0;
int player1Total = 0,player2Total=0;
Die *player1;
player1 = NULL;
Die *player2;
player2 = NULL;
cout<<" Enter the Number of sides of die for player1 :";
cin>>player1numSides;
cout<<" Enter the Number of sides of die for player2 :";
cin>>player2numSides;
cout<<" Enter the Die Type for player1 :";
cin>>player1DieType;
cout<<" Enter the Die Type for player2 :";
cin>>player2DieType;
cout<<" Enter the number of turns :";
cin>>turns;
if( player1DieType == 1)
{
player1 = new Die(player1numSides);
}
else if ( player1DieType == 2)
{
player1 = new LoadedDie(player1numSides);
}
if ( player2DieType == 1)
{
player2 = new Die(player2numSides);
}
else if( player2DieType == 2)
{
player2 = new LoadedDie(player2numSides);
}
for (int i = 0; i < turns; i++ )
{
player1score = player1->rollDie();
player2score = player2->rollDie();
player1Total += player1score;
player2Total += player2score;
cout << "Player 1 rolled a " << player1score << endl;
cout << "Player 2 rolled a " << player2score << endl;
cout << "Player 1 total score " << player1Total << endl;
cout << "Player 2 total score " << player2Total << endl;
}
if (player1Total > player2Total)
{
cout << "Player 1 wins. Final score: Player 1: " << player1Total << " Player 2: " << player2Total << endl;
}
else if (player1Total < player2Total)
{
cout << "Player 2 wins. Final score: Player 2: " << player2Total << " Player 1: " << player1Total << endl;
}
else if (player1Total == player2Total)
{
cout << "It is a tie. Final score: Player 1: " << player1Total << " Player 2: " << player2Total << endl;
}
return 0;
}
Sample Run:
Enter the Number of sides of die for player1 :6
Enter the Number of sides of die for player2 :6
Enter the Die Type for player1 :2
Enter the Die Type for player2 :2
Enter the number of turns :4
Dice is running.
LoadedDie is running.
Dice is running.
LoadedDie is running.
the Die random number is 2
the Die random number is 2
Player 1 rolled a 6
Player 2 rolled a 2
Player 1 total score 6
Player 2 total score 2
the Die random number is 2
the Die random number is 6
Player 1 rolled a 1
Player 2 rolled a 2
Player 1 total score 7
Player 2 total score 4
the Die random number is 6
the Die random number is 5
Player 1 rolled a 5
Player 2 rolled a 6
Player 1 total score 12
Player 2 total score 10
the Die random number is 3
the Die random number is 5
Player 1 rolled a 6
Player 2 rolled a 4
Player 1 total score 18
Player 2 total score 14
Player 1 wins. Final score: Player 1: 18 Player 2: 14
RUN SUCCESSFUL (total time: 8s)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.