You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

73 lines
2.3 KiB

#include "mainwindow.h"
#include "boardwidget.h"
#include <QInputDialog>
#include <QLabel>
#include <QMenuBar>
#include <QMessageBox>
#include <QPushButton>
#include <QToolBar>
#include <QVBoxLayout>
#include <QWidget>
MainWindow::MainWindow(int size, QWidget *parent)
: QMainWindow(parent), m_size(size) {
setWindowTitle(QStringLiteral("Выключить свет"));
m_board = new BoardWidget(this);
m_board->newGame(m_size);
m_movesLabel = new QLabel(this);
m_movesLabel->setText(tr("Ходы: 0"));
auto *hintButton = new QPushButton(tr("Подсказка"), this);
hintButton->setCheckable(true);
connect(hintButton, &QPushButton::toggled, this, &MainWindow::toggleHint);
auto *newButton = new QPushButton(tr("Новая игра"), this);
connect(newButton, &QPushButton::clicked, this, &MainWindow::newGame);
auto *controls = new QWidget(this);
auto *layout = new QVBoxLayout(controls);
layout->addWidget(m_board, 1);
layout->addWidget(m_movesLabel);
layout->addWidget(hintButton);
layout->addWidget(newButton);
setCentralWidget(controls);
connect(m_board, &BoardWidget::stateChanged, this, &MainWindow::onBoardStateChanged);
}
// Начинает новую партию, запрашивая размер поля у пользователя.
void MainWindow::newGame() {
bool ok = false;
const int size = QInputDialog::getInt(
this, tr("Новая игра"),
tr("Размер поля (3–8):"), m_size, 3, 8, 1, &ok);
if (!ok) {
return;
}
m_size = size;
m_board->newGame(m_size);
m_movesLabel->setText(tr("Ходы: 0"));
}
// Переключает показ подсказок.
void MainWindow::toggleHint() {
m_board->setHintEnabled(!m_board->isHintEnabled());
}
// Обновляет счётчик ходов и сообщает о победе.
void MainWindow::onBoardStateChanged(bool solved) {
m_movesLabel->setText(tr("Ходы: %1").arg(m_board->moves()));
if (solved) {
const auto ret = QMessageBox::information(
this, tr("Победа!"),
tr("Свет выключен за %1 ходов.").arg(m_board->moves()),
QMessageBox::Ok | QMessageBox::Reset, QMessageBox::Ok);
if (ret == QMessageBox::Reset) {
newGame();
}
}
}