♻️ Moved core game mechanic to its own class

main
Isabell Pflug 2 years ago
parent 6375254892
commit 232174b309

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

@ -5,19 +5,12 @@ extern std::unique_ptr<QSettings> 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<std::vector<QString>>();
srand (time(NULL));
std::vector<QString> 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();

@ -10,6 +10,8 @@
#include <QPushButton>
#include <QSettings>
#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<std::vector<QString>> gameMatrix;
SameGame game;
};
#endif // MAINWINDOW_H

@ -0,0 +1,30 @@
#include "samegame.h"
// ist es nicht cool?
extern std::unique_ptr<QSettings> settings;
void SameGame::initialiseGameMatrix(int rows, int columns) {
gameMatrix = std::vector<std::vector<QString>>();
srand (time(NULL));
std::vector<QString> 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];
}

@ -0,0 +1,19 @@
#ifndef SAMEGAME_H
#define SAMEGAME_H
#include <vector>
#include <QSettings>
#include <QString>
class SameGame
{
private:
std::vector<std::vector<QString>> 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
Loading…
Cancel
Save