ysablewolf
Programmer
Hi I'm trying to pass an array into a function without using pointers..
My code is as follows
void Select_Game_Mode(void) ;
void Single_Player_Mode(void) ;
int Single_Player_Play(int bucket[], int arrSize) ;
int main()
{
srand(time(0)) ;
Select_Game_Mode() ;
return 0;
}
// Function to select game mode
// If the choices are not within the specified range then the program exits
void Select_Game_Mode()
{
cout << "Welcome to The Game of DICE BUCKET"
<< "\n\nPlease select the mode "
<< "\n\n1. Single player "
<< "\n2. Two Players "
<< "\n3. Play against the computer "
<< "\n\nEnter your choice : " ;
int playerChoice ;
cin >> playerChoice ;
if (playerChoice <= 0 || playerChoice > 3)
{
cerr << "Invalid Entry " ;
system("pause");
exit(0);
}
else
{
Print_Rules(playerChoice) ;
}
}
// Function for single player mode
void Single_Player_Mode(void)
{
char answer = 'y' ;
const int SIZE = 13;
bool arrBucket[SIZE] = {0} ;
int count (0), points(0), totalPoints(0), maxPoint(24); // 24 is the magic number of points the player needs equal or surpass in order to win
while (( answer == 'y' || answer == 'Y') )
{
points = Single_Player_Play ( (static_cast <int> arrBucket[]), SIZE) ; // function call
totalPoints = totalPoints + points ;
if (totalPoints >= maxPoint)
{
Check_Winner( totalPoints, maxPoint );
}
cout << "\nYour total winning so far is " << totalPoints
<< "\nDo you want to play another round ? You risk LOSING EVERYTHING! " ;
cin >> answer ;
if ( (answer == 'n' || answer == 'N') )
{
Check_Winner( totalPoints, maxPoint );
}
}
}
// singleplayer play
int Single_Player_Play(int bucket[], int arrSize)
{
int SinglePlayerPoints(0), round(0);
round = Dice_Roll();
for ( int i = 2; i <= arrSize; i++ ) // i is set to 2 because in a 2 die roll, the result can never be lower than 2
{
if ( round == i ) // check if dice roll matches the array element
{
if ( bucket ) // check and see if bucket is full
{
SinglePlayerPoints = 0 ;
cout << "\nSorry " << round << " has already been rolled"
<< "\nYou lose all your points" << endl ;
system("pause");
return 0 ;
}
else if ( !bucket ) // if bucket is empty, fill it
{
bucket = 1 ;
SinglePlayerPoints = SinglePlayerPoints + round ;
return SinglePlayerPoints ;
}
}
}
}
My code is as follows
void Select_Game_Mode(void) ;
void Single_Player_Mode(void) ;
int Single_Player_Play(int bucket[], int arrSize) ;
int main()
{
srand(time(0)) ;
Select_Game_Mode() ;
return 0;
}
// Function to select game mode
// If the choices are not within the specified range then the program exits
void Select_Game_Mode()
{
cout << "Welcome to The Game of DICE BUCKET"
<< "\n\nPlease select the mode "
<< "\n\n1. Single player "
<< "\n2. Two Players "
<< "\n3. Play against the computer "
<< "\n\nEnter your choice : " ;
int playerChoice ;
cin >> playerChoice ;
if (playerChoice <= 0 || playerChoice > 3)
{
cerr << "Invalid Entry " ;
system("pause");
exit(0);
}
else
{
Print_Rules(playerChoice) ;
}
}
// Function for single player mode
void Single_Player_Mode(void)
{
char answer = 'y' ;
const int SIZE = 13;
bool arrBucket[SIZE] = {0} ;
int count (0), points(0), totalPoints(0), maxPoint(24); // 24 is the magic number of points the player needs equal or surpass in order to win
while (( answer == 'y' || answer == 'Y') )
{
points = Single_Player_Play ( (static_cast <int> arrBucket[]), SIZE) ; // function call
totalPoints = totalPoints + points ;
if (totalPoints >= maxPoint)
{
Check_Winner( totalPoints, maxPoint );
}
cout << "\nYour total winning so far is " << totalPoints
<< "\nDo you want to play another round ? You risk LOSING EVERYTHING! " ;
cin >> answer ;
if ( (answer == 'n' || answer == 'N') )
{
Check_Winner( totalPoints, maxPoint );
}
}
}
// singleplayer play
int Single_Player_Play(int bucket[], int arrSize)
{
int SinglePlayerPoints(0), round(0);
round = Dice_Roll();
for ( int i = 2; i <= arrSize; i++ ) // i is set to 2 because in a 2 die roll, the result can never be lower than 2
{
if ( round == i ) // check if dice roll matches the array element
{
if ( bucket ) // check and see if bucket is full
{
SinglePlayerPoints = 0 ;
cout << "\nSorry " << round << " has already been rolled"
<< "\nYou lose all your points" << endl ;
system("pause");
return 0 ;
}
else if ( !bucket ) // if bucket is empty, fill it
{
bucket = 1 ;
SinglePlayerPoints = SinglePlayerPoints + round ;
return SinglePlayerPoints ;
}
}
}
}