Initialised game grid with colors

main
Isabell Pflug 1 year ago
parent 8a381d011b
commit c004cb2cbb

@ -18,11 +18,22 @@ void MainWindow::initialiseGameGrid() {
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 j = 0; j < n_rows; j++)
{
QPushButton * button = new QPushButton(this);
button->setFixedSize(button_size);
button->setStyleSheet("background-color: " + gameMatrix[j][i] + "; border: none;");
ui->gameGridLayout->addWidget(button, j, i);
// 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() {
MainWindow::setWindowTitle("ThinkPink | SameGame in PINK by Isifluff");
MainWindow::setWindowIcon(QIcon(":/icons/ThinkPink.png"));
@ -42,7 +75,6 @@ void MainWindow::initialiseWindow() {
void MainWindow::initialiseMenuBar() {
ui->menuPlay->addAction(QIcon(":/icons/game.png"), "New Game");
ui->menuPlay->addSeparator();
//ui->menuPlay->addAction("Brag");
}
MainWindow::MainWindow(QWidget *parent)
@ -54,6 +86,7 @@ MainWindow::MainWindow(QWidget *parent)
{
ui->setupUi(this);
initialiseGameMatrix();
initialiseGameGrid();
initialiseWindow();
initialiseMenuBar();

@ -1,10 +1,11 @@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <vector>
#include <QDebug>
#include <QGenericMatrix>
#include <QMainWindow>
#include <QPushButton>
#include <QSettings>
@ -22,6 +23,7 @@ public:
~MainWindow();
// initialisation functions
void initialiseGameGrid();
void initialiseGameMatrix();
void initialiseWindow();
void initialiseMenuBar();
@ -33,5 +35,8 @@ private:
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;
};
#endif // MAINWINDOW_H

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

Loading…
Cancel
Save