- Home /
Question is off-topic or not relevant
Puzzle Piece Match Mechanic
I need help figuring out how to make the match mechanic for each individual color. In this game you match 2 of the same color to gain points. Think of this game as puyo puyo at it's core foundation, or as in the industry we call Minimum Viable Products (MVP). I'm still having trouble with the flip mechanic for the pieces, but right now I'll focus on the match mechanic. What kind of code do I need in order to make the match 2 mechanic fully operational?
Answer by toddisarockstar · Jun 18, 2019 at 04:48 AM
Your code should use a 2D array to keep track of where things are. this way you can use x and y coordinates to look at squares and check things. when you check things you use a loop inside a loop to page through every coordinate. here is an example of setting up a grid of colors, setting some colors on the grid, and finally printing where the matches are:
public Color[,] c;
void Start () {
c = new Color[10, 10];
c [1, 2] = Color.red;
c [8, 8] = Color.green;
c [8, 2] = Color.blue;
c [1, 3] = Color.red;
findmatch ();
}
void findmatch(){
int x = 10;
while(x>1){x--;
int y = 10;
while(y>1){y--;
if(c[x,y]==c[x-1,y]||c[x,y]==c[x,y-1]){
if(c[x,y]!=new Color(0,0,0,0)){
print ("found match next to "+x+","+y);}
}
}} }
Thanks dude. By the time this is done and over with, I'm gonna make a tutorial of this on YouTube. Cause nobody has done one at all. Course there's something else. Forgive me, but here's some of the code for the grid:
public static int gridwidth = 4;
public static int gridheight = 12;
public static Transform[,] grid = new Transform[gridwidth, gridheight];
Any way to attach the color code to the sprites and grid?
you just use a custom class ins$$anonymous$$d of the color variable.
public class box {
Sprite sprite;
Color color;
public box (Sprite S, Color C){
sprite = S; color = C;
}
}
public box[,] c;
void Start () {
c = new box[10, 10];
c [1, 2] = new box (somesprite, Color.red);
c [8, 8] = new box (somesprite, Color.green);
}
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How do I force the player to press buttons in order? 2 Answers
Pattern Detection 1 Answer
Colour Matching Game: Search board for matches? 2 Answers
How to set different score values to different prefabs of an array? 0 Answers