Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Hadrien · Jul 24, 2015 at 10:46 AM · javascriptscripting problemmultidimensional array

[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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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

On trigger turn off particles and lights make room dark,On trigger by FPC disable/destroy lights and particles 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges