Initialised game grid with colors

main
Isabell Pflug 2 years ago
parent 82ea68a3cb
commit 6375254892

@ -18,11 +18,22 @@ void MainWindow::initialiseGameGrid() {
qDebug() << colors.value(i); qDebug() << colors.value(i);
} }
double max_width = MainWindow::width();
double max_height = MainWindow::height() - ui->menuBar->height() - ui->labelScore->height();
qDebug() << "gameGridLayout width: " << max_width << " height: " << max_height;
QSize button_size(max_width/n_columns, max_height/n_rows);
qDebug() << "Width: " << button_size.width() << " Height: " << button_size.height();
for(int i = 0; i < n_columns; i++) for(int i = 0; i < n_columns; i++)
{ {
for(int j = 0; j < n_rows; j++) for(int j = 0; j < n_rows; j++)
{ {
QPushButton * button = new QPushButton(this); QPushButton * button = new QPushButton(this);
button->setFixedSize(button_size);
button->setStyleSheet("background-color: " + gameMatrix[j][i] + "; border: none;");
ui->gameGridLayout->addWidget(button, j, i); ui->gameGridLayout->addWidget(button, j, i);
// Set size text etc. for each button // Set size text etc. for each button
@ -34,6 +45,28 @@ 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() { void MainWindow::initialiseWindow() {
MainWindow::setWindowTitle("ThinkPink | SameGame in PINK by Isifluff"); MainWindow::setWindowTitle("ThinkPink | SameGame in PINK by Isifluff");
MainWindow::setWindowIcon(QIcon(":/icons/ThinkPink.png")); MainWindow::setWindowIcon(QIcon(":/icons/ThinkPink.png"));
@ -42,7 +75,6 @@ void MainWindow::initialiseWindow() {
void MainWindow::initialiseMenuBar() { void MainWindow::initialiseMenuBar() {
ui->menuPlay->addAction(QIcon(":/icons/game.png"), "New Game"); ui->menuPlay->addAction(QIcon(":/icons/game.png"), "New Game");
ui->menuPlay->addSeparator(); ui->menuPlay->addSeparator();
//ui->menuPlay->addAction("Brag");
} }
MainWindow::MainWindow(QWidget *parent) MainWindow::MainWindow(QWidget *parent)
@ -54,6 +86,7 @@ MainWindow::MainWindow(QWidget *parent)
{ {
ui->setupUi(this); ui->setupUi(this);
initialiseGameMatrix();
initialiseGameGrid(); initialiseGameGrid();
initialiseWindow(); initialiseWindow();
initialiseMenuBar(); initialiseMenuBar();

@ -1,10 +1,11 @@
#ifndef MAINWINDOW_H #ifndef MAINWINDOW_H
#define MAINWINDOW_H #define MAINWINDOW_H
#include <stdlib.h>
#include <iostream> #include <time.h>
#include <vector> #include <vector>
#include <QDebug> #include <QDebug>
#include <QGenericMatrix>
#include <QMainWindow> #include <QMainWindow>
#include <QPushButton> #include <QPushButton>
#include <QSettings> #include <QSettings>
@ -22,6 +23,7 @@ public:
~MainWindow(); ~MainWindow();
// initialisation functions // initialisation functions
void initialiseGameGrid(); void initialiseGameGrid();
void initialiseGameMatrix();
void initialiseWindow(); void initialiseWindow();
void initialiseMenuBar(); void initialiseMenuBar();
@ -33,5 +35,8 @@ private:
const int n_rows; const int n_rows;
const int n_columns; const int n_columns;
const QStringList colors; const QStringList colors;
// mein Instinkt sagt mir, dass das hier hässlich ist:
std::vector<std::vector<QString>> gameMatrix;
}; };
#endif // MAINWINDOW_H #endif // MAINWINDOW_H

@ -14,106 +14,100 @@
<string>MainWindow</string> <string>MainWindow</string>
</property> </property>
<widget class="QWidget" name="centralwidget"> <widget class="QWidget" name="centralwidget">
<widget class="QWidget" name="verticalLayoutWidget"> <layout class="QVBoxLayout" name="verticalLayout_2">
<property name="geometry"> <item>
<rect> <layout class="QVBoxLayout" name="verticalLayout">
<x>0</x> <item>
<y>0</y> <layout class="QHBoxLayout" name="horizontalLayout">
<width>801</width> <item>
<height>441</height> <spacer name="horizontalSpacer">
</rect> <property name="orientation">
</property> <enum>Qt::Horizontal</enum>
<layout class="QVBoxLayout" name="verticalLayout"> </property>
<item> <property name="sizeHint" stdset="0">
<layout class="QHBoxLayout" name="horizontalLayout"> <size>
<item> <width>40</width>
<spacer name="horizontalSpacer"> <height>20</height>
<property name="orientation"> </size>
<enum>Qt::Horizontal</enum> </property>
</property> </spacer>
<property name="sizeHint" stdset="0"> </item>
<size> <item>
<width>40</width> <widget class="QLabel" name="labelScore">
<height>20</height> <property name="sizePolicy">
</size> <sizepolicy hsizetype="Minimum" vsizetype="Minimum">
</property> <horstretch>0</horstretch>
</spacer> <verstretch>0</verstretch>
</item> </sizepolicy>
<item> </property>
<widget class="QLabel" name="labelScore"> <property name="text">
<property name="sizePolicy"> <string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;&lt;span style=&quot; font-size:20pt;&quot;&gt;Score&lt;/span&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
<sizepolicy hsizetype="Minimum" vsizetype="Minimum"> </property>
<horstretch>0</horstretch> <property name="alignment">
<verstretch>0</verstretch> <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</sizepolicy> </property>
</property> </widget>
<property name="text"> </item>
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;&lt;span style=&quot; font-size:20pt;&quot;&gt;Score&lt;/span&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string> <item>
</property> <widget class="QLCDNumber" name="lcdScore">
<property name="alignment"> <property name="sizePolicy">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set> <sizepolicy hsizetype="MinimumExpanding" vsizetype="Minimum">
</property> <horstretch>0</horstretch>
</widget> <verstretch>0</verstretch>
</item> </sizepolicy>
<item> </property>
<widget class="QLCDNumber" name="lcdScore"> <property name="smallDecimalPoint">
<property name="sizePolicy"> <bool>false</bool>
<sizepolicy hsizetype="MinimumExpanding" vsizetype="Minimum"> </property>
<horstretch>0</horstretch> <property name="digitCount">
<verstretch>0</verstretch> <number>5</number>
</sizepolicy> </property>
</property> <property name="mode">
<property name="smallDecimalPoint"> <enum>QLCDNumber::Dec</enum>
<bool>false</bool> </property>
</property> <property name="segmentStyle">
<property name="digitCount"> <enum>QLCDNumber::Flat</enum>
<number>5</number> </property>
</property> </widget>
<property name="mode"> </item>
<enum>QLCDNumber::Dec</enum> <item>
</property> <spacer name="horizontalSpacer_2">
<property name="segmentStyle"> <property name="orientation">
<enum>QLCDNumber::Flat</enum> <enum>Qt::Horizontal</enum>
</property> </property>
</widget> <property name="sizeHint" stdset="0">
</item> <size>
<item> <width>40</width>
<spacer name="horizontalSpacer_2"> <height>20</height>
<property name="orientation"> </size>
<enum>Qt::Horizontal</enum> </property>
</property> </spacer>
<property name="sizeHint" stdset="0"> </item>
<size> </layout>
<width>40</width> </item>
<height>20</height> <item>
</size> <layout class="QGridLayout" name="gameGridLayout"/>
</property> </item>
</spacer> <item>
</item> <layout class="QHBoxLayout" name="horizontalLayout_3">
</layout> <item>
</item> <spacer name="verticalSpacer">
<item> <property name="orientation">
<layout class="QGridLayout" name="gameGridLayout"/> <enum>Qt::Vertical</enum>
</item> </property>
<item> <property name="sizeHint" stdset="0">
<layout class="QHBoxLayout" name="horizontalLayout_3"> <size>
<item> <width>20</width>
<spacer name="verticalSpacer"> <height>40</height>
<property name="orientation"> </size>
<enum>Qt::Vertical</enum> </property>
</property> </spacer>
<property name="sizeHint" stdset="0"> </item>
<size> </layout>
<width>20</width> </item>
<height>40</height> </layout>
</size> </item>
</property> </layout>
</spacer>
</item>
</layout>
</item>
</layout>
</widget>
</widget> </widget>
<widget class="QMenuBar" name="menuBar"> <widget class="QMenuBar" name="menuBar">
<property name="geometry"> <property name="geometry">

Loading…
Cancel
Save