Match Three Game help.

So, i am making a match three game in c++ and i need help with creating a matching function to check if three or four pieces are together.
I am using classes, one generic class and 6 derived classes to make the pieces.
After that i fill my table with the pieces.

1
2
3
4
5
6
piece* table[width][height];
     for(int i=0;i<width;i++){
	for(int j=0;j<height;j++){
		table[i][j] = fill();
	}
}


My fill() function goes into a switch and for each case (from 1 to 6) returns a new piece.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
piece* fill(){
	int i=0, z;
	for(i=0;i<10;i++){
		z=rand()%6+1;
		switch(z){
		case 1:
			{
				return new square;
				break;
                        }
                case 2:
			{
				return new ship;
				break;
                        }
...


After i fill the array, i draw the pieces inside of it.
1
2
3
4
5
for(int i=0;i<width;i++){
	for(int j=0;j<height;j++){
		table[i][j] -> draw_pieces(3+i,1+j);
	}
}


But now i need to check if there are two or three matching pieces in each row and column and here is my problem.


1
2
3
4
5
for(int i=0;i<width;i++){
	for(int j=0;j<height;j++){
		if(table[i][j]==table[i-1][j]&&table[i-1][j]==table[i-2][j]);
	}
}


But obviously this doesn´t work so i need a little guidance on where to go from here.
two or three matching pieces in each row and column


Could you explain farther what you mean.
Like in candy crush or bejeweled where if you have three or more pieces that are the same, they get deleted automatically by the program and replaced and because i am using an array i have rows and columns.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
void searchCol(piece* table[width][height])
{
    int consegativeRepeat = 0;
    
    for(auto i = 0; i < height; i++)
    {
        piece *key = table[0][i];   //first element in the column

        for(auto j = 1; j < width; j++)
        {
            if(table[i][j] == key)
            {
                ++consegativeRepeat;
            }
            else
            {
                key = table[i][j];    //new key
                consegativeRepeat = 0;  //new key, new count;
            }
            if(consegativeRepeat == 2)  //the key makes the 3rd consegative piece
            {
                //you have a match
                //TODO: replace previous 3 elements
            }
        }//move on to the next column and check for subsequent matches.
    }
}


Do same for searchRow .
Last edited on
1
2
3
4
5
6
7
for (int i = 0; i < width; ++i) {
    bool match = true;
    for (j=1; i<height; ++j) {
        match = match && *table[i][0] == *table[i][j];
    }
    // if match == true then the row matches.
}

You will probably need to define bool virtual piece::operator==(const piece &right);
deruku is not checking werther the whole column or row matches.
He wants to check if in the table, there is any combination of 3 consergative matching elements.

In addition to the code i wrote above, this is the patch for line 23 .

1
2
3
4
5
6
if(consegativeRepeat == 2)  //the key makes the 3rd consegative piece
{
   j-=2; //go back three places.
   for(auto k = j; k <= j+2; k++)
        table[i][k] = fill(); //fill last 3 elements of same column
}//j has gone three places back. this is important so the function rechecks the newly placed elements. 


Thank you shadowcode, now to try and make it work.
what do you mean
try and make it work.
? you mean main() ?
Topic archived. No new replies allowed.