Files
utun2/tools/lightsout/src/mainwindow.cpp
T

74 lines
2.3 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#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();
}
}
}