🚧 Added basic cell group functionality (not entirely working yet)

main
Isabell Pflug 2 years ago
parent 6d40409314
commit 5e4eac29a7

@ -4,9 +4,14 @@
extern std::unique_ptr<QSettings> 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<std::vector<int>> 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));

@ -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:

@ -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<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);
}
}

@ -1,6 +1,7 @@
#ifndef SAMEGAME_H
#define SAMEGAME_H
#include <set>
#include <vector>
#include <QSettings>
#include <QString>
@ -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<std::vector<int>> *connectedSet, QString color, int row, int column);
};
#endif // SAMEGAME_H

Loading…
Cancel
Save