//I\'ve included the header file and a .cc file.The .cc is what I need help with
ID: 3612061 • Letter: #
Question
//I've included the header file and a .cc file.The .cc is what I need help with please.#ifndef DIE_H_
#define DIE_H_
#include <fstream>
using namespace std;
class Die
{
private:
int num; //the number of the die, [2-12]
/*
* The minimum die number.
*/
const static int MIN = 2;
/*
* The maximim die number.
*/
const static int MAX = 12;
/*
* The number of times this die has been rolled.e
*/
int roll_count;
public:
Die(); //contructs an initial die (has a default number)
void roll(); //rolls this die.
int get_value() const; //returns the current value of the die.
/*
* Inserts the current value of the die into os.
*/
friend ostream& operator<<(ostream& os, Die die);
};
#endif /*DIE_H_*/
#include "die.h"
#include <ctime> //for time()
#include <cstdlib> //for rand() and srand()
using namespace std;
Die::Die()
{
/* TODO: Initialize the die. */
/* TODO: Initialize the roll count. */
}
void Die::roll()
{
//TODO: Write the code to roll a die here ...
}
int Die::get_value() const
{
return this->num;
}
ostream& operator<<(ostream& os, Die die)
{
/* TODO: Write the code the insert the
* die’s current value into ’os’ */
}
Explanation / Answer
#include <iostream.h>
#include <cstdlib>
using namespace std;
int main()
{int die,i,j,player1,player2,left;
string yesno;
srand(time(0));
cout<<"Player 1 rolls: ";
left=1;
do{
cout<<"roll "<<left<<endl;
player1=0;
for(j=1;j<6;j++)
{die=rand()%6+1;
cout<<"Dice"<<j<<": "<<die<<endl;
player1+=die;
}
if(left<4)
{cout<<"Player 1 roll again? (yes or no)";
cin>>yesno;
if(yesno!="yes")
left=6;
}
left++;
}while (left<5);
cout<<" Player 2 rolls: ";
left=1;
do{
cout<<"roll "<<left<<endl;
player2=0;
for(j=1;j<6;j++)
{die=rand()%6+1;
cout<<"Dice"<<j<<": "<<die<<endl;
player2+=die;
}
if(left<4)
{cout<<"Player 2 roll again? (yes or no)";
cin>>yesno;
if(yesno!="yes")
left=6;
}
left++;
}while (left<5);
cout<<"Player 1 Total:"<<player1<<endl;
cout<<"Player 2 Total:"<<player2<<endl;
if(player1>player2)
cout<<"Player 1 Wins ";
else
if(player2>player1)
cout<<"Player 2Wins ";
else
cout<<"Game Tied ";
system("pause");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.