diff --git a/ThinkPink/mainwindow.cpp b/ThinkPink/mainwindow.cpp index 4a8d037..0a537d7 100644 --- a/ThinkPink/mainwindow.cpp +++ b/ThinkPink/mainwindow.cpp @@ -8,11 +8,12 @@ void MainWindow::buttonPressed(int row, int column) { QString text = "\u2605"; std::set> connected; game->getConnected(&connected, game->getCell(row, column), row, column); - 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); + if ( connected.size() > 1) + for (auto i = connected.begin(); i != connected.end(); i++) { + buttonGrid[(*i)[0]][(*i)[1]]->setText(text); + } + else + ui->statusBar->showMessage("Only friends get Stars!", 2000); } void MainWindow::newGame() { diff --git a/ThinkPink/mainwindow.ui b/ThinkPink/mainwindow.ui index 32c3a90..1bbc577 100644 --- a/ThinkPink/mainwindow.ui +++ b/ThinkPink/mainwindow.ui @@ -121,6 +121,7 @@ + diff --git a/ThinkPink/samegame.cpp b/ThinkPink/samegame.cpp index f20fe4d..867228a 100644 --- a/ThinkPink/samegame.cpp +++ b/ThinkPink/samegame.cpp @@ -29,25 +29,25 @@ QString SameGame::getCell(int row, int column) { return gameMatrix[row][column]; } +void SameGame::checkNeighbour(std::set> *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{row, column})) ) { + // check color + if (gameMatrix[row][column] == color) + getConnected(connectedSet, color, row, column); + } + } +} + void SameGame::getConnected(std::set> *connectedSet, QString color, int row, int column) { std::vector 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{row+1, column})) && gameMatrix[row+1][column] == color) { - getConnected(connectedSet, color, row+1, column); - } - // check above - if (row-1 > -1 && !(connectedSet->contains(std::vector{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{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{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); } diff --git a/ThinkPink/samegame.h b/ThinkPink/samegame.h index 04127b1..fdf8fdd 100644 --- a/ThinkPink/samegame.h +++ b/ThinkPink/samegame.h @@ -15,6 +15,7 @@ public: void initialiseGameMatrix(int rows, int columns); SameGame(int rows, int columns); QString getCell(int row, int column); + void checkNeighbour(std::set> *connectedSet, QString color, int row, int column); void getConnected(std::set> *connectedSet, QString color, int row, int column); };