- Home /
[JS] Match 3 game - How to compare certain values stocked in a multi-dimensional Array
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.
1. 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.
2. 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.
3. 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".
4. 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.
Answer by TheFish657 · Dec 22, 2016 at 09:30 PM
I would change your multi-dimensional array so that you have a class called Cell which is system serializable and has a position Vector2, a colour and an int ID. Then make a 2D array of these Cells, made such that the index of each cell is its position on the grid, x and y. This will make checking easier now, as you can just do something like:
for (int x = 0; x > widthOfGrid;x++)
{
for (int y = 0; y > heightOfGrid;y++)
{
if (cells[x,y].id == cells[x+1, y] && cells[x,y] == cells[x+2,y])
{
//Do stuff
}
else if(cells[x,y].id == cells[x, y+1].id && cells[x,y].id == cells[x,y+2].id)
{
//Do stuff
}
}
}
This should work, tell me if there are any syntax errors
Your answer
Follow this Question
Related Questions
Is this "scene switcher" script set up the right way? 1 Answer
Why isn't my teleport on collision script working? 0 Answers
Building on android. 1 Answer
Multidimensional array in Javascript 0 Answers