diff --git a/ThinkPink/mainwindow.cpp b/ThinkPink/mainwindow.cpp index f5d0e8f..4a8d037 100644 --- a/ThinkPink/mainwindow.cpp +++ b/ThinkPink/mainwindow.cpp @@ -4,9 +4,14 @@ extern std::unique_ptr settings; // slots -void MainWindow::buttonPressed(QPushButton *button, int row, int column) { +void MainWindow::buttonPressed(int row, int column) { QString text = "\u2605"; - button->setText(text); + 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); } @@ -28,6 +33,7 @@ void MainWindow::initialiseGame() { void MainWindow::initialiseGameGrid() { QSize button_size(max_width/columns, max_height/rows); + buttonGrid.clear(); for(int i = 0; i < rows; i++) { @@ -43,7 +49,7 @@ void MainWindow::initialiseGameGrid() { // Set size text etc. for each button connect(button, &QPushButton::clicked, [=](){ - buttonPressed(button, i, j); + buttonPressed(i, j); }); } buttonGrid.push_back(std::move(button_row)); diff --git a/ThinkPink/mainwindow.h b/ThinkPink/mainwindow.h index 73fc6ab..4890bdc 100644 --- a/ThinkPink/mainwindow.h +++ b/ThinkPink/mainwindow.h @@ -31,7 +31,7 @@ public: void initialiseMenuBar(); private slots: - void buttonPressed(QPushButton *button, int row, int column); + void buttonPressed(int row, int column); void newGame(); private: diff --git a/ThinkPink/samegame.cpp b/ThinkPink/samegame.cpp index 263f19a..f20fe4d 100644 --- a/ThinkPink/samegame.cpp +++ b/ThinkPink/samegame.cpp @@ -28,3 +28,26 @@ SameGame::SameGame(int rows, int columns) QString SameGame::getCell(int row, int column) { return gameMatrix[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); + } +} diff --git a/ThinkPink/samegame.h b/ThinkPink/samegame.h index 39232b3..04127b1 100644 --- a/ThinkPink/samegame.h +++ b/ThinkPink/samegame.h @@ -1,6 +1,7 @@ #ifndef SAMEGAME_H #define SAMEGAME_H +#include #include #include #include @@ -14,6 +15,7 @@ public: void initialiseGameMatrix(int rows, int columns); SameGame(int rows, int columns); QString getCell(int row, int column); + void getConnected(std::set> *connectedSet, QString color, int row, int column); }; #endif // SAMEGAME_H