From 232174b30985ca21768e9df156291cf994c75fed Mon Sep 17 00:00:00 2001 From: Isabell Pflug Date: Wed, 24 May 2023 23:33:35 +0200 Subject: [PATCH] :recycle: Moved core game mechanic to its own class --- assignments/06/ThinkPink/ThinkPink.pro | 6 +++-- assignments/06/ThinkPink/mainwindow.cpp | 36 +++---------------------- assignments/06/ThinkPink/mainwindow.h | 7 +++-- assignments/06/ThinkPink/samegame.cpp | 30 +++++++++++++++++++++ assignments/06/ThinkPink/samegame.h | 19 +++++++++++++ 5 files changed, 59 insertions(+), 39 deletions(-) create mode 100644 assignments/06/ThinkPink/samegame.cpp create mode 100644 assignments/06/ThinkPink/samegame.h diff --git a/assignments/06/ThinkPink/ThinkPink.pro b/assignments/06/ThinkPink/ThinkPink.pro index 91ff8a2..424af8b 100644 --- a/assignments/06/ThinkPink/ThinkPink.pro +++ b/assignments/06/ThinkPink/ThinkPink.pro @@ -8,10 +8,12 @@ DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs depr SOURCES += \ main.cpp \ - mainwindow.cpp + mainwindow.cpp \ + samegame.cpp HEADERS += \ - mainwindow.h + mainwindow.h \ + samegame.h FORMS += \ mainwindow.ui diff --git a/assignments/06/ThinkPink/mainwindow.cpp b/assignments/06/ThinkPink/mainwindow.cpp index 3bc05d1..c0b4cf3 100644 --- a/assignments/06/ThinkPink/mainwindow.cpp +++ b/assignments/06/ThinkPink/mainwindow.cpp @@ -5,19 +5,12 @@ extern std::unique_ptr settings; // event functions void MainWindow::buttonPressed(QPushButton *button) { - QString text = "owo"; + QString text = "\u2605"; button->setText(text); } // initialisation functions void MainWindow::initialiseGameGrid() { - qDebug() << "Number of rows: " << n_rows; - qDebug() << "Number of columns: " << n_columns; - qDebug() << "Colors: "; - for (long long i = 0; i < colors.size(); i++) { - qDebug() << colors.value(i); - } - double max_width = MainWindow::width(); double max_height = MainWindow::height() - ui->menuBar->height() - ui->labelScore->height(); @@ -33,7 +26,7 @@ void MainWindow::initialiseGameGrid() { { QPushButton * button = new QPushButton(this); button->setFixedSize(button_size); - button->setStyleSheet("background-color: " + gameMatrix[j][i] + "; border: none;"); + button->setStyleSheet("background-color: " + game.getCell(i,j) + "; border: none;"); ui->gameGridLayout->addWidget(button, j, i); // Set size text etc. for each button @@ -45,28 +38,6 @@ void MainWindow::initialiseGameGrid() { } } -void MainWindow::initialiseGameMatrix() { - gameMatrix = std::vector>(); - - srand (time(NULL)); - std::vector t_row; - for (int i = 0; i < n_rows; i++) { - t_row.clear(); - for (int j = 0; j < n_columns; j++) { - // wähle zufällige Farbe aus den verfügbaren Farben - t_row.push_back( colors[rand() % colors.size()] ); - } - gameMatrix.push_back(std::move(t_row)); - } - - for (int i = 0; i < n_rows; i++) { - qDebug() << "row: " << i; - for (int j = 0; j < n_columns; j++) { - qDebug() << gameMatrix[i][j]; - } - } -} - void MainWindow::initialiseWindow() { MainWindow::setWindowTitle("ThinkPink | SameGame in PINK by Isifluff"); MainWindow::setWindowIcon(QIcon(":/icons/ThinkPink.png")); @@ -82,11 +53,10 @@ MainWindow::MainWindow(QWidget *parent) , ui(new Ui::MainWindow) , n_rows(settings->value("size/rows", 9).toInt()) , n_columns(settings->value("size/columns", 9).toInt()) - , colors(settings->value("colors/colors", QStringList() << "0xBD005E" << "0xFFC0CB" << "0xDB7093" << "0xF7A8B8" << "0x660033").toStringList()) + , game(n_rows, n_columns) { ui->setupUi(this); - initialiseGameMatrix(); initialiseGameGrid(); initialiseWindow(); initialiseMenuBar(); diff --git a/assignments/06/ThinkPink/mainwindow.h b/assignments/06/ThinkPink/mainwindow.h index 1269dcf..885a3ce 100644 --- a/assignments/06/ThinkPink/mainwindow.h +++ b/assignments/06/ThinkPink/mainwindow.h @@ -10,6 +10,8 @@ #include #include +#include "samegame.h" + QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE @@ -34,9 +36,6 @@ private: Ui::MainWindow *ui; const int n_rows; const int n_columns; - const QStringList colors; - - // mein Instinkt sagt mir, dass das hier hässlich ist: - std::vector> gameMatrix; + SameGame game; }; #endif // MAINWINDOW_H diff --git a/assignments/06/ThinkPink/samegame.cpp b/assignments/06/ThinkPink/samegame.cpp new file mode 100644 index 0000000..678a5be --- /dev/null +++ b/assignments/06/ThinkPink/samegame.cpp @@ -0,0 +1,30 @@ +#include "samegame.h" + +// ist es nicht cool? +extern std::unique_ptr settings; + +void SameGame::initialiseGameMatrix(int rows, int columns) { + gameMatrix = std::vector>(); + + srand (time(NULL)); + std::vector t_row; + for (int i = 0; i < rows; i++) { + t_row.clear(); + for (int j = 0; j < columns; j++) { + // wähle zufällige Farbe aus den verfügbaren Farben + t_row.push_back( colors[rand() % colors.size()] ); + } + gameMatrix.push_back(std::move(t_row)); + } +} + +SameGame::SameGame(int rows, int columns) + : colors(settings->value("colors/colors", QStringList() << "0xBD005E" << "0xFFC0CB" << "0xDB7093" << "0xF7A8B8" << "0x660033").toStringList()) + // also ich finds echt cool! +{ + initialiseGameMatrix(rows, columns); +} + +QString SameGame::getCell(int row, int column) { + return gameMatrix[row][column]; +} diff --git a/assignments/06/ThinkPink/samegame.h b/assignments/06/ThinkPink/samegame.h new file mode 100644 index 0000000..39232b3 --- /dev/null +++ b/assignments/06/ThinkPink/samegame.h @@ -0,0 +1,19 @@ +#ifndef SAMEGAME_H +#define SAMEGAME_H + +#include +#include +#include + +class SameGame +{ +private: + std::vector> gameMatrix; + const QStringList colors; +public: + void initialiseGameMatrix(int rows, int columns); + SameGame(int rows, int columns); + QString getCell(int row, int column); +}; + +#endif // SAMEGAME_H