|
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. |
GullRaDriel
Member #3,861
September 2003
|
Google it. "Code is like shit - it only smells if it is not yours" |
Edgar Reynaldo
Major Reynaldo
May 2007
|
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. My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
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... 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 " |
GullRaDriel
Member #3,861
September 2003
|
That's a part of my signature "Code is like shit - it only smells if it is not yours" |
Edgar Reynaldo
Major Reynaldo
May 2007
|
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. My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
OnlineCop
Member #7,919
October 2006
|
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"} and The former has disks rendered either in a "resting" state or a "hovering" state.
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.
Another class will be dedicated to drawing those disks to screen and calling back to the first class when it has completed the "animation".
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
|
I recommend you find out the algorithm on your own and avoid searching it on Google. 🌈🌈🌈 🌟 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
|
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" |
|