You are on page 1of 6

// FUNCTION PROTOTYPES GO HERE:

//reads number of pegs to be used in game


int read_number_of_pegs(int max_pegs);
//reads objects per peg
void read_objects_per_peg(int *pegs, int num_pegs, int max_objects);
//reads objects for a given peg
void read_objects_for_a_peg(int *pegs, int peg, int max_objects);
//draws pegs
void draw_pegs(int *pegs, int num_pegs);
//draw object in a peg
void draw_objects_in_peg(int peg, int num_objects, int total_objects);
//display statistics
void display_statistics(int *pegs, int num_pegs);
//display pegs having smallest number of objects
void display_smallest_pegs(int *pegs, int num_pegs);
//display pegs having largest number of objects
void display_largest_pegs(int *pegs, int num_pegs);
//display pegs having average number of objects
void display_average_object_per_peg(int *pegs, int num_pegs);

int main()
{
// Define variables and constants here
const int MAX_PEGS = 20;
const int MAX_OBJECTS = 10;
int num_pegs; //number of pegs
int *pegs; //objects in pegs
int num_players = 2; //maximum number of players
// Algorithm:

// Prompt and read number of pegs


num_pegs = read_number_of_pegs(MAX_PEGS);
// Prompt and read the number of objects in each peg
//array for objects in pegs
pegs = new int[num_pegs];
read_objects_per_peg(pegs, num_pegs, MAX_OBJECTS);

// Draw the pegs with percentages


draw_pegs(pegs, num_pegs);

// Display statistics
display_statistics(pegs, num_pegs);
// WHILE some peg is NOT empty DO
int peg = 1; //used in loop: counter for peg
int current_player = 1; //indicates the current player
int peg_to_modify; //peg being modified
int objects_to_remove; //objects to be removed from specified peg

while(peg <= num_pegs && pegs[peg]>0)


{
// Prompt and read the next player's move
if(current_player<=num_players)
{
cout<<"Player ( "<<current_player<<") : Which peg would you like to play? "<<endl;
cin>>peg_to_modify;
cout<<"Enter number of stones to remove from peg " << peg_to_modify;
cin>>objects_to_remove;
// Remove the specified number of objects from the specified peg
pegs[peg_to_modify-1]-=objects_to_remove;
//check if all pegs empty
bool all_pegs_empty=false; //flag to mark pegs empty
for(int i=0;i<num_pegs;i++)
{
if(pegs[i]<=0)
{
all_pegs_empty=true;
}
else
{
all_pegs_empty=false;
}
}
// IF all the heaps are empty, THEN
if(all_pegs_empty)
{
// Print a message congratulating the winning player.
cout<<"Congratulations of winning the game, Player "<<current_player<<"!"<<endl;
}
// ELSE
else
{
// Redraw the pegs with percentages
for(int i=0;i<num_pegs;i++)
{
//cout<<"Peg "<<i+1<<":"<<endl;
for(int j=0;j<pegs[i];j++)
{
cout<<"%";
}
cout<<endl;
}
//Display statistics
display_statistics(pegs, num_pegs);

//Change to other player


if(current_player==1)
current_player=2;
else
current_player=1;
}
}//END IF
peg++;
}//END WHILE

return 0;
}

// FUNCTION DEFINITIONS GO HERE:


//function definition for reading pegs
int read_number_of_pegs(int max_pegs)
{
int num_pegs; //number of pegs
//read number of pegs
cout<<"Enter number of pegs:";
cin>>num_pegs;
//check if valid
while(1)
{
if(num_pegs < 1 || num_pegs > max_pegs)
{
cout<<"Not a valid value for number of pegs. Enter again:";
cin>>num_pegs;
}
else
break;
}
return num_pegs;
}
//function definition for reading obejcts per peg
void read_objects_per_peg(int *pegs, int num_pegs, int max_objects)
{
//read number of objects in each peg
for(int i=0;i<num_pegs;i++)
{
read_objects_for_a_peg(pegs,i,max_objects);
}
}
//helper function for read_objects_per_peg
void read_objects_for_a_peg(int *pegs, int peg, int max_objects)
{
cout<<"Enter number of objects in peg "<<peg+1<<":";
cin>>pegs[peg];
//check if valid
int objects=pegs[peg];
while(1)
{
if(objects < 1 || objects > max_objects)
{
cout<<"Not a valid value for number of objects. Enter again:";
cin>>pegs[peg];
}
else
break;
}
}
//function definition to draw pegs
void draw_pegs(int *pegs, int num_pegs)
{
cout<<endl;
int total_objects = 0;
for(int i = 0;i < num_pegs; i++)
{
total_objects += pegs[i];
}
for(int i = 0; i < num_pegs; i++)
{
int num_objects = pegs[i];
draw_objects_in_peg(i, num_objects, total_objects);
double percentage = ((double) num_objects / (double) total_objects) * 100.00;
cout << setprecision(5) << "(" << percentage << "%)" << endl;
}
cout << endl;
}
cout <<"("<<(num_objects/total_objects)*100<<"%)"<<endl;
}
}
//helper function for draw_pegs()
void draw_objects_in_peg(int peg, int num_objects, int total_objects)
{
cout<<setw(5) << "Peg "<<peg<<": ";
for(int j = 0; j< num_objects; j++)
{
cout<<"#";
}
}
//display statistics
void display_statistics(int *pegs, int num_pegs) {
display_smallest_pegs(pegs, num_pegs);
display_largest_pegs(pegs, num_pegs);
display_average_object_per_peg(pegs, num_pegs);
}
//display pegs having smallest number of objects
void display_smallest_pegs(int *pegs, int num_pegs) {
int smallest_pegs[num_pegs];
int j = 0;
int smallest = 0;
smallest_pegs[j] = smallest;
for (int i = 1; i < num_pegs; i++) {
if (pegs[i] < pegs[smallest]) {
smallest = i;
smallest_pegs[j] = smallest;
} else if (pegs[i] == pegs[smallest]) {
j++;
smallest_pegs[j] = i;
}
}
for (int k = 0; k <= j; k++)
cout << cout << "Peg " << smallest_pegs[k] << "has the smallest number of stones" << endl;
cout << endl;
}
//display pegs having largest number of objects
void display_largest_pegs(int *pegs, int num_pegs) {
int largest_pegs[num_pegs];
int j = 0;
int largest = 0;
largest_pegs[j] = largest;
for (int i = 1; i < num_pegs; i++) {
if (pegs[i] > pegs[largest]) {
largest = i;
largest_pegs[j] = largest;
}
else if (pegs[i] == pegs[largest]) {
j++;
largest_pegs[j] = i;
}
}

for (int k = 0; k <= j; k++)


cout << "Peg " << largest_pegs[k] << "has the largest number of stones" << endl;
}
//display pegs having average number of objects
void display_average_object_per_peg(int *pegs, int num_pegs) {
double total_objects = 0.0, average_objects;
int pegs_with_zero_objects = 0;
for (int i = 0; i < num_pegs; i++) {
int num_objects = pegs[i];
total_objects += num_objects;
if (num_objects == 0)
pegs_with_zero_objects++;
}
//calculate average
average_objects = total_objects / (num_pegs - pegs_with_zero_objects);
cout << "The average number of stones per peg (i.e., pegs with stones) is " << average_objects
<< endl;
cout << endl;
}

You might also like