- Home /
Tower of Hanoi style
Hi, I'm new to unity and I don't know the best way to store data about a game with similar mechanics to Tower of Hanoi. I have 3 empty game objects that act as the poles and 2 red blocks and 2 blue blocks (disks). How do I use Raycasts to store which blocks are where (They are all on a pole) at initialization and after each move. or is there a simpler way of achieving this. Thank you!
Answer by JDelekto · Apr 27, 2019 at 04:51 PM
I would probably create a couple of scripts. One script would hold the properties of the disc such as the color and its size for order checking (you could call the script 'Disc', for example). The second script I would apply to the poles and they would be a collection (array or list) of game objects which have the Disc script components.
If you go that route, no ray casting required, you just initialize each pole's collection with the disc game objects in your Start() method. When the user moves one disc from a pole to another, you'll have to get the properties of the disc being moved, check them with the top item in the collection on the pole to where it is being moved, then see if it can be moved. If it can, remove it from the collection on the originating pole and add it to what would be the "top" of the collection on the destination pole.
The mechanics of manipulating the discs can be however you like, but that pattern should be sufficient for a Tower of Hanoi simulation.
Thanks for the fast reply. Is it at all possible to see some pseudo code (similar). $$anonymous$$g. I'm not sure how the pole script would "communicate" with the disk script.
For a simple starter (Note, we will be working with empty game objects, you can render them as you see fit):
1) Create an empty game object, call it "Poles". 2) Create three empty game objects, call them "Pole 1", "Pole 2", "Pole 3", make them all direct children of "Poles". 3) Create an empty game object, call it "Discs". 4) Create two empty game objects, call them "Red Discs" and "Blue Discs", make them direct children of "Discs". 5) Create three empty game objects, call them "Red 1", "Red 2", "Red 3", make them direct children of "Red Discs". 6) Create three empty game objects, calle them "Blue 1", "Blue 2", "Blue 3", make them direct children of "Blue Discs".
Now, let's write the scripts.
7) Create the "PoleScript" script which will hold the disc game objects on that pole: 8) Create the "DiscScript" script which will hold the attributes of the disc, specifically their size and color:
Now let's apply the scripts to the objects:
9) Select Pole 1, Pole 2, Pole 3 and then drag and drop the "PoleScript" onto them in the Inspector. 10) Select Red 1, Red 2, Red 3, Blue 1, Blue 2, Blue 3 and then drag and drop the "DiscScript" onto them in the Inspector.
Now, let's color the discs:
11) Select Red 1, Red 2 and Red 3 and then change their color in the Disc Script to Red. 12) Select Blue 1, Blue 2 and Blue 3 and then change their color in the Disc Script to Blue.
Great, now let's set sizes (weights, diameters, measurements, etc.)
13) Select Red 1 and Blue 1 and type "1" for size property in the Disc script. 14) Select Red 2 and Blue 2 and type "2" for size property in the Disc script. 15) Select Red 3 and Blue 3 and type "3" for size property in the Disc script.
Now, the fun part, putting the discs on the poles:
Select Pole 1, drag and drop the Discs in the order you would like them to start initially onto the "Discs" property on the Pole Script, repeat the process for Poles 2 and 3. $$anonymous$$ake sure you set up the puzzle correctly.
I will leave the rest of the exercise up to you as to how you will the Disc game objects between the "Discs" collections on the Pole Objects script.
The Scripts:
// PoleScript.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PoleScript : $$anonymous$$onoBehaviour
{
public List<GameObject> discs;
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DiscScript : $$anonymous$$onoBehaviour
{
public int size;
public Color color;
}
Now, when it comes to getting access to script data attached to the GameObjects, just use poleObject.GetComponent() and discObject.GetComponent(). The Pole objects have a collection of Disc game objects, so it can iterate through them and manipulate the collection as it sees fit.
Thank so much, The set up is working fine and I've learnt a lot. But the scripts to interact and me trying to GetComponent aren't working but I'll keep trying. Thanks so much for all your help! You're a really good $$anonymous$$cher.
Where would the code about add and remove disc be? In pole script or disc script or a disc manager code that uses GetComponent to reference them? How would I reference the poles as they are game object ? I.e. add disc to the middle pole? How would I get it to go into that poles array? Thanks
If you're just looking for an example implementation of the towers of hanoi, have a look at my example. The whole game is just a single C# script with about 216 lines of code. It's just a pretty straight forward implementation without much setup required in Unity. All you need is to setup the 3 poles, reference them from the script and provide two AudioClips which should play when you pick up / drop a disc.
The game itself is controlled by the keys 1,2 and 3 for the corresponding pole.