-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
133 lines (111 loc) · 4.44 KB
/
mainwindow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
//
// Created by Никита on 12.05.2023.
//
// You may need to build the project (run Qt uic code generator) to get "ui_MainWindow.h" resolved
#include "mainwindow.h"
#include "ui_MainWindow.h"
#include <QMouseEvent>
#include <QPainter>
#include <QDialog>
#include <QVBoxLayout>
#include <QPushButton>
#include "Core.h"
Token::Token(Coords position, std::string type) : position_(std::move(position)), type_(std::move(type)) {}
Coords Token::getPosition() {
return this->position_;
}
std::string Token::getType() {
return this->type_;
}
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent), ui(new Ui::MainWindow) {
ui->setupUi(this);
}
MainWindow::~MainWindow() {
delete ui;
}
void MainWindow::showInfo() {
State currentState = this->core_->getCurrentState();
Color currentColor = this->core_->getCurrentColor();
this->pieces = this->core_->getPiecesTokens();
switch (currentState) {
case State::Common:
ui->status->setText("");
break;
case State::Mate: {
QByteArray s = currentColor == Color::Write ? "Мат. Победа чёрных." : "Мат. Победа белых.";
ui->status->setText(s);
break;
}
case State::Check:
ui->status->setText("Шах");
break;
case State::Stalemate:
ui->status->setText("Пат");
break;
}
switch (currentColor) {
case Color::Write:
ui->moveColor->setText("Ход белых");
break;
case Color::Black:
ui->moveColor->setText("Ход чёрных");
break;
}
}
void MainWindow::mousePressEvent(QMouseEvent *event) {
int x = event->x();
int y = event->y();
if (x >= this->leftTopBoardPoint.x() + this->indent && x <= this->rightDownBoardPoint.x() - this->indent &&
y >= this->leftTopBoardPoint.y() + this->indent && y <= this->rightDownBoardPoint.y() - this->indent) {
Coords clickedCell = this->getClickedCell(event->x(), event->y());
if (this->availableMoves.count(clickedCell)) {
this->core_->makeMove(this->selectedCell, clickedCell);
this->showInfo();
this->selectedCell = {-1, -1};
this->availableMoves.clear();
} else {
this->availableMoves = this->core_->getAvailableMoves(clickedCell);
if (!this->availableMoves.empty())
this->selectedCell = clickedCell;
}
update();
}
}
Coords MainWindow::getClickedCell(int x, int y) const {
return {7 - (y - (this->leftTopBoardPoint.y() + this->indent)) / this->cellSize,
(x - (this->leftTopBoardPoint.x() + this->indent)) / this->cellSize};
}
QPoint MainWindow::getCoordsOfCell(Coords c) const {
return {this->leftTopBoardPoint.x() + this->cellSize * c.second + this->indent,
this->leftTopBoardPoint.y() + this->cellSize * (7 - c.first) + this->indent};
}
void MainWindow::paintEvent(QPaintEvent *event) {
QPainter painter(this);
painter.drawPixmap(this->leftTopBoardPoint, QPixmap("../images/board.png"));
for (Coords x: this->availableMoves) {
painter.drawPixmap(getCoordsOfCell(x), QPixmap("../images/availableMove.png"));
}
for (Token x: this->pieces) {
painter.drawPixmap(getCoordsOfCell(x.getPosition()), this->sprites[x.getType()]);
}
}
void MainWindow::choosePawnPromoting(std::string &s) {
QDialog dialog;
auto *layout = new QVBoxLayout(&dialog);
auto *label = new QLabel("Выберете фигуру для превращения пешки:", &dialog);
auto *bishopButton = new QPushButton("Слон", &dialog);
auto *knightButton = new QPushButton("Конь", &dialog);
auto *rookButton = new QPushButton("Ладья", &dialog);
auto *queenButton = new QPushButton("Ферзь", &dialog);
layout->addWidget(label);
layout->addWidget(bishopButton);
layout->addWidget(knightButton);
layout->addWidget(rookButton);
layout->addWidget(queenButton);
connect(bishopButton, &QPushButton::clicked, this, [&s, &dialog]() { s = "B"; dialog.close(); });
connect(knightButton, &QPushButton::clicked, this, [&s, &dialog]() { s = "Kn"; dialog.close(); });
connect(rookButton, &QPushButton::clicked, this, [&s, &dialog]() { s = "R"; dialog.close(); });
connect(queenButton, &QPushButton::clicked, this, [&s, &dialog]() { s = "Q"; dialog.close(); });
dialog.exec();
}