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 /
This question was closed Jul 17, 2016 at 11:30 AM by Borjka for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Borjka · Jul 03, 2016 at 12:22 PM · c#logicgrid based game

Making square of blocks in match3 C#

Hi guys! Im with friend making Match3 game. We have fully working game you need to move pieces to make set of 3 same color or T and L shape. Our goal for now is to add square shape of 4 pieces. We tried many different ways and i'll explain some of what we discovered.

 public List<BGamePiece> GetMatch(BGamePiece piece, int newX, int newY)
     {
         if (piece.IsColored ()) {
             BColorPiece.Colortype color = piece.ColorComponent.Color;
             List<BGamePiece> horizontalPieces = new List<BGamePiece> ();
             List<BGamePiece> verticalPieces = new List<BGamePiece> ();
             List<BGamePiece> matchingPieces = new List<BGamePiece> ();
 
             //first check horizontal
             horizontalPieces.Add (piece);
 
             for (int dir = 0; dir <= 1; dir++) {
                 for (int x0ffset = 1; x0ffset < xDim; x0ffset++) {
                     int x;
 
                     if (dir == 0) {//left
                         x = newX - x0ffset;
                     } else {//right
                         x = newX + x0ffset;
                     }
 
                     if (x < 0 || x >= xDim) {
                         break;
                     }
 
                     if (pieces [x, newY].IsColored () && pieces [x, newY].ColorComponent.Color == color) {
                         horizontalPieces.Add (pieces [x, newY]);
                     } else {
                         break;
                     }
                 }
             }
 
             if (horizontalPieces.Count >= 3) {  
                 for (int i = 0; i < horizontalPieces.Count; i++) {
                     matchingPieces.Add (horizontalPieces [i]);
                 }
             } 
                 
 
 
             //traverse vertically  if we found a match (for L and T shape)
             if (horizontalPieces.Count >= 3) {
                 for (int i = 0; i < horizontalPieces.Count; i++) {
                     for (int dir = 0; dir <= 1; dir++) {
                         for (int y0ffset = 1; y0ffset < yDim; y0ffset++) {
                             int y;
 
                             if (dir == 0) { Debug.Log ("PPPPP");// up
                                 y = newY - y0ffset;
                             } else { //down
                                 y = newY + y0ffset;
                             }
 
                             if (y < 0 || y >= yDim) {
                                 break;
                             }
 
                             if (pieces [horizontalPieces [i].X, y].IsColored () && pieces [horizontalPieces [i].X, y].ColorComponent.Color == color) {
                                 verticalPieces.Add (pieces [horizontalPieces [i].X, y]);
                             } else {
                                 break;
                             }
                         }
                     }
                     if (verticalPieces.Count < 2) {
                         verticalPieces.Clear ();
                     } else {
                         for (int j = 0; j < verticalPieces.Count; j++) {
                             matchingPieces.Add (verticalPieces [j]);
                         }
                         break;
                     }
                 }
             }
 
             // TEST 1.0 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
 
             // END TEST 1.0!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
 
             if (matchingPieces.Count >= 3) {
                 return matchingPieces;
             } else {
                 matchingPieces.Clear ();
             }
             //didn't find anything  going horizontally first
             //so now check vertically
             horizontalPieces.Clear();
             verticalPieces.Clear();
             verticalPieces.Add (piece);
 
             for (int dir = 0; dir <= 1; dir++) {
                 for (int y0ffset = 1; y0ffset < yDim; y0ffset++) {
                     int y;
 
                     if (dir == 0) {
                         y = newY - y0ffset;
                     } else {
                         y = newY + y0ffset;
                     }
 
                     if (y < 0 || y >= yDim) {
                         break;
                     }
 
                     if (pieces [newX, y].IsColored () && pieces [newX, y].ColorComponent.Color == color) {
                         verticalPieces.Add (pieces [newX, y]);
                     } else {
                         break;
                     }
                 }
             }
                 
             if (verticalPieces.Count >= 3) { // tyt bbil edit 3 na 2
                 for (int i = 0; i < verticalPieces.Count; i++) {
                     matchingPieces.Add (verticalPieces [i]);
                 }
             }
                 
 
 
 
 
             // traverse horizontally if we found  a match (for L and T shape)
             if (verticalPieces.Count >= 3) {
                 for (int i = 0; i < verticalPieces.Count; i++) {
                     for (int dir = 0; dir <= 1; dir++) {
                         for (int x0ffset = 1; x0ffset < xDim; x0ffset++) {
                             int x;
 
                             if (dir == 0) { //left
                                 x = newX - x0ffset;
                             
                             } else { //right
                                 x = newX + x0ffset;
                             }
 
                             if (x < 0 || x >= xDim) {
                                 break;
                             }
 
                             if (pieces [x, verticalPieces[i].Y].IsColored () && pieces [x, verticalPieces[i].Y].ColorComponent.Color == color) {
                                 horizontalPieces.Add (pieces [x, verticalPieces [i].Y]);
                             } else {
                                 break;
                             }
                         }
                     }
 
                     if (horizontalPieces.Count < 2) {
                         horizontalPieces.Clear ();
                     } else {
                         for (int j = 0; j < horizontalPieces.Count; j++) {
                             matchingPieces.Add (horizontalPieces [j]);
                         }
                         break;
                     }
                 }
             }
 
             // START !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
 
 
             /*if (verticalPieces.Count == 2) {
                 for (int i = 0; i < 2; i++) {
                     for (int dir = 0; dir <= 2; dir++) {
                         for (int x0ffset = 1; x0ffset < 2; x0ffset++) {
                             int x;
 
                             if (dir == 0) {
                                 x = newY - x0ffset;
                             } else {
                                 x = newY + x0ffset;
                             } 
 
                             if (x < 0 || x >= yDim) {
                                 break;
                             }
 
                             if (pieces [x, verticalPieces[i].Y].IsColored () && pieces [x, verticalPieces[i].Y].ColorComponent.Color == color) {
                                 horizontalPieces.Add (pieces [x, verticalPieces[i].Y]);
                                 Debug.Log ("asdasda");
                             } else {
                                 break;
                             }
 
 
                         }
                     }
                     if(horizontalPieces.Count >= 3) {
                         for (int j = 0; j < horizontalPieces.Count; j++) {
                             matchingPieces.Add (horizontalPieces [j]);
                         }
                         break;
                     }
 
                 }
             }*/
 
             //END !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
 
             if (matchingPieces.Count >= 3) {
                 return matchingPieces;
             }else {
                 matchingPieces.Clear ();
             }
 
         }
 
         return null;
     }

this is a part that check shapes, rows and columns. We've tried to make square check of L and T shape code, we have reached a point where we got squares pop but there was some mistakes that been causing some indiviual pieces to pop or wierd shapes.

Sorry if i missed something to show just ask me if needed, thanks!

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

  • Sort: 
avatar image
0
Best Answer

Answer by Borjka · Jul 17, 2016 at 11:28 AM

 if (horizontalPieces.Count == 2) {
                 for (int i = 0; i < horizontalPieces.Count; i++) {
                     for (int dir = 0; dir <= 1; dir++) {
                         for (int yOffset = 1; yOffset < yDim; yOffset++) {
                             int y;
 
                             if (dir == 0) {
                                 y = newY - yOffset;
                             } else {
                                 y = newY + yOffset;
                             }
 
                             if (y < 0 || y >= yDim) {
                                 break;
                             }
 
                             if (pieces [horizontalPieces [0].X, y].IsColored () && pieces [horizontalPieces [0].X, y].ColorComponent.Color == color && 
                                 pieces [horizontalPieces [1].X, y].IsColored () && pieces [horizontalPieces [1].X, y].ColorComponent.Color == color) {
                                 cubePieces.Add (pieces [horizontalPieces [0].X, y]);
                                 cubePieces.Add (pieces [horizontalPieces [1].X, y]);
                             } else {
                                 break;
                             }
                         }
                     }
 
                     if (cubePieces.Count != 4) {
                         cubePieces.Clear ();
                     } else {
                         for (int j = 0; j < cubePieces.Count; j++) {
                             matchingPieces.Add (cubePieces [j]);
                         }
                         break;
                     }
                 }
             }
 
             if (matchingPieces.Count >= 3) {
                 return matchingPieces;
             }


Okay guys, if anyone will be interested, we've done that simply by using coodinates manually, which we couldn't achieve earlier because we used some wrong logic that been causing wrong figures to pop up. Im pretty sure this is ain't only one and for sure not best solution for our question but it works perfecto! In code you can see check for square of horizontal matches, also we got same mirrored code for vertical. Thanks.

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

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Simple `Match 3+ in a row` pattern 0 Answers

switch case statment 0 Answers

How do I create a 2D grid using MonoDevelop? 1 Answer

Problem with the logic in my code 0 Answers

How to create a Matrix / Array / List / Grid of gameObjects? 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