Hi,
I make a matched-3 game but based on Tetris gameplay (cubes are dropped like Tetris to create row & columns of colored cubes), and I need to find a way to Check if there is 3 colored cubes in a row (or column).
The game logic is actually very simple, but I have a problem with Cubes Comparison.
- Storing the values
I create a multi dimensional Array, and I push Values for every Cube, by using a Loop :
cells[0, 0] // this is the Cube n°0, and it's X Position
cells[0, 1] // this is the Cube n°0, and it's Y Position
cells[0, 2] // this is the Cube n°0, and it's ID
cells[0, 3] // this is the Cube n°0, and it's Color
cells[1, 0] // this is the Cube n°1, and it's X Position
cells[1, 1] // this is the Cube n°1, and it's Y Position
cells[1, 2] // this is the Cube n°1, and it's ID
cells[1, 3] // this is the Cube n°1, and it's Color
…
// etc…
So, if I want the X Pos. of my Cube 1, I juste have to Call “cells[0, 0];”. This works perfectly fine.
- The Matching 3 Logic
The logic to find a 3 cubes Vertical match is simple:
var row : int;
var cell : int = 0;
for (row = 0; row < cells.GetLength(0); row++){
for (cell = 0; cell < cells.GetLength(1)-2; cell++){
if ( cells[row,cell] == cells[row,cell+1] == cells[row,cell+2] ) // The Problem is here, see my question below
{
print("Congrats, you have stacked 3 cubes in a column !");
// + Here I add my function to Destroy cubes and Collapse row(s)/col(s)
}
}
}
As you can see in my script, the line : “if ( cells[row,cell] == cells[row,cell+1] == cells[row,cell+2] )” is the problem.
What I (really) want to do:
if “CELL_X with Y position = 3” is equal to “CELL_X with Y position = 2 + 1” is equal to “CELL_X with Y position = 1 + 2”, then it’s a match.
- The problem
So, I can perfectly search for Vertical Cubes with the for loop, but my problem is in the line: “cells[row,cell] == cells[row,cell+1] == cells[row,cell+2]”.
Since if I wan’t “cell+1” to be “The Cell Pos Y value + 1”, the loop don’t find that value, but the next Value stored in the Array : Y Position, or CubeID, or Color ID…
Because if cell = 1, and I wan’t “cell+2”, cell+2 = cells[0, 2], so the result is: “the Cube n°0, and it’s ID”.
- The Question
How can I add a value to the Second int of Cells Array ?
Instead of:
cells[0, (1+1)] = Cube ID ( because result is: cells[0, 2] )
I want:
cells[0, (1+1)] = the value “Cube 0: Y Position” +1
So:
When I search for “if ( cells[row,cell+1] == 1 )”, I find the cube who has the number 1 as Pos Y Value.
I hope my explanation is good enough, if you need more details let me know. Thanks in advance for your help, and sorry for my poor english.