Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by MrReynevan2 · Jul 31, 2016 at 03:03 PM · c#algorithmlogicpuzzle

Simple `Match 3+ in a row` pattern

I'm trying to create a simple minigame for my project that requires player to match 3+ objects in a row/column to get points. Currently I'm at a loss as to why it sometimes works, and sometimes not.

Also, more often than not my script matches blocks that it shouldn't.

Basically, I'm storing all blocks in a MinigameBlock[,] array, then projecting it on the scene. After that every move on the scene is refflected back on an array, then the function for matching rows kicks in:

 public void CheckForRows()
 {
     var list = new List<MinigameBlock>();
     for (int y = blocksArray.GetLength(1) - 1; y >= 0; y--)
         for (int x = blocksArray.GetLength(0) - 1; x >= 0; x--)
         {
             MinigameBlock[] r = null;
             if (blocksArray[x, y] != null)
                 r = blocksArray[x, y].Check();
             if (r != null) list.AddRange(r);
         }
     foreach (var obj in list.Distinct())
         Destroy(obj.gameObject); //For now
 }

Here's the Check() function of MinigameBlock:

 public MinigameBlock[] Check()
 {
     var t = type;
     List<MinigameBlock> blocks = new List<MinigameBlock>();
     var pos = manager.blocksArray.FindIndex(this);
     if (pos[0] > 1)
     {
         var _blocks = new List<MinigameBlock>();
         _blocks.Add(this);
         var match = 1;
         for (int i = 1; i <= 2; i++)
             if (manager.blocksArray[pos[0] - i, pos[1]] != null && manager.blocksArray[pos[0] - i, pos[1]].type == t)
             {
                 match++;
                 _blocks.Add(manager.blocksArray[pos[0] - i, pos[1]]);
             }
             else break;
         if (match >= 3)
             for (int i = pos[0]; i < manager.blocksArray.GetLength(0); i++)
                 if (manager.blocksArray[i, pos[1]] != null && manager.blocksArray[i, pos[1]].type == t)
                 {
                     match++;
                     _blocks.Add(manager.blocksArray[i, pos[1]]);
                 }
                 else break;
         if (_blocks.Count >= 3)
             blocks.AddRange(_blocks);
     }

(This is only for horizontal matching for now)

Also, here is my butchered attempt at populating the board with blocks:

 void InstantiateBlocks()
 {
     var parent = GameObject.Find("Table").transform;
     int lastId = 0, sameBlock = 0;
     int _x = 0, _y = 0;
     for (int y = -2; y <= 3; y++)
     {
         for (int x = -4; x < 4; x++)
         {
             if (Random.Range(0f, 1f) < 0.8)
             {
                 if (sameBlock == 0)
                     lastId = Random.Range(0, blocks.Count);
                 else if (sameBlock >= Random.Range(1, 3))
                 {
                     sameBlock = 1;
                     var _id = lastId;
                     while (lastId == _id)
                         lastId = Random.Range(0, blocks.Count);
                 }
                 else sameBlock--;
                 var b = Instantiate(blocks[lastId]);
                 b.transform.SetParent(parent, false);
                 b.transform.position = new Vector3(x, y, 0f);
                 blocksArray[_x, _y] = b.GetComponent<MinigameBlock>();
                 sameBlock++;
             }
             _x++;
         }
         _y++;
         _x = 0;
     }
 }

What am I doing wrong, aside from the fact that the code needs craploads of optimization?

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

0 Replies

· Add your reply
  • Sort: 

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Detect all object are touching as one cluster 0 Answers

How to determine the number of bubbles that share that same color in a cluster and return as a list? 1 Answer

Area filling like Paper.io using fllod fill algorithm 0 Answers

Coloring encircled tiles 0 Answers

Avoid changes if I dont changing my position. 1 Answer


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