|
|
|
|
@ -29,25 +29,25 @@ QString SameGame::getCell(int row, int column) {
|
|
|
|
|
return gameMatrix[row][column];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SameGame::checkNeighbour(std::set<std::vector<int>> *connectedSet, QString color, int row, int column) {
|
|
|
|
|
// check for grid boundaries
|
|
|
|
|
if (row > -1 && column > -1 && (unsigned long) row < gameMatrix.size() && (unsigned long) column < gameMatrix[0].size()) {
|
|
|
|
|
// check if already visited
|
|
|
|
|
if ( !(connectedSet->contains(std::vector<int>{row, column})) ) {
|
|
|
|
|
// check color
|
|
|
|
|
if (gameMatrix[row][column] == color)
|
|
|
|
|
getConnected(connectedSet, color, row, column);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SameGame::getConnected(std::set<std::vector<int>> *connectedSet, QString color, int row, int column) {
|
|
|
|
|
std::vector<int> cellCoords;
|
|
|
|
|
cellCoords.push_back(row);
|
|
|
|
|
cellCoords.push_back(column);
|
|
|
|
|
connectedSet->insert(cellCoords);
|
|
|
|
|
// check below
|
|
|
|
|
if ((unsigned long) row+1 < gameMatrix.size() && !(connectedSet->contains(std::vector<int>{row+1, column})) && gameMatrix[row+1][column] == color) {
|
|
|
|
|
getConnected(connectedSet, color, row+1, column);
|
|
|
|
|
}
|
|
|
|
|
// check above
|
|
|
|
|
if (row-1 > -1 && !(connectedSet->contains(std::vector<int>{row-1, column})) && gameMatrix[row-1][column] == color) {
|
|
|
|
|
getConnected(connectedSet, color, row-1, column);
|
|
|
|
|
}
|
|
|
|
|
// check right
|
|
|
|
|
if ((unsigned long) column+1 < gameMatrix[0].size() && !(connectedSet->contains(std::vector<int>{row, column+1})) && gameMatrix[row][column+1] == color) {
|
|
|
|
|
getConnected(connectedSet, color, row, column+1);
|
|
|
|
|
}
|
|
|
|
|
// check left
|
|
|
|
|
if ((unsigned long) column-1 > -1 && !(connectedSet->contains(std::vector<int>{row, column-1})) && gameMatrix[row][column-1] == color) {
|
|
|
|
|
getConnected(connectedSet, color, row, column-1);
|
|
|
|
|
}
|
|
|
|
|
checkNeighbour(connectedSet, color, row+1, column);
|
|
|
|
|
checkNeighbour(connectedSet, color, row-1, column);
|
|
|
|
|
checkNeighbour(connectedSet, color, row, column+1);
|
|
|
|
|
checkNeighbour(connectedSet, color, row, column-1);
|
|
|
|
|
}
|
|
|
|
|
|