The question is answered, right answer was accepted
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!
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.
Follow this Question
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