Towers of Hanoi in C
Hello everyone. This is Madhurima Paruchuri and here to share about my code for game Towers of Hanoi in C.
Game Structure :
In this game I considered three rods as three places and named them as A, B, C and discs as rectangle boxes, numbered as 1, 2, 3 (as shown below).
Game Logic :
Coming to logic part , I took three arrays of size 3 for three places A, B, C where those arrays behaves as stack (array size is 3 because there are three boxes in the game). Initially, 'A' place array is filled with all box numbers 1,2,3 (as game starts with all boxes there) and arrays of places 'B' & 'C' with 0. If the box is moved to other place from current place then the box number gets popped (deleted) from current place array and pushed (added) to other place array following the rules of Towers of Hanoi. This process continues until all the boxes are moved to place 'C' i.e, 'C' place array should be contain all the box numbers 1,2,3 and arrays of places 'B' & 'C' with 0.
Game Designing :
Coming to drawing part , the whole structure containing places, boxes and names of boxes inside it is done by using windows specific methods. Windows specific methods are helpful in printing at whatever the place you want in the console. For eg: if I want to draw a line in the middle of console using '-', all I need to do is to give the right index of the point where I need to print (as console is divided into 2D array of indexes) before printing '-' as shown in screenshots below.
Here are some screenshots of code to draw a 3x4 rectangle in console and the game .
Code to draw a 3x4 rectangle using * :
Output will be like :
Similarly you can write text as below using right indexes in the above code , can draw shapes like square, circle, rectangle or even diagrams. All you need to do is keep track of indexing.
Screenshots of Game :
here is the link for my code :
https://github.com/paruchurimadhurima/C-Towers-of-Hanoi/blob/master/towers%20of%20hanoi.cpp
Thank you :)
Comments
Post a Comment