Well here is my biggest jam of the day:
1) I am not sure if I am using the test method successfully in this program. I need to test
the recursion to make sure it’s working accurately. I am not sure how to test this before
it prints because I am having issues passing the data.
2) So I tested it after to at least print if the function worked successfully by making it to the
base case. However, I am having one error:
.cpp(174) : error C2597: illegal reference to non-static member 'Disks::disks'
for the line in the function definition
if(Disks::disks == 1)
My code is below.
Header file:
// Disks class definition
class Disks
{
public:
Disks(int); // constructor
void setVariables(int);
//void validateDisks(int);
//void validateChoice(int);
// function that contains the recursive algorithm
void towers(int,int,int,int);
// function to gather the data and perform the algorithm based on the disk info
void transfer();
private:
int disks;
int choice;
};
class ToH
{
public:
static bool test();
};
Main() :
int main()
{
// create the object
Disks disk(3); // number of disks
// transfer disks and display
disk.transfer();
cout << endl;
return 0;
}
Function definitions:
#include "Disks.h" // include definition of class Disks
//constructor to initialize the data member
Disks::Disks(int enteredInteger)
{
setVariables(enteredInteger);
}
// set function to set the values for the data members
void Disks::setVariables(int enteredInteger)
{
disks = enteredInteger; // 3
choice = enteredInteger; // peg 3
}
// function towers contains the algorithm to transfer the disks / pegs, using the 4//parameters of n number of disks to be moved, the peg on which the disks initially start
//on, the peg to use as use as a temporary holding area, and the peg to which the disks
// will be moved to. Basically the base case is if you need to transfer 1 disk from A to B,
//then transfer it directly but if
// you need to transfer n disks from A to B, then there are 3 basic steps, we can transfer n-1
//disks from A to C, then we can
// transfer the last disk from A to B, and finally transfer the n-1 disks from C to B
void Disks::towers( int disks, int start, int end, int temp )
{
if ( disks == 1 )
{
cout << start << " --> " << end << endl;
}
else // recursion step
{
towers( disks-1, start, temp, end );
cout << start << " --> " << end << endl;
towers( disks-1, temp, end, start);
}
}
// function transfer prompts the user for the number of disks, prints the instructions to
//move the disks, and then allows the user
// to choose from which tower to transfer, then based on the choice it uses a switch
//statement to pass the parameters to
// function towers, to apply the algorithm based on the scenario
void Disks::transfer()
{
cout << "Instructions to move disks:";
cout << endl
<< "1) 1 --> 2" << endl
<< "2) 1 --> 3" << endl
<< "3) 2 --> 1" << endl
<< "4) 2 --> 3" << endl
<< "5) 3 --> 1" << endl
<< "6) 3 --> 2" << endl;
//<< "\nChoose from which to which tower to transfer: ";
//cin >> choice;
//validateChoice(choice);
cout << endl;
switch(choice)
{
case 1:
towers( disks, 1, 2, 3 );
break;
case 2:
towers( disks, 1, 3, 2 );
break;
case 3:
towers( disks, 2, 1, 3 );
break;
case 4:
towers( disks, 2, 3, 1 );
break;
case 5:
towers( disks, 3, 1, 2 );
break;
case 6:
towers( disks, 3, 2, 1 );
break;
}
}
// test function in class ToH uses a test method to test the recursion
bool ToH::test()
{
bool answer;
if(Disks::disks == 1)
{
answer = true;
cout << "Function passed.";
}
else
{
answer = false;
cout << "Function failed.";
}
return answer;
}