// File: dartgame.cpp // Darts game program. Throw x darts with goal y points. How many combs??? #include #include #include #include #include #define MaxValues 10 using namespace std; // We'll use a quick & dirty class for 2 reasons: // 1. In order to be able to initialize the DartsByValue array automatically. // 2, We can't use an array alone because it is important that DartsByValue // be passed by value. See ThrowDarts() below class DartRec { public: DartRec(); int DartsByValue[MaxValues]; }; // Implement the class' constructor DartRec::DartRec() {for (int i=0;i=0) values in Value array void GetInfo(int Points[],int &Count,int &Goal,int &DartsToThrow) {char *DartPt=new char[10]; Count=0; // Get possible dart values cout << "Enter the Point Values for Darts, 1 Integer to a line\n"; cout << "When done, press return............\n"; do { cin.getline( DartPt,'\n'); if (strlen(DartPt)>0) Points[Count++]=atoi(DartPt); } while (strlen(DartPt)>0); // Get Goal cout << "Enter the # of Points that the Darts Should Total >"; cin >> Goal; assert(Goal>0); // Get # to throw cout << "Enter the # of Darts To Throw >"; cin >> DartsToThrow; assert(DartsToThrow>0); } // Display a successful combination, showing the number of darts of // each possible value. Args: // Comb: Holds array with # of darts of each value. // Comb.dartsByValue[i] holds # of darts thrown with Value[i] // Pre: Comb holds a legitimate combination // Post:Contents of Comb have been output void ShowResult(const DartRec &Comb) {cout << " "; cout.setf(ios::left); for (int i=0;i Success...Print HowMany // 2. PointsSoFar>Goal or run out of values => Fail...Stop // Recursive Step: // Throw all possible darts of the present value void ThrowDarts(int Darts,int PointsSoFar,DartRec HowMany,int Index) {if (PointsSoFar==Goal) { ShowResult(HowMany); Combinations++; return; } if (PointsSoFar>Goal || Index==Values) return; for (int i=0;i<=(DartsToThrow-Darts);i++) { HowMany.DartsByValue[Index]=i; ThrowDarts(Darts+i,Value[Index]*i+PointsSoFar,HowMany,Index+1); } }