# 2 Re: need help with function calls
I didnt include the code b/c I thought it was how my functions move from one to the other, and because there is lot. However, I guess it would help. Here it is.
Thanks!
#include <iostream>
#include <fstream>
#include <string>
#include <ctime> // For time()
#include <time.h>
#include <cstdlib> // For srand() and rand()
using namespace std;
//*** Functions ***
void RollDice(int d1, int d2, int d3, int d4, int d5);
void DisplayScorePad();
void TakeScore();
void KeepDice();
//*** Global Variables ***
int TurnsLeft; //# of turns left. TurnsLeft=0 means game over
int RollsLeft; //# of rolls left in turn. RollsLeft = 0 means player must score before next turn.
int Dice[4]; //represents dice 1 through dice 5. Possible values are between 1 and 6
int Points; // represents points obtained for certain functions. Initial value set (or re-set) to
//zero at the start of each function that uses it
struct Score //all variables used to set values on score pad
{
int
Ones,
Twos,
Threes,
Fours,
Fives,
Sixes,
UpperSubTotal,
BonusPoints,
UpperSectionTotal,
ThreeKind,
FourKind,
FullHouse,
SmStraight,
LrgStraight,
Yahtzee,
Chance,
BonusYahtzee,
LowerSectionTotal,
GrandTotalScore,
RollsLeft,
TurnsLeft;
};
Score ScorePad;
//*** Start ***
void main()
{
char rules, ready; //answer variables
// variables for displaying text file of written rules of the game, if player chooses to
string line;
ifstream rulesFile;
// Ask Player if they want to see game rules, Display contents of rules.txt file if requested
cout << "Welcome to YAHTZEE!" << endl;
cout << "This is a Single Player game.\n";
cout << "The best possible score you can achieve is 375 points." << endl << endl;
cout << "Would you like to review the Rules of YAHTZEE? Y/N: ";
cin >> rules;
cout << endl << endl;
if ((rules == 'Y')|| (rules == 'y'))
{
rulesFile.open("rules.txt");
if (rulesFile.is_open())
{
while (! rulesFile.eof())
{
getline (rulesFile,line);
cout << line << endl;
}
rulesFile.close();
}
else
{
cout << "\nUnable to open file."<< endl << endl;
}
}
cout << "\n\nAre you ready to begin play? Y/N: ";
cin >> ready;
if ((ready == 'Y') || (ready == 'y'))
{
//Set initial values for # or turns, rolls, and score sums
TurnsLeft = 13;
RollsLeft =3;
ScorePad.UpperSubTotal = 0;
ScorePad.BonusPoints = 0;
ScorePad.UpperSectionTotal =0;
ScorePad.LowerSectionTotal =0,
ScorePad.GrandTotalScore =0;
system("cls");
cout << "This is your score pad."<< endl << endl;
DisplayScorePad( );
cout << "Let's Begin!!" << endl << endl;
RollDice(0,0,0,0,0); // First roll, all 5 dice are rolled
}
else
{
cout << "Goodbye" << endl;
}
} //end of main()
/**************************************************************************************
Each time the score pad is displayed, the Upper & Lower section totals will be recalculated
with the score box variables that are initialized.
**************************************************************************************/
void DisplayScorePad()
{
if (ScorePad.Ones !=NULL)
{
ScorePad.UpperSubTotal = ScorePad.UpperSubTotal + ScorePad.Ones;
}
if (ScorePad.Twos !=NULL)
{
ScorePad.UpperSubTotal = ScorePad.UpperSubTotal + ScorePad.Twos;
}
if (ScorePad.Threes !=NULL)
{
ScorePad.UpperSubTotal = ScorePad.UpperSubTotal + ScorePad.Threes;
}
if (ScorePad.Fours !=NULL)
{
ScorePad.UpperSubTotal = ScorePad.UpperSubTotal + ScorePad.Fours;
}
if (ScorePad.Fives !=NULL)
{
ScorePad.UpperSubTotal = ScorePad.UpperSubTotal + ScorePad.Fives;
}
if (ScorePad.Sixes !=NULL)
{
ScorePad.UpperSubTotal = ScorePad.UpperSubTotal + ScorePad.Sixes;
}
if (ScorePad.UpperSubTotal > 62)
{
ScorePad.BonusPoints = 35;
}
ScorePad.UpperSectionTotal = (ScorePad.UpperSubTotal + ScorePad.BonusPoints);
if (ScorePad.ThreeKind !=NULL)
{
ScorePad.LowerSectionTotal = ScorePad.LowerSectionTotal + ScorePad.ThreeKind;
}
if (ScorePad.FourKind !=NULL)
{
ScorePad.LowerSectionTotal = ScorePad.LowerSectionTotal + ScorePad.FourKind;
}
if (ScorePad.FullHouse !=NULL)
{
ScorePad.LowerSectionTotal = ScorePad.LowerSectionTotal + ScorePad.FullHouse;
}
if (ScorePad.SmStraight !=NULL)
{
ScorePad.LowerSectionTotal = ScorePad.LowerSectionTotal + ScorePad.SmStraight;
}
if (ScorePad.LrgStraight !=NULL)
{
ScorePad.LowerSectionTotal = ScorePad.LowerSectionTotal + ScorePad.LrgStraight;
}
if (ScorePad.Yahtzee !=NULL)
{
ScorePad.LowerSectionTotal = ScorePad.LowerSectionTotal + ScorePad.Yahtzee;
}
if (ScorePad.Chance !=NULL)
{
ScorePad.LowerSectionTotal = ScorePad.LowerSectionTotal + ScorePad.Chance;
}
if (ScorePad.BonusYahtzee !=NULL)
{
ScorePad.LowerSectionTotal = ScorePad.LowerSectionTotal + ScorePad.BonusYahtzee;
}
ScorePad.GrandTotalScore = (ScorePad.UpperSectionTotal + ScorePad.LowerSectionTotal);
ScorePad.RollsLeft = RollsLeft;
ScorePad.TurnsLeft = TurnsLeft;
//Display score pad
cout << "***********************************************************************************************************"<< endl;
cout << "* YAHTZEE Score Pad *" << endl;
cout << "***********************************************************************************************************" << endl;
cout << "* Upper Section ** Lower Section *" << endl;
cout << "***********************************************************************************************************" << endl;
cout << "* 1 | ONES - sum of all Ones : " << ScorePad.Ones;
cout << " ** 7 | THREE OF A KIND - sum of all dice : " << ScorePad.ThreeKind;
cout << " *" << endl;
cout << "***********************************************************************************************************" << endl;
cout << "* 2 | TWOS - sum of all Twos : " << ScorePad.Twos;
cout << " ** 8 | FOUR OF KIND - sum of all dice : " << ScorePad.FourKind;
cout << " *" << endl;
cout << "***********************************************************************************************************" << endl;
cout << "* 3 | THREES - sum of all Threes : " << ScorePad.Threes;
cout << " ** 9 | FULL HOUSE - 25 points : " << ScorePad.FullHouse;
cout << " *" << endl;
cout << "***********************************************************************************************************" << endl;
cout << "* 4 | FOURS - sum of all Fours : " << ScorePad.Fours;
cout << " ** 10 | SM STRAIGHT - 30 points : " << ScorePad.SmStraight;
cout << " *" << endl;
cout << "***********************************************************************************************************" << endl;
cout << "* 5 | FIVES - sum of all Fives : " << ScorePad.Fives;
cout << " ** 11 | LRG STRAIGHT - 40 points : " << ScorePad.LrgStraight;
cout << " *" << endl;
cout << "***********************************************************************************************************" << endl;
cout << "* 6 | SIXES - sum all all Sixes : " << ScorePad.Sixes;
cout << " ** 12 | YAHTZEE - 50 points : " << ScorePad.Yahtzee;
cout << " *" << endl;
cout << "***********************************************************************************************************" << endl;
cout << "* ** 13 | CHANCE - sum of all dice : " << ScorePad.Chance;
cout << " *" << endl;
cout << "* ******************************************************" << endl;
cout << "* ** 14 | BONUS YAHTZEE - 100 points each : " << ScorePad.BonusYahtzee;
cout << " *" << endl;
cout << "***********************************************************************************************************" << endl;
cout << "* ** *" << endl;
cout << "***********************************************************************************************************" << endl;
cout << "* Sub Total : " << ScorePad.UpperSubTotal;
cout << " ** *" << endl;
cout << "***********************************************************************************************************" << endl;
cout << "* Bonus Points of 35 if Sub Total is over 62 : " << ScorePad.BonusPoints;
cout << " ** *" << endl;
cout << "***********************************************************************************************************" << endl;
cout << "* Upper Section Total : " << ScorePad.UpperSectionTotal;
cout << " ** Lower Section Total : " << ScorePad.LowerSectionTotal;
cout << " *" << endl;
cout << "***********************************************************************************************************" << endl;
cout << "* *" << endl;
cout << "***********************************************************************************************************" << endl;
cout << "* GRAND TOTAL : " << ScorePad.GrandTotalScore;
cout << " *" << endl;
cout << "***********************************************************************************************************" << endl;
cout << "* Rolls Left: " << ScorePad.RollsLeft << " * Turns Left: " << ScorePad.TurnsLeft;
cout << " *" << endl;
cout << "***********************************************************************************************************" << endl;
cout << endl << endl << endl;
} //end DisplayScore()
/**************************************************************************************
This function uses parameters to determine which dice[x] to roll. A zero value indicates
the die is to be rolled. A non-zero value indicates that die is to be 'kept'
***************************************************************************************/
void RollDice(int d1, int d2, int d3, int d4, int d5)
{
Dice[0] = d1;
Dice[1] = d2;
Dice[2] = d3;
Dice[3] = d4;
Dice[4] = d5;
char role_score; //answer variable
const int low = 1;
const int high =6;
time_t seconds;
time(&seconds);
srand((unsigned int)seconds);
cout << "**************************************************************************" << endl;
cout << "Rolling the dice." << endl;
if (Dice[0] ==0)
{
Dice[0] = rand() % (high - low + 1) + low;
}
if (Dice[1] ==0)
{
Dice[1] = rand() % (high - low + 1) + low;
}
if (Dice[2] ==0)
{
Dice[2] = rand() % (high - low + 1) + low;
}
if (Dice[3] ==0)
{
Dice[3] = rand() % (high - low + 1) + low;
}
if (Dice[4] ==0)
{
Dice[4] = rand() % (high - low + 1) + low;
}
RollsLeft = RollsLeft--;
cout << "\n\nDice 1 = " << Dice[0] << endl;
cout << "Dice 2 = " << Dice[1] << endl;
cout << "Dice 3 = " << Dice[2] << endl;
cout << "Dice 4 = " << Dice[3] << endl;
cout << "Dice 5 = " << Dice[4] << endl << endl;
if (RollsLeft == 0)
{
TakeScore();
}
else
{
cout << "RollsLeft = " << RollsLeft << endl;
cout << "Do you want to roll again or score now? R/S: ";
cin >> role_score;
if ((role_score == 'S') || (role_score == 's'))
{
TakeScore();
}
else if ((role_score == 'R') || (role_score == 'r'))
{
KeepDice();
}
else
{
cout << "\nInvalid Answer." << endl;
}
}
} // end of RollDice
/**************************************************************************************
This function passes five parameters back to RollDice( ), which represents each of the
five dice. Playe's input flags which die to keep (if any) before they roll again
**************************************************************************************/
void KeepDice()
{
char diekeep[4]; //char var that = input and transfer the info to each die[x]
int die[4]; //int variable used to pass parameters back to RollDice( )
cout << "\nWhich dice do you want to keep?" << endl;
cout << "Your entry must follow the format xxxxx which represents each dice." << endl;
cout << "Enter a non-zero value to keep and a zero value to roll." << endl;
cout << "Example 1: 10045 - means you want to keep dice 1, 4, 5, and re-roll dice 2 and 3" << endl;
cout << "Example 2: 00000 - means you want to roll all 5 dice." << endl << endl;
cout << "Enter your choices: ";
cin >> diekeep;
for (int i=0; i<=4; i++)
{
if (diekeep[i] == '0')
{
die[i] = 0;
}
else
{
die[i] = 1;
}
}
RollDice(die[0], die[1], die[2], die[3], die[4]);
} //end of KeepDice
/**************************************************************************************
Player is ready to score points in available option box. Ask player what score box they
want to score in. If the chosen score box is available, then run function of chosen option
else, tell player it's an invalid selection and offer choices again. Once score has been
taken, decrease TurnsLeft.
If TurnsLeft !=0 then reset RollsLeft and decrease TurnsLeft by 1, or
Else Game Over, Ask if player wants to play again.
If Y, rerun, else end program
**************************************************************************************/
void TakeScore()
{
cout << "yes" << endl;
} //end of TakeScore()