I have coded a craps game but there are problems in the program and am not sure what they are. I am new to c++ so keep that in mind. The initialize array function should be pointing to the first element of the my 2 dimensional array.
Code:
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <ctime>
using namespace std;
static const int maxRoll = 50;
static const int maxGame = 3;
class Craps { //Begin CRAPS CLASS
private:
public:
int rolls[maxGame][maxRoll];//2dim
int *pFirst; //pointer
Craps(); //constrctor
void DoubleArray();
static void InitializeApp();//Bonus Assignment
};//END CRAPS CLASS
Craps::Craps()//scope res operator //for loop
{
int i, j;
for(i = 0 ; i < maxGame ; ++i)
{
for(j = 0 ; j < maxRoll ; ++j)
{
rolls[i][j] = 0;
}
}
pFirst = &rolls[0][0];
}
void Craps::DoubleArray()
{
int i, j;
for(i = 0 ; i < maxGame ; ++i)
{
for(j = 0 ; j < maxRoll ; ++j)
{
if (i ==0)
rolls[i][j] =j;
else if (i == 1)
rolls[i][j] = j+50;
else if (i == 2)
rolls[i][j] = j+100;
}
}
}
int RollDie()
{
return (1 + rand() %6);
}
int RollDice()
{
return RollDie() + RollDie();
}
bool PlayGame(int allRolls[][50], int rollCounts[], int gameNum)
{
int point = RollDice();
int roll;
int rollCount = 0;
rollCounts[gameNum] = 0;
if (7 == point)
{
return true;
}
do
{
roll = RollDice();
allRolls[gameNum][rollCount] = roll;
++rollCount;
++rollCounts[gameNum];
if (7 == roll)
{
return false;
}
}
while (roll != point);
return true;
}
void InitializeArray(Craps *pc, int initVal)//function that takes an intger pointer to the first element of two
//dimensional array holding all the rolls.
{
int i,j;
for(i = 0 ; i < maxGame ; ++i)
{
for(j = 0 ; j < maxRoll ; ++j)
{
pc->rolls[i][j] = initVal;
}
}
*pc->pFirst = initVal;
pc->DoubleArray();
}
void Craps::InitializeApp() //definition of static member function InitializeApp
//This function takes no parameters and does nothing but seed my random number generator.
{
srand(time(0));
}
int _tmain(int argc, _TCHAR* argv[])
{
int allRolls[3][50];
int rollCounts[3];
int i;
int rolls;
//Calling the static member function of the Craps class
Craps::InitializeApp();
for (i = 0 ; i < 3 ; ++i)
{
PlayGame(allRolls, rollCounts, i);
cout << "Game number " << i+1 << " had " << rollCounts[i] << " rolls:\n\t";
for (rolls = 0 ; rolls < rollCounts[i] ; ++ rolls)
{
cout << allRolls[i][rolls] << " ";
}
cout << endl;
}
Craps c1;
Craps c2;
Craps c3;
int j;
InitializeArray(&c1, -1); //uses address operator(&) to pass the address of c1 to the InitializeArray function
InitializeArray(&c2, 2); //uses address operator(&) to pass the address of c2 to the InitializeArray function
InitializeArray(&c3, 0); //uses address operator(&) to pass the address of c3to the InitializeArray function
for(i = 0 ; i < maxGame ; ++i)
{
for(j = 0 ; j < maxRoll ; ++j)
{
cout << setw(3) << c1.rolls[i][j] << " ";//set the width for display
}
cout << endl;
}
cout << "\n\n";
for(i = 0 ; i < maxGame ; ++i)
{
for(j = 0 ; j < maxRoll ; ++j)
{
cout << setw(3) << c2.rolls[i][j] << " "; //set the width for display
}
cout << endl;
}
cout << "\n\n";
for(i = 0 ; i < maxGame ; ++i)
{
for(j = 0 ; j < maxRoll ; ++j)
{
cout << setw(3) << c3.rolls[i][j] << " "; //set the width for display
}
cout << endl;
}
cout << endl << *c1.pFirst << endl;//points to the first element of the two dimensional array
cout << endl << *c2.pFirst << endl;//points to the first element of the two dimensional array
cout << endl << *c3.pFirst << endl;//points to the first element of the two dimensional array
system("PAUSE");
return 0;
}