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
|
#include <iostream>
#include "Rectangle.h"
#include "Colour.h"
#include <list>
#include "EasyBMP.h"
#include "DisplayBackground.h"
void towers(int numDiscs, int presentPeg, int tempPeg, int toPeg);
void DisplayStep();
void ExecuteStep(int from, int to);
list<Rectangle> pegA;
list<Rectanlge> pegB;
list<Rectangle> pegC;
int curStep;
int reqStep;
void DisplayStep();
void main(void)
{
int n = 0;
int step = 0;
int h = 800;
int w = 600;
int wDisc = 0;
int xLoc = 0;
int yLoc = 0;
Colour blue = Colour(0,0,255,255);
Colour green = Colour(0,255,0,255);
BMP img;
DisplayBackground(img,green,w,h);
//prompt the user for the number of pegs
cout << "Enter number of pegs: " << endl;
cin >> n;
cout << "Which step would you like displayed?" << endl;
cin >> reqStep;
Rectangle Rtemp;
//initialize the first peg
for(int i = 0; i < n; i++)
{
wDisc = (int)400*pow(0.8,i);//exponential function
xLoc = (w-wDisc)/2;
yLoc = 800-(25*(i+1));
Rtemp.SetXLocation(xLoc);
Rtemp.SetYLocation(yLoc);
Rtemp.SetWidth(wDisc);
Rtemp.SetHeight(25);
Rtemp.SetColour(blue);
pegA.push_back(Rtemp);
Rtemp.RectangleDisplay(img);
}
img.WriteToFile("C:\\Users\\Dave\\Documents\\bitfile.bmp");
towers(n, 1, 2, 3);
}
void towers(int numDiscs, int presentPeg, int tempPeg, int toPeg)
{
if(numDiscs > 0)
{
curStep++;
towers(numDiscs-1, presentPeg, toPeg, tempPeg);
cout << "Move disc from Peg#" << presentPeg << " to Peg#" << toPeg << endl;
ExecuteStep(presentPeg, toPeg);
if(curStep==reqStep)
{
DisplayStep();
}
towers(numDiscs-1, tempPeg, presentPeg, toPeg);
}
}
void ExecuteStep(int from, int to)
{
if(from==1 && to==2)
{
pegB.push_back(pegA.back());
pegA.pop_back();
}
else if(from==1 && to==3)
{
pegC.push_back(pegA.back());
pegA.pop_back();
}
else if(from==2 && to==3)
{
pegC.push_back(pegB.back());
pegB.pop_back();
}
else if(from==2 && to==1)
{
pegA.push_back(pegB.back());
pegB.pop_back();
}
else if(from==3 && to==1)
{
pegA.push_back(pegC.back());
pegC.pop_back();
}
else if(from==3 && to==2)
{
pegB.push_back(pegC.back());
pegC.pop_back();
}
}
|