diff --git a/ThinkPink/mainwindow.cpp b/ThinkPink/mainwindow.cpp index 4503eec..d23f581 100644 --- a/ThinkPink/mainwindow.cpp +++ b/ThinkPink/mainwindow.cpp @@ -18,23 +18,51 @@ void MainWindow::clearConnected(std::set> *connected) { game->deleteCell((*i)[0], (*i)[1]); } refreshButtonGrid(); + qApp->processEvents(); } 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 j = 0; j < columns; j++) { buttonGrid[i][j]->setStyleSheet("color: #FFFFFF; Font : 30pt; background-color: " + game->getCell(i,j) + "; border: none;"); 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> *connected) { + usleep(400000); + for (auto i = connected->begin(); i != connected->end(); i++) { + bubbleUp((*i)[0], (*i)[1]); + } + refreshButtonGrid(); +} + // slots void MainWindow::buttonPressed(int row, int column) { std::set> connected; game->getConnected(&connected, game->getCell(row, column), row, column); if ( connected.size() > 1) { clearConnected(&connected); + bubbleBoxes(&connected); } else ui->statusBar->showMessage("Only friends get Stars!", 2000); } @@ -87,22 +115,31 @@ void MainWindow::initialiseWindow() { void MainWindow::initialiseMenuBar() { QAction *new_game_action = ui->menuPlay->addAction("New Game"); - connect(new_game_action, SIGNAL(triggered()), this, SLOT(newGame())); 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) : QMainWindow(parent) , ui(new Ui::MainWindow) , rows(settings->value("size/rows", 9).toInt()) , columns(settings->value("size/columns", 9).toInt()) , max_width(800) - , max_height(544) + , max_height(519) { 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); initialiseGame(); initialiseWindow(); diff --git a/ThinkPink/mainwindow.h b/ThinkPink/mainwindow.h index 41996b5..56ddd8e 100644 --- a/ThinkPink/mainwindow.h +++ b/ThinkPink/mainwindow.h @@ -31,6 +31,9 @@ public: void initialiseMenuBar(); void clearConnected(std::set> *connected); void refreshButtonGrid(); + void refreshSizes(); + void bubbleUp(int row, int column); + void bubbleBoxes(std::set> *connected); private slots: void buttonPressed(int row, int column); @@ -40,8 +43,8 @@ private: Ui::MainWindow *ui; const int rows; const int columns; - const double max_width; - const double max_height; + double max_width; + double max_height; SameGame *game; std::vector> buttonGrid; }; diff --git a/ThinkPink/samegame.cpp b/ThinkPink/samegame.cpp index 7a04350..d95b07b 100644 --- a/ThinkPink/samegame.cpp +++ b/ThinkPink/samegame.cpp @@ -29,6 +29,10 @@ QString SameGame::getCell(int row, int column) { return gameMatrix[row][column]; } +void SameGame::setCell(int row, int column, QString color) { + gameMatrix[row][column] = color; +} + 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()) { diff --git a/ThinkPink/samegame.h b/ThinkPink/samegame.h index 62a1738..3b1af82 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 setCell(int row, int column, QString color); void checkNeighbour(std::set> *connectedSet, QString color, int row, int column); void getConnected(std::set> *connectedSet, QString color, int row, int column); void deleteCell(int row, int column);