Allegro.cc - Online Community

Allegro.cc Forums » Game Design & Concepts » How can I use allegro to simulate the towers of hanoi algorithm?

This thread is locked; no one can reply to it. rss feed Print
How can I use allegro to simulate the towers of hanoi algorithm?
b aryal
Member #17,071
April 2019

I have learnt the towers of hanoi problem.
Now, I wish to simulate it in allegro 4.
What is the algorithm/way for doing it?

GullRaDriel
Member #3,861
September 2003
avatar

Google it.

"Code is like shit - it only smells if it is not yours"
Allegro Wiki, full of examples and articles !!

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

If you know the problem but not the algorithm, that's your homework to complete. If you have trouble visualizing it in Allegro, that's different.

Basically you move from the largest to the smallest using the middle. Have your teacher explain the algorithm and then we can help you visualize it.

Allegro is drawing, input, and windows only.

b aryal
Member #17,071
April 2019

thanks for your reply. Thanks for giving your valuable time to see my comment.

As you told that I should ask the teacher if i haven't understand the algorithm,i want to make you clear that I am clear with the algorithm.

We have to study it in our classes..so i have learnt it...
i can simulate it using recursion in text form...
eg
"moving disk A from peg 1 to peg 3"..

but i want to really show the disk being moved using allegro...

for that, what changes should i do in my recursion code?( I WILL POST IT IF YOU WANT IT)::)

only concepts and i am fine..."thanks for telling others code only smells :D"

GullRaDriel
Member #3,861
September 2003
avatar

That's a part of my signature

"Code is like shit - it only smells if it is not yours"
Allegro Wiki, full of examples and articles !!

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

I have to say, my code smells wonderful, but my doodoo does not...

Anyway...

To help you visualize your towers of hanoi, you need to be more specific what you want to do. Allegro can draw anything you want.

As long as you keep track of the state of each peg and each disk, then you can draw it any way you want. Start each frame by using al_clear_to_color, and then you could use al_draw_circle to draw the disks from top down, and as each one moves, update the display using al_flip_display.

OnlineCop
Member #7,919
October 2006
avatar

As long as you've already got the algorithm completed for calculating which peg to move a disk onto, you just need to determine how you plan to visualize the disks and pegs. Allegro 4 would be fine if you just want to render some sprites in a window somewhere.

Notice both animations on Wikipedia: {"name":"300px-Tower_of_Hanoi_4.gif","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/3\/f\/3f57f9930cbfd3cea96f39455cc0874b.gif","w":300,"h":111,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/3\/f\/3f57f9930cbfd3cea96f39455cc0874b"}300px-Tower_of_Hanoi_4.gif and 220px-Iterative_algorithm_solving_a_6_disks_Tower_of_Hanoi.gif

The former has disks rendered either in a "resting" state or a "hovering" state.

  • The resting state casts a hard shadow onto the disks (or ground) below it

  • The hovering state casts a soft shadow onto the disks (or empty space) it travels over

The latter has disks which simply "pop" onto the correct peg with no shadows, which doesn't show the disks "hovering" over the middle peg as it moves to the last peg.

Either of these approaches is fine, depending on how much work you want to put into animating everything.

One class will be dedicated to determining which disk to move, and where to move it to next.

  • This will need to calculate one disk movement at a time, after which it tells a rendering class to display that.

  • This will need a callback from that rendering class to know when it should proceed to calculate the next disk movement.

Another class will be dedicated to drawing those disks to screen and calling back to the first class when it has completed the "animation".

  • You really don't want to combine both the "calculation" and "rendering" into the same class.

  • You can have multiple versions of this class, ranging from "super-simple animation" to "bells-and-whistles effects".

#SelectExpand
1/// CalculationClass.h 2 3// Forward declaration so you don't have to #include the files here. 4class Peg; 5class RenderingClass; 6 7class CalculationClass { 8 private: 9 std::unique_ptr<RenderingClass> renderer_; 10 std::vector<Peg> pegs_; 11 // ... 12 13 public: 14 // ... 15 16 /** 17 * Calculate which disk should move onto which peg. 18 * 19 * This calls RenderingClass::MoveDisk(Disk diskToMove, 20 * Peg pegToMoveFrom, 21 * Peg pegToMoveTo); 22 * 23 * @returns true while there are more moves to make; false when 24 * simulation is done. 25 */ 26 bool MoveNextDisk(); 27 28 /** 29 * Callback for RenderingClass so we know to continue. 30 * 31 * This is called after RenderingClass has finished its animation 32 * so this class can calculate another move, or exit at the end 33 * of the simulation. 34 */ 35 std::function<void()> renderingCompletedCb; 36};

That way, CalculationClass can go idle while the RenderingClass does its thing, and RenderingClass can spend as much, or as little, time as it wants to show the disks moving around.

RmBeer2
Member #16,660
April 2017
avatar

I recommend you find out the algorithm on your own and avoid searching it on Google.
Otherwise you miss all the fun.

🌈🌈🌈 🌟 BlackRook WebSite (Only valid from my installer) 🌟 C/C++ 🌟 GNU/Linux 🌟 IceCream/Cornet 🌟 🌈🌈🌈

Rm Beer for Emperor 2021! Rm Beer for Ruinous Slave Drained 2022! Rm Beer for Traveler From The Future Warning Not To Enter In 2023! Rm Beer are building a travel machine for Go Back from 2023! Rm Beer in an apocalyptic world burning hordes of Zombies in 2024!

GullRaDriel
Member #3,861
September 2003
avatar

It smells the homework so bad that I have the feeling I'm back to college.

"Code is like shit - it only smells if it is not yours"
Allegro Wiki, full of examples and articles !!

Go to: