🎉 IT WORKS

main
Isabell Pflug 1 year ago
parent 2f5984e36c
commit ecf2c9f555

@ -18,15 +18,42 @@ void MainWindow::clearConnected(std::set<std::vector<int>> *connected) {
game->deleteCell((*i)[0], (*i)[1]); game->deleteCell((*i)[0], (*i)[1]);
} }
refreshButtonGrid(); refreshButtonGrid();
qApp->processEvents();
} }
void MainWindow::refreshButtonGrid() { void MainWindow::refreshButtonGrid() {
QSize button_size(max_width/columns, max_height/rows);
QLayoutItem *wItem;
while ((wItem = ui->gameGridLayout->takeAt(0)) != 0)
delete wItem;
for (int i = 0; i < rows; i++) { for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) { for (int j = 0; j < columns; j++) {
buttonGrid[i][j]->setStyleSheet("color: #FFFFFF; Font : 30pt; background-color: " + game->getCell(i,j) + "; border: none;"); buttonGrid[i][j]->setStyleSheet("color: #FFFFFF; Font : 30pt; background-color: " + game->getCell(i,j) + "; border: none;");
buttonGrid[i][j]->setText(""); buttonGrid[i][j]->setText("");
buttonGrid[i][j]->setFixedSize(button_size);
ui->gameGridLayout->addWidget(buttonGrid[i][j], i, j);
}
}
}
void MainWindow::bubbleUp(int row, int column) {
if (row > 0) {
QPushButton *tbutton = std::move(buttonGrid[row][column]);
buttonGrid[row][column] = std::move(buttonGrid[row-1][column]);
buttonGrid[row-1][column] = std::move(tbutton);
QString tcolor = game->getCell(row, column);
game->setCell(row, column, game->getCell(row-1, column));
game->setCell(row-1, column, tcolor);
bubbleUp(row-1, column);
} }
}
void MainWindow::bubbleBoxes(std::set<std::vector<int>> *connected) {
usleep(400000);
for (auto i = connected->begin(); i != connected->end(); i++) {
bubbleUp((*i)[0], (*i)[1]);
} }
refreshButtonGrid();
} }
// slots // slots
@ -35,6 +62,7 @@ void MainWindow::buttonPressed(int row, int column) {
game->getConnected(&connected, game->getCell(row, column), row, column); game->getConnected(&connected, game->getCell(row, column), row, column);
if ( connected.size() > 1) { if ( connected.size() > 1) {
clearConnected(&connected); clearConnected(&connected);
bubbleBoxes(&connected);
} else } else
ui->statusBar->showMessage("Only friends get Stars!", 2000); ui->statusBar->showMessage("Only friends get Stars!", 2000);
} }
@ -87,22 +115,31 @@ void MainWindow::initialiseWindow() {
void MainWindow::initialiseMenuBar() { void MainWindow::initialiseMenuBar() {
QAction *new_game_action = ui->menuPlay->addAction("New Game"); QAction *new_game_action = ui->menuPlay->addAction("New Game");
connect(new_game_action, SIGNAL(triggered()), this, SLOT(newGame())); connect(new_game_action, SIGNAL(triggered()), this, SLOT(newGame()));
ui->menuPlay->addSeparator(); ui->menuPlay->addSeparator();
} }
void MainWindow::refreshSizes() {
max_width = MainWindow::width();
max_height = MainWindow::height() - ui->menuBar->height() - ui->lcdScore->height() - ui->statusBar->height();
}
MainWindow::MainWindow(QWidget *parent) MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent) : QMainWindow(parent)
, ui(new Ui::MainWindow) , ui(new Ui::MainWindow)
, rows(settings->value("size/rows", 9).toInt()) , rows(settings->value("size/rows", 9).toInt())
, columns(settings->value("size/columns", 9).toInt()) , columns(settings->value("size/columns", 9).toInt())
, max_width(800) , max_width(800)
, max_height(544) , max_height(519)
{ {
ui->setupUi(this); ui->setupUi(this);
this->setStyleSheet("color: #DB7093;"); QPalette pal = QPalette();
pal.setColor(QPalette::Window, 0xfff0f3);
pal.setColor(QPalette::Text, 0xa4133c);
this->setPalette(pal);
ui->menuPlay->setPalette(pal);
ui->menuBar->setStyleSheet("QMenuBar::item {color: #a4133c}");
refreshSizes();
game = new SameGame(rows, columns); game = new SameGame(rows, columns);
initialiseGame(); initialiseGame();
initialiseWindow(); initialiseWindow();

@ -31,6 +31,9 @@ public:
void initialiseMenuBar(); void initialiseMenuBar();
void clearConnected(std::set<std::vector<int>> *connected); void clearConnected(std::set<std::vector<int>> *connected);
void refreshButtonGrid(); void refreshButtonGrid();
void refreshSizes();
void bubbleUp(int row, int column);
void bubbleBoxes(std::set<std::vector<int>> *connected);
private slots: private slots:
void buttonPressed(int row, int column); void buttonPressed(int row, int column);
@ -40,8 +43,8 @@ private:
Ui::MainWindow *ui; Ui::MainWindow *ui;
const int rows; const int rows;
const int columns; const int columns;
const double max_width; double max_width;
const double max_height; double max_height;
SameGame *game; SameGame *game;
std::vector<std::vector<QPushButton *>> buttonGrid; std::vector<std::vector<QPushButton *>> buttonGrid;
}; };

@ -29,6 +29,10 @@ QString SameGame::getCell(int row, int column) {
return gameMatrix[row][column]; return gameMatrix[row][column];
} }
void SameGame::setCell(int row, int column, QString color) {
gameMatrix[row][column] = color;
}
void SameGame::checkNeighbour(std::set<std::vector<int>> *connectedSet, QString color, int row, int column) { void SameGame::checkNeighbour(std::set<std::vector<int>> *connectedSet, QString color, int row, int column) {
// check for grid boundaries // check for grid boundaries
if (row > -1 && column > -1 && (unsigned long) row < gameMatrix.size() && (unsigned long) column < gameMatrix[0].size()) { if (row > -1 && column > -1 && (unsigned long) row < gameMatrix.size() && (unsigned long) column < gameMatrix[0].size()) {

@ -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 setCell(int row, int column, QString color);
void checkNeighbour(std::set<std::vector<int>> *connectedSet, QString color, 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);
void deleteCell(int row, int column); void deleteCell(int row, int column);

Loading…
Cancel
Save