- Home /
I'm sure this algorithm is correct...yet its not performing its function properly..
I have three scripts working together. The first spawns a 3-d grid of objects (in memory as a 3-dimensional array[6,6,6]) arranged in a cube, with the center left open for an animated core.
The second checks to see if there are 3 or more in a row on the surface of the cube, and if it finds 3 or more it puts then into a stack to be destroyed.
The third script contains a function to receive this stack and destroy the the objects in the stack(the stack actually contains their transforms, which are used to find their positions in the array and delete the objects directly from the array(their position is the same as their index.)
However, I have written the matching script three times from scratch and I still find myself running into the problem where it is just simply not getting rid of the objects when their tags match. It will destroy some, but not all. Ive been over my algorithm so many times i can nearly recite it; my assumption now is that I must be overlooking a quirk in the engine.
I have attached the three scripts below..and thanks in advance to anyone who may take the time to glance over this. I know it is rather long, admittedly sloppy, and poorly commented.
The input statement is purely for testing purposes.
First is the spawning script:
using UnityEngine; using System.Collections;
public class GemSpawnControl : MonoBehaviour { public Matcher Matching; public GameObject gem1, gem2, gem3, gem4; public GameObject[, ,] gemArray = new GameObject[6, 6, 6];
int num;
void Start()
{
//Random.seed = 20;
gemArray.Initialize();
}
void Update()
{
if (Input.GetButton("Fire1"))
Spawn();
}
void Spawn()
{
for (int m = 0; m < 200; m++)
{
do
{
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < 6; j++)
{
for (int k = 0; k < 6; k++)
{
if ((i != 2 && i != 3) || (j != 2 && j != 3) || (k != 2 && k != 3))
{
num = Random.Range(0, 3);
if (!gemArray[i, j, k])
{
switch (num)
{
case 0:
gemArray[i, j, k] = (GameObject)Instantiate(gem1, new Vector3(i, j, k), Quaternion.identity);
break;
case 1:
gemArray[i, j, k] = (GameObject)Instantiate(gem2, new Vector3(i, j, k), Quaternion.identity);
break;
case 2:
gemArray[i, j, k] = (GameObject)Instantiate(gem3, new Vector3(i, j, k), Quaternion.identity);
break;
case 3:
gemArray[i, j, k] = (GameObject)Instantiate(gem4, new Vector3(i, j, k), Quaternion.identity);
break;
}
}
//Debug.Log(gemArray[i, j, k] + " " + i+" "+j+" "+k);
}
else
{
gemArray[i, j, k] = null;
}
}
}
}
//Debug.Log(Matching.Match());
}
while (Matching.Match());
}
// Destroy(gemArray[5, 5, 5]);
// Destroy(gemArray[0, 0, 0]);
}
}
Next is the Matching class.
using UnityEngine; using System.Collections; using System.Collections.Generic;
public class Matcher : MonoBehaviour { public GemSpawnControl Spawner; public Destroyer Destruction; Stack <GameObject> current = new Stack<GameObject>(); Stack <Transform> destroyStack = new Stack<Transform>();
bool changed;
public bool Match()
{
changed = false;
horizontal();
vertical();
Destruction.DestroyMe(destroyStack);
return changed;
}
void StackUp()
{
if (current.Count > 4)
{
while (current.Count > 0)
destroyStack.Push(current.Pop().transform);
changed = true;
}
if (current.Count > 3)
{
while (current.Count > 0)
destroyStack.Push(current.Pop().transform);
changed = true;
}
if (current.Count > 2)
{
while (current.Count > 0)
destroyStack.Push(current.Pop().transform);
changed = true;
}
else
{
current.Clear();
}
//Debug.Log(destroyStack.Count);
}
void horizontal()
{
int x = 0, y = 0, z = 0;
for (y = 0; y < 6; y++)
{
for (x = 0; x < 6; x++)
{
if (current.Count == 0)
current.Push(Spawner.gemArray[x, y, z]);
else if (current.Peek().tag == Spawner.gemArray[x, y, z].tag)
current.Push(Spawner.gemArray[x, y, z]);
else StackUp();
}
StackUp();
x = 5;
for (z = 0; z < 6; z++)
{
if (current.Count == 0)
current.Push(Spawner.gemArray[x, y, z]);
else if (current.Peek().tag == Spawner.gemArray[x, y, z].tag)
current.Push(Spawner.gemArray[x, y, z]);
else StackUp();
}
StackUp();
z = 5;
for (x = 5; x > -1; x--)
{
if (current.Count == 0)
current.Push(Spawner.gemArray[x, y, z]);
else if (current.Peek().tag == Spawner.gemArray[x, y, z].tag)
current.Push(Spawner.gemArray[x, y, z]);
else StackUp();
}
StackUp();
x = 0;
for (z = 5; z > -1; z--)
{
if (current.Count == 0)
current.Push(Spawner.gemArray[x, y, z]);
else if (current.Peek().tag == Spawner.gemArray[x, y, z].tag)
current.Push(Spawner.gemArray[x, y, z]);
else StackUp();
}
StackUp();
z = 0;
}
}
void vertical()
{
int x = 0, y = 0, z = 0;
for (x = 0; x < 6; x++)
{
for (y = 0; y < 6; y++)
{
if (current.Count == 0)
current.Push(Spawner.gemArray[x, y, z]);
else if (current.Peek().tag == Spawner.gemArray[x, y, z].tag)
current.Push(Spawner.gemArray[x, y, z]);
else StackUp();
}
StackUp();
y = 5;
for (z = 0; z < 6; z++)
{
if (current.Count == 0)
current.Push(Spawner.gemArray[x, y, z]);
else if (current.Peek().tag == Spawner.gemArray[x, y, z].tag)
current.Push(Spawner.gemArray[x, y, z]);
else StackUp();
}
StackUp();
z = 5;
for (y = 5; y > -1; y--)
{
if (current.Count == 0)
current.Push(Spawner.gemArray[x, y, z]);
else if (current.Peek().tag == Spawner.gemArray[x, y, z].tag)
current.Push(Spawner.gemArray[x, y, z]);
else StackUp();
}
StackUp();
y = 0;
for (z = 5; z > -1; z--)
{
if (current.Count == 0)
current.Push(Spawner.gemArray[x, y, z]);
else if (current.Peek().tag == Spawner.gemArray[x, y, z].tag)
current.Push(Spawner.gemArray[x, y, z]);
else StackUp();
}
StackUp();
z = 0;
}
x = 5; y = 0; z = 0;
for (x = 0; x < 6; x++)
{
for (y = 0; y < 6; y++)
{
if (current.Count == 0)
current.Push(Spawner.gemArray[x, y, z]);
else if (current.Peek().tag == Spawner.gemArray[x, y, z].tag)
current.Push(Spawner.gemArray[x, y, z]);
else StackUp();
}
StackUp();
y = 5;
for (x = 5; x > -1; x--)
{
if (current.Count == 0)
current.Push(Spawner.gemArray[x, y, z]);
else if (current.Peek().tag == Spawner.gemArray[x, y, z].tag)
current.Push(Spawner.gemArray[x, y, z]);
else StackUp();
}
StackUp();
x = 0;
for (y = 5; y > -1; y--)
{
if (current.Count == 0)
current.Push(Spawner.gemArray[x, y, z]);
else if (current.Peek().tag == Spawner.gemArray[x, y, z].tag)
current.Push(Spawner.gemArray[x, y, z]);
else StackUp();
}
StackUp();
y = 0;
for (x = 0; x < 6; x++)
{
if (current.Count == 0)
current.Push(Spawner.gemArray[x, y, z]);
else if (current.Peek().tag == Spawner.gemArray[x, y, z].tag)
current.Push(Spawner.gemArray[x, y, z]);
else StackUp();
}
StackUp();
x = 5;
}
}
}
Finally the destruction class(very simple, is separate for modularity's sake)
using UnityEngine; using System.Collections; using System.Collections.Generic;
public class Destroyer : MonoBehaviour {
public GemSpawnControl spawner;
public void DestroyMe(Stack<Transform> kill)
{
while (kill.Count>0)
{
//Debug.Log(spawner.gemArray[(int)kill.Peek().position.x, (int)kill.Peek().position.y, (int)kill.Peek().position.z]);
Destroy(spawner.gemArray[(int)kill.Peek().position.x, (int)kill.Peek().position.y, (int)kill.Peek().position.z]);
spawner.gemArray[(int)kill.Peek().position.x, (int)kill.Peek().position.y, (int)kill.Pop().position.z] = null;
}
}
}
seemed to be the best way to iterate through three dimensions
Answer by tertle · Mar 02, 2011 at 01:39 AM
Can you try something for me
change
Destroy(spawner.gemArray[(int)kill.Peek().position.x, (int)kill.Peek().position.y, (int)kill.Peek().position.z]);
spawner.gemArray[(int)kill.Peek().position.x, (int)kill.Peek().position.y, (int)kill.Pop().position.z] = null;
Into
Vector3 pos = kill.Pop().position;
Destroy(spawner.gemArray[(int)pos.x, (int)pos.y, (int)pos.z]); spawner.gemArray[(int)pos.x, (int)pos.y, (int)pos.z] = null;
I'm not sure how the compiler would behhave when you have a pop and peek in same statement, if the pop is necessarily going to come last or not.
Just try that out.
That made no difference, and I've also just confirmed that passing a stack with a single transform in it did destroy the correct item.
Your answer
Follow this Question
Related Questions
How to destroy the first clone of a UI Image using an array every time I press a button? 1 Answer
How to destroy all the items with one function? 2 Answers
Destroy Particle System in an Array 0 Answers
How to destroy an instantiated object when I touch it? 0 Answers
Check times a value is next to the same value in an array 4 Answers