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.
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.
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.
}
}
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 boolvirtual 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.