Quiksilver89
Programmer
hey,
I'm working on a program the simulates the Towers Of Hanoi puzzle. The user enters the number of disks, and the program uses winbgim's BGI interface to do a simple simulation of the puzzle being solved. I already have the algorithm to solve the problem, but I'm having trouble with the graphics.
I don't know how I would go about showing the disks actually being "moved" on the screen. Heres my program so far. It constructs the pegs, and shows the first disks. All i have to do is show them being moved, according to the algorithm.
Thanks!
CODE
#include<iostream.h>
#include<iomanip.h>
#include<apstring.h>
#include<apvector.h>
#include<apmatrix.h>
#include<winbgim.h>
void Hanoi(int n, int s, int d, int i)
{
if (n == 1){
cout << "Move from " << s << " to " << d << endl;
}else{
Hanoi(n-1, s, i, d);
Hanoi(1, s, d, i);
Hanoi(n-1, i, d, s);
}
}
int main()
{
int n;
int left=55, top=640, right=265, bottom=650, i=0;
cout << "Enter number of disks: ";
cin >> n;
setcolor(RED);
initwindow(1000,700);
setbkcolor(BLACK);
setlinestyle (SOLID_LINE, 5, THICK_WIDTH);
while (!kbhit()) //Continue while the keyboard has not been hit.
{
rectangle( 150, 220, 165, 650 );
rectangle( 450, 220, 465, 650 );
rectangle( 750, 220, 765, 650 );
setcolor(RED);
while(i != n)
{
setcolor(RED);
rectangle( left, top, right, bottom );
left=left+10;
top=top-10;
right=right-10;
bottom=bottom-10;
i++;
}
}
Hanoi(n, 1, 3, 2);
closegraph();
system("pause");
return 1;
}
I'm working on a program the simulates the Towers Of Hanoi puzzle. The user enters the number of disks, and the program uses winbgim's BGI interface to do a simple simulation of the puzzle being solved. I already have the algorithm to solve the problem, but I'm having trouble with the graphics.
I don't know how I would go about showing the disks actually being "moved" on the screen. Heres my program so far. It constructs the pegs, and shows the first disks. All i have to do is show them being moved, according to the algorithm.
Thanks!
CODE
#include<iostream.h>
#include<iomanip.h>
#include<apstring.h>
#include<apvector.h>
#include<apmatrix.h>
#include<winbgim.h>
void Hanoi(int n, int s, int d, int i)
{
if (n == 1){
cout << "Move from " << s << " to " << d << endl;
}else{
Hanoi(n-1, s, i, d);
Hanoi(1, s, d, i);
Hanoi(n-1, i, d, s);
}
}
int main()
{
int n;
int left=55, top=640, right=265, bottom=650, i=0;
cout << "Enter number of disks: ";
cin >> n;
setcolor(RED);
initwindow(1000,700);
setbkcolor(BLACK);
setlinestyle (SOLID_LINE, 5, THICK_WIDTH);
while (!kbhit()) //Continue while the keyboard has not been hit.
{
rectangle( 150, 220, 165, 650 );
rectangle( 450, 220, 465, 650 );
rectangle( 750, 220, 765, 650 );
setcolor(RED);
while(i != n)
{
setcolor(RED);
rectangle( left, top, right, bottom );
left=left+10;
top=top-10;
right=right-10;
bottom=bottom-10;
i++;
}
}
Hanoi(n, 1, 3, 2);
closegraph();
system("pause");
return 1;
}