Finished getConnected functionality

You can now fill all cells with stars that have at least one neighbour
main
Isabell Pflug 3 years ago
parent 5e4eac29a7
commit e5ad6e110f

@ -8,11 +8,12 @@ void MainWindow::buttonPressed(int row, int column) {
QString text = "\u2605"; QString text = "\u2605";
std::set<std::vector<int>> connected; std::set<std::vector<int>> connected;
game->getConnected(&connected, game->getCell(row, column), row, column); game->getConnected(&connected, game->getCell(row, column), row, column);
for (auto i = connected.begin(); i != connected.end(); i++) { if ( connected.size() > 1)
buttonGrid[(*i)[0]][(*i)[1]]->setText(text); for (auto i = connected.begin(); i != connected.end(); i++) {
} buttonGrid[(*i)[0]][(*i)[1]]->setText(text);
//button->setText(text); }
//buttonGrid[row][column+1 % columns]->setText(text); else
ui->statusBar->showMessage("Only friends get Stars!", 2000);
} }
void MainWindow::newGame() { void MainWindow::newGame() {

@ -121,6 +121,7 @@
</widget> </widget>
<addaction name="menuPlay"/> <addaction name="menuPlay"/>
</widget> </widget>
<widget class="QStatusBar" name="statusBar"/>
</widget> </widget>
<resources/> <resources/>
<connections/> <connections/>

@ -29,25 +29,25 @@ QString SameGame::getCell(int row, int column) {
return gameMatrix[row][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) { void SameGame::getConnected(std::set<std::vector<int>> *connectedSet, QString color, int row, int column) {
std::vector<int> cellCoords; std::vector<int> cellCoords;
cellCoords.push_back(row); cellCoords.push_back(row);
cellCoords.push_back(column); cellCoords.push_back(column);
connectedSet->insert(cellCoords); connectedSet->insert(cellCoords);
// check below checkNeighbour(connectedSet, color, row+1, column);
if ((unsigned long) row+1 < gameMatrix.size() && !(connectedSet->contains(std::vector<int>{row+1, column})) && gameMatrix[row+1][column] == color) { checkNeighbour(connectedSet, color, row-1, column);
getConnected(connectedSet, color, row+1, column); checkNeighbour(connectedSet, color, row, column+1);
} checkNeighbour(connectedSet, color, row, column-1);
// 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);
}
} }

@ -15,6 +15,7 @@ public:
void initialiseGameMatrix(int rows, int columns); void initialiseGameMatrix(int rows, int columns);
SameGame(int rows, int columns); SameGame(int rows, int columns);
QString getCell(int row, int column); QString getCell(int row, int column);
void checkNeighbour(std::set<std::vector<int>> *connectedSet, QString color, int row, int column);
void getConnected(std::set<std::vector<int>> *connectedSet, QString color, int row, int column); void getConnected(std::set<std::vector<int>> *connectedSet, QString color, int row, int column);
}; };

Loading…
Cancel
Save