Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions Task3_Tetris.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
QT += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++17

# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
figure.cpp \
griddrawer.cpp \
main.cpp \
mainwindow.cpp \
nextfigure.cpp \
tetris.cpp

HEADERS += \
figure.h \
griddrawer.h \
mainwindow.h \
nextfigure.h \
tetris.h

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
266 changes: 266 additions & 0 deletions Task3_Tetris.pro.user

Large diffs are not rendered by default.

120 changes: 120 additions & 0 deletions figure.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
#include "figure.h"

figure::figure()

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Классы с большой буквы.

{
m_x = 0;
m_y = 0;
m_condition = 0;
m_color = QColor(Qt::red);
m_data = {{{1}}};
}

int figure::x() const
{
return m_x;
}

void figure::setX(int newX)
{
m_x = newX;
}

int figure::y() const
{
return m_y;
}

void figure::setY(int newY)
{
m_y = newY;
}

int figure::condition() const
{
return m_condition;
}

void figure::setCondition(int newCondition)
{
m_condition = newCondition;
}

QColor figure::color() const
{
return m_color;
}

void figure::setColor()
{
QRandomGenerator *rg = QRandomGenerator::global();
int randomNumber = rg->bounded(0, 6);

QVector<QColor> colorRand = {Qt::red, Qt::blue, Qt::green, Qt::yellow, Qt::cyan, Qt::magenta};
m_color = colorRand[randomNumber];
}

void figure::setSpecificColor(QColor color_)
{
m_color = color_;
}

QVector<QVector<QVector<bool>>> figure::data() const
{
return m_data;
}

void figure::setData()
{
QRandomGenerator *rg = QRandomGenerator::global();
int randomNumber = rg->bounded(1, 7);
switch (randomNumber) {
case 1:
m_data = {{{1,1,1,1}}, {{1},{1},{1},{1}}};
break;
case 2:
m_data = {{{1,0},{1,1},{1,0}},
{{1,1,1},{0,1,0}},
{{0,1},{1,1},{0,1}},
{{0,1,0},{1,1,1}}
};
break;
case 3:
m_data = {{{1,1},{1,0},{1,0}},
{{1,1,1},{0,0,1}},
{{0,1},{0,1},{1,1}},
{{1,0,0},{1,1,1}}
};
break;
case 4:
m_data = {{{1,1},{0,1},{0,1}},
{{0,0,1},{1,1,1}},
{{1,0},{1,0},{1,1}},
{{1,1,1},{1,0,0}}
};
break;
case 5:
m_data = {{{1,0},{1,1},{0,1}},
{{0,1,1},{1,1,0}}
};
break;
case 6:
m_data = {{{0,1},{1,1},{1,0}},
{{1,1,0},{0,1,1}}
};
break;
default:
break;
Comment on lines +71 to +106

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

case должны быть на одну табуляцию правее от switch

}
}

void figure::setSpecificData(QVector<QVector<QVector<bool>>> data_){

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Постфикс "_" для параметров методов не нужен. Пробел перед { (касается всего проекта, унифицируйте написание скобок и поправьте отступы)

m_data = data_;
}

void figure::nextCondition(){
setCondition((condition() + 1)%data().size());

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Отделить пробелами %

}

void figure::prevCondition(){
setCondition((condition() + data().size()- 1)%data().size());
}
41 changes: 41 additions & 0 deletions figure.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#ifndef FIGURE_H
#define FIGURE_H

#include <QObject>
#include <QColor>
#include <QVector>
#include <QRandomGenerator>

class figure : public QObject
{
Q_OBJECT
Q_PROPERTY(int x READ x WRITE setX)
Q_PROPERTY(int y READ y WRITE setY)
Q_PROPERTY(int condition READ condition WRITE setCondition)

public:
figure();
int x() const;
void setX(int newX);
int y() const;
Comment on lines +18 to +20

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Методы все же с глаголов начинаются. Можно было бы оставить x и у, если бы мы говорили про структуры, но для классов лучше getX getY

void setY(int newY);
int condition() const;

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getСondition

void setCondition(int newCondition);
QColor color() const;
void setColor();
QVector<QVector<QVector<bool>>> data() const;
void setData();
void setSpecificColor(QColor color_);
void setSpecificData(QVector<QVector<QVector<bool>>> data_);

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Постфикс _ не нужен, как и в строке выше.

void nextCondition();
void prevCondition();
Comment on lines +30 to +31

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Методы почти всегда что-то делают -> именуются глаголами или начинаются с глаголов.


private:
int m_x;
int m_y;
int m_condition;
QColor m_color;
QVector<QVector<QVector<bool>>> m_data;
Comment on lines +34 to +38

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

В нашей нотации (по google) приватные поля именуются все же с постфиксом "_". m_x -> x_ и т.д.

};

#endif // FIGURE_H
82 changes: 82 additions & 0 deletions griddrawer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#include "griddrawer.h"

GridDrawer::GridDrawer(QWidget *parent):QWidget(parent), m_columns(3), m_rows(4), m_sizeCell(30)
{

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Раз начали внутри кода не переносить скобки, то они не переносятся и в методах. Везде в одном стиле.

for (int i = 0; i < m_columns; i++) {
QVector<QColor> cellsRow = {};
for (int j = 0; j < m_rows; j++) {
cellsRow.push_back(Qt::gray);
}
cellColors.push_back(cellsRow);
}
}

int GridDrawer::columns() const
{
return m_columns;
}

void GridDrawer::setColumns(int newColumns)
{
m_columns = newColumns;
cellColors.clear();
for (int i = 0; i < m_columns; i++) {
QVector<QColor> cellsRow = {};
for (int j = 0; j < rows(); j++) {
cellsRow.push_back(Qt::gray);
}
cellColors.push_back(cellsRow);
}
}

int GridDrawer::rows() const
{
return m_rows;
}

void GridDrawer::setRows(int newRows)
{
m_rows = newRows;
cellColors.clear();
for (int i = 0; i < columns(); i++) {
QVector<QColor> cellsRow = {};
for (int j = 0; j < m_rows; j++) {
cellsRow.push_back(Qt::gray);
}
cellColors.push_back(cellsRow);
}
}

int GridDrawer::sizeCell() const
{
return m_sizeCell;
}

void GridDrawer::setSizeCell(int newSizeCell)
{
m_sizeCell = newSizeCell;
}

QVector<QVector<QColor>> GridDrawer::getCellColors() const
{
return cellColors;
}

void GridDrawer::setCellColors(const QVector<QVector<QColor> > &newGetCellColors)
{
cellColors = newGetCellColors;
}

void GridDrawer::draw(QPainter *painter) {
int cellWidth = m_sizeCell;
int cellHeight = m_sizeCell;

for (int i = 0; i < m_columns; i++) {
for (int j = 0; j < m_rows; j++) {
painter->setPen(Qt::white);
painter->setBrush(cellColors[i][j]);
painter->drawRect(i * cellWidth, j * cellHeight, cellWidth, cellHeight);
}
}
this->update();
}
38 changes: 38 additions & 0 deletions griddrawer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#ifndef GRIDDRAWER_H
#define GRIDDRAWER_H

#include <QWidget>
#include <QVector>
#include <QPainter>

class GridDrawer : public QWidget {
Q_OBJECT
Q_PROPERTY(int columns READ columns WRITE setColumns)
Q_PROPERTY(int rows READ rows WRITE setRows)
Q_PROPERTY(int sizeCell READ sizeCell WRITE setSizeCell)

public:
GridDrawer(QWidget *parent = 0);

int columns() const;
void setColumns(int newColumns);

int rows() const;
void setRows(int newRows);

int sizeCell() const;
void setSizeCell(int newSizeCell);

QVector<QVector<QColor> > getCellColors() const;
void setCellColors(const QVector<QVector<QColor> > &newGetCellColors);

void draw(QPainter *painter);

private:
int m_columns;
int m_rows;
int m_sizeCell;
Comment on lines +32 to +34

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Опять же, в нашей нотации приватные поля именуются не с префиксом "m_", а с постфиксом "_"

QVector<QVector<QColor>> cellColors;
};

#endif // GRIDDRAWER_H
13 changes: 7 additions & 6 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#include "mainwindow.h"
#include <QApplication>

#include "tetris.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();

int main(int argc, char *argv[]) {
QApplication a(argc, argv);
Tetris w;
w.show();
return a.exec();
return a.exec();
}
Loading