I am having troubles with this C++ code: #include <iostream> #include <stdlib.h>
ID: 3669834 • Letter: I
Question
I am having troubles with this C++ code:
#include <iostream>
#include <stdlib.h> /* srand, rand */
#include <time.h>
using namespace std;
class Player
{
public:
//method to simulate game
void printBoard();
Player(int jac, int jil,bool finish); //constructor
int jackWins;
int jillWins;
int game;
private:
// initialize jack and jill
int roll, jack, jill;
// class variables declared
bool finished;
string winner;
};
// constructor .. inititalzing values
Player::Player(int jac, int jil,bool finish)
{
jack = jac;
jill = jil;
finished = finish;
jackWins = 0;
jillWins = 0;
game = 0;
}
void Player::printBoard()
{
// initializing random number generator
srand(time(NULL));
jack = 0;
jill = 0;
// loop until game is finished
do
{
// Jack’s turn.
// generate a random number in the range 1 to 6
roll = (rand() % 6) + 1;
// add it to jack
jack += roll;
// print roll value followed by jack position
cout << roll << " "<< jack << " ";
// if jack equals jill, tumble jill
if(jack == jill)
{
// generate random number in range 1 to 3 and deduct it from jill
jill -= (rand() % 3) + 1;
// if jill goes to negative position, set jill to 0
if(jill < 0)
jill = 0;
// print jill position and Tumble Jill
cout << jill << " " << "Tumble Jill!";
}
// feed new line
cout << endl;
// if jack reached the top of hill
if(jack >= 20)
{
// Set finished to true
finished = true;
// Set winner to “Jack”
winner = "Jack";
// Skip Jill's turn if Jack already won
break;
}
//Jill's turn
// generate a random number in the range 1 to 6
roll = (rand() % 6) + 1;
// print roll value
cout << roll << " ";
// add it to Jill's position
jill += roll;
// if Jill equals Jack, tumble Jack
if(jack == jill)
{
// generate random number in range 1 to 3 and deduct it from jack
jack -= (rand() % 3) + 1;
// if jack goes to negative position, set jack to 0
if(jack < 0)
jack = 0;
// print jack and jill's positions and Tumble jack
cout << jack << " " << jill << " " << "Tumble Jack!" << endl;
}
else
// otherwise print only jill's position
cout << " " << jill << endl;
// if jack reached the top of hill
if(jill >= 20)
{
// Set finished to true
finished = true;
// Set winner to “Jill”
winner = "Jill";
}
}
while(!finished); // loop until finished is true
// print winner
if(winner == "Jack"){
jackWins++;
}
else{
jillWins++;
}
game++;
cout << winner << " wins game " << game << "!" << endl;
}
// Main function for the program
int main( )
{
//creating Player object
Player diceGame(0,0,false);
bool index = true;
int n;
cout<<"How many times you want to simulate game (choose an odd number): ";
cin>>n;
while (index == true)
if(n % 2 == 1){
cout<<" Roll,Jack,Jill";
cout<<" ____________________________ ";
for(int i=0;i<n;i++){
//calling method on object
diceGame.printBoard();
}
cout<<" Jack Wins: "<<diceGame.jackWins<<" Times";
cout<<" Jill Wins: "<<diceGame.jillWins<<" Times";
if (diceGame.jackWins > diceGame.jillWins)
cout<<" Jack is the Dice Race Grand Champion! ";
else
cout<<" Jill is the Dice Race Grand Champion! ";
index = false;
}
else {
cout<<"How many times you want to simulate game (choose an odd number): ";
cin>>n;
}
return 0;
It outputs this:
How many times you want to simulate game (choose an odd number): 5
Roll,Jack,Jill
____________________________
4 4
5 5
5 9
6 11
3 12
4 15
5 17
3 18
3 20
Jack wins game 1!
4 4
5 5
Jack wins game 2!
4 4
5 5
Jack wins game 3!
4 4
5 5
Jack wins game 4!
4 4
5 5
Jack wins game 5!
Jack Wins: 5 Times
Jill Wins: 0 Times
Jack is the Dice Race Grand Champion!
When I want it to output this:
How many times you want to simulate game (choose an odd number): 5
Roll Jack Jill
------------------------
5 5
5 2 5 Tumble Jack!
6 8
2 7
4 12
5 10 12 Tumble Jack!
2 12 11 Tumble Jill!
5 16
1 13
5 21
Jill wins game 1!
6 6
5 5
2 8
4 9
1 9 8 Tumble Jill!
1 6 9 Tumble Jack!
6 12
4 13
3 15
2 14 15 Tumble Jack!
3 17
2 15 17 Tumble Jack!
6 21
Jack wins game 2
And then the next 3 games...
and then at the end it should print this:
Jack won 3 games and Jill won 2 games.
Jack is the Dice Race Grand Champion!
Explanation / Answer
Given below the modified working program
#include <iostream>
#include <stdlib.h> /* srand, rand */
#include <time.h>
using namespace std;
class Player
{
public:
//method to simulate game
void printBoard();
Player(int jac, int jil,bool finish); //constructor
int jackWins;
int jillWins;
int game;
private:
// initialize jack and jill
int roll, jack, jill;
// class variables declared
bool finished;
string winner;
};
// constructor .. inititalzing values
Player::Player(int jac, int jil,bool finish)
{
jack = jac;
jill = jil;
finished = finish;
jackWins = 0;
jillWins = 0;
game = 0;
}
void Player::printBoard()
{
// initializing random number generator
srand(time(NULL));
jack = 0;
jill = 0;
// loop until game is finished
do
{
// Jack’s turn.
// generate a random number in the range 1 to 6
roll = (rand() % 6) + 1;
// add it to jack
jack += roll;
// print roll value followed by jack position
cout << roll << " "<< jack << " ";
// if jack equals jill, tumble jill
if(jack == jill)
{
// generate random number in range 1 to 3 and deduct it from jill
jill -= (rand() % 3) + 1;
// if jill goes to negative position, set jill to 0
if(jill < 0)
jill = 0;
// print jill position and Tumble Jill
cout << jill << " " << "Tumble Jill!";
}
// feed new line
cout << endl;
// if jack reached the top of hill
if(jack >= 20)
{
// Set finished to true
finished = true;
// Set winner to “Jack”
winner = "Jack";
// Skip Jill's turn if Jack already won
break;
}
//Jill's turn
// generate a random number in the range 1 to 6
roll = (rand() % 6) + 1;
// print roll value
cout << roll << " ";
// add it to Jill's position
jill += roll;
// if Jill equals Jack, tumble Jack
if(jack == jill)
{
// generate random number in range 1 to 3 and deduct it from jack
jack -= (rand() % 3) + 1;
// if jack goes to negative position, set jack to 0
if(jack < 0)
jack = 0;
// print jack and jill's positions and Tumble jack
cout << jack << " " << jill << " " << "Tumble Jack!" << endl;
}
else
// otherwise print only jill's position
cout << " " << jill << endl;
// if jack reached the top of hill
if(jill >= 20)
{
// Set finished to true
finished = true;
// Set winner to “Jill”
winner = "Jill";
}
}
while(!finished); // loop until finished is true
// print winner
if(winner == "Jack"){
jackWins++;
}
else{
jillWins++;
}
game++;
cout << winner << " wins game " << game << "!" << endl;
}
// Main function for the program
int main( )
{
//creating Player object
Player diceGame(0,0,false);
bool index = true;
int gameCounter = 0, finalJackWins = 0, finalJillWins = 0;
int n;
cout<<"How many times you want to simulate game (choose an odd number): ";
cin>>n;
gameCounter = n;
while (gameCounter != 0)
{
if(n % 2 == 1){
cout<<" Roll,Jack,Jill";
cout<<" ____________________________ ";
for(int i=0;i<n;i++){
//calling method on object
diceGame.printBoard();
}
cout<<" Jack Wins: "<<diceGame.jackWins<<" Times";
cout<<" Jill Wins: "<<diceGame.jillWins<<" Times";
if (diceGame.jackWins > diceGame.jillWins)
{
finalJackWins++;
cout<<" Jack is the Dice Race Grand Champion! ";
}
else
{
finalJillWins++;
cout<<" Jill is the Dice Race Grand Champion! ";
}
//index = false;
gameCounter--;
}
else {
cout<<"How many times you want to simulate game (choose an odd number): ";
cin>>n;
gameCounter = n;
}
}
cout<<"Jack won "<<finalJackWins<<" games and Jill won "<<finalJillWins<<" games"<<endl;
if(finalJackWins > finalJillWins)
{
cout<<"Jack is the Dice Race Grand Champion!"<<endl;
}
else if(finalJackWins < finalJillWins)
{
cout<<"Jill is the Dice Race Grand Champion!"<<endl;
}
else
{
cout<<"Its a draw"<<endl;
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.