1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
|
#include <iostream>
#include <iomanip>
using namespace std;
// FUNCTION PROTOTYPES GO HERE:
int read_pegs(const int maxi_num);
void peg_procedure(int peg_array[], int size, const int maxi_discs);
void discs_pegs(int array[],int array_index, const int maxi_discs);
void draw_peg(int peg_array, int size);
void display(int array_index, int discs_number, int discs_total);
int main()
{
// Define variables and constants here
const int maxpegs_size(20),maxdisc_size(10);
int pegs_num;
// Algorithm:
// Prompt and read number of rods
pegs_num= read_pegs(maxpegs_size);
int *peg=new int[pegs_num];
// Prompt and read the number of objects in each rod
peg_procedure(peg,pegs_num, maxdisc_size);
// Draw the rods with percentages
draw_peg(peg, pegs_num);
// Display statistics
// WHILE some rod is NOT empty DO
// Prompt and read the next player's move
// Remove the specified number of objects from the specified rod
// IF all the heaps are empty, THEN
// Print a message congratulating the winning player.
// ELSE
// Redraw the rods with percentages
// Display statistics
// Change to the other player
// END IF
// END WHILE
return 0;
}
// FUNCTION DEFINITIONS GO HERE:
// Prompt the suer for the number of pegs used
// pegs_num=maximum number of pegs
int read_pegs(const int maxi_num)
{
int pegs_num;
cout << "How many pegs are in this game?";
cin >> pegs_num;
while(pegs_num>20 && pegs_num<1)
{
cout << "Number of pegs must be positive and less than or equal to 20"<<endl;
cout << "Enter number of pegs again: ";
cin >> pegs_num;
}
return(pegs_num);
}
//prompt and read the number of discs in each peg;
//peg_array=array of pegs;
//size = size of the array;
//maxi_discs=maximum number of discs per peg
void peg_procedure(int peg_array[], int size, const int maxi_discs)
{
for(int i=0;i<size;i++)
{ discs_pegs(peg_array,i,maxi_discs); }
}
//helper procedure of peg_procedure, prompt the suer for the number of discs to place on this peg
//array[]=array entry for this peg
//array_index=the index of array
//maxi_discs=the maximum number of discs
void discs_pegs(int array[],int array_index, const int maxi_discs)
{
cout << "How many discs are on peg" <<array_index<<": ";
cin >> array[array_index];
while(array[array_index]<1 && array[array_index]>maxi_discs)
{
cout<<"Number of pegs must be positive and less than or equal to " << maxi_discs<<endl;
cout<<"Enter numbers of discs again: ";
cin >> array[array_index];
}
}
//draw the pegs
//peg_array=array of pegs
//size= size of array
void draw_peg(int peg_array[], const int size)
{
int total_discs(0);
for(int i=0;i<size;i++)
{
total_discs=total_discs+peg_array[i];
}
for(int i=0;i<size;i++)
{
display(i,peg_array[i],total_discs);
}
}
//helper procedure of draw_peg
//array_index=index of this peg
//discs_number=number of discs for this peg
//discs_total=total number of discs in the array of pegs
void display(int array_index, int discs_number, int discs_total)
{
cout<<"peg"<<setw(3)<<array_index<<":";
for(int i=1;i<=discs_number;i++)
{
cout<<"%";
}
cout<<left<<setw(10)<<discs_number/discs_total<<endl;
}
|