You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
184 lines
5.6 KiB
C++
184 lines
5.6 KiB
C++
#include "mainwindow.h"
|
|
#include "ui_mainwindow.h"
|
|
|
|
extern std::unique_ptr<QSettings> settings;
|
|
|
|
void MainWindow::clearConnected(std::set<std::vector<int>> *connected) {
|
|
QString text = "\u2605";
|
|
int n = connected->size();
|
|
int new_score = ui->lcdScore->intValue() + (n * (n-1));
|
|
ui->lcdScore->display(new_score);
|
|
for (auto i = connected->begin(); i != connected->end(); i++) {
|
|
buttonGrid[(*i)[0]][(*i)[1]]->setText(text);
|
|
}
|
|
qApp->processEvents();
|
|
usleep(550000);
|
|
for (auto i = connected->begin(); i != connected->end(); i++) {
|
|
game->deleteCell((*i)[0], (*i)[1]);
|
|
}
|
|
usleep(5000); // paranoia
|
|
refreshButtonGrid();
|
|
qApp->processEvents();
|
|
}
|
|
|
|
void MainWindow::refreshButtonGrid() {
|
|
//QSize button_size(max_width/columns, max_height/rows);
|
|
for (int i = 0; i < rows; i++) {
|
|
for (int j = 0; j < columns; j++) {
|
|
buttonGrid[i][j]->setStyleSheet("color: #FFFFFF; Font : 30pt; background-color: " + game->getCell(i,j) + "; border: none;");
|
|
buttonGrid[i][j]->setText("");
|
|
//buttonGrid[i][j]->setFixedSize(button_size);
|
|
}
|
|
}
|
|
qApp->processEvents();
|
|
}
|
|
|
|
void MainWindow::checkColumn(int column) {
|
|
bool isEmpty;
|
|
isEmpty = true;
|
|
for (int row = 0; row < rows; row++) {
|
|
if (game->getCell(row, column) != "none")
|
|
isEmpty = false;
|
|
}
|
|
if (isEmpty) {
|
|
bubbleRight(column);
|
|
}
|
|
}
|
|
|
|
void MainWindow::bubbleRight(int column) {
|
|
if (column < columns-1) {
|
|
for (int row = 0; row < rows; row++) {
|
|
QString tcolor = game->getCell(row, column);
|
|
game->setCell(row, column, game->getCell(row, column+1));
|
|
game->setCell(row, column+1, tcolor);
|
|
}
|
|
bubbleRight(column+1);
|
|
}
|
|
}
|
|
|
|
void MainWindow::bubbleUp(int row, int column) {
|
|
if (row > 0) {
|
|
QString tcolor = game->getCell(row, column);
|
|
game->setCell(row, column, game->getCell(row-1, column));
|
|
game->setCell(row-1, column, tcolor);
|
|
bubbleUp(row-1, column);
|
|
}
|
|
}
|
|
|
|
void MainWindow::bubbleBoxes(std::set<std::vector<int>> *connected) {
|
|
usleep(400000);
|
|
for (auto i = connected->begin(); i != connected->end(); i++) {
|
|
bubbleUp((*i)[0], (*i)[1]);
|
|
}
|
|
refreshButtonGrid();
|
|
qApp->processEvents();
|
|
usleep(400000);
|
|
for (int j = 0; j < columns; j++) {
|
|
for (int x = 0; x < columns; x++)
|
|
checkColumn(j);
|
|
}
|
|
refreshButtonGrid();
|
|
}
|
|
|
|
// slots
|
|
void MainWindow::buttonPressed(int row, int column) {
|
|
if(game->getCell(row, column) != "none") {
|
|
std::set<std::vector<int>> connected;
|
|
game->getConnected(&connected, game->getCell(row, column), row, column);
|
|
if ( connected.size() > 1) {
|
|
clearConnected(&connected);
|
|
bubbleBoxes(&connected);
|
|
} else
|
|
ui->statusBar->showMessage("Only friends get Stars!", 2000);
|
|
}
|
|
}
|
|
|
|
void MainWindow::newGame() {
|
|
QMessageBox newGameDialog;
|
|
newGameDialog.setText("Do you really want to start a new game?");
|
|
newGameDialog.setStandardButtons(QMessageBox::Yes | QMessageBox::Cancel);
|
|
if (newGameDialog.exec() == QMessageBox::Yes) {
|
|
game->initialiseGameMatrix(rows, columns);
|
|
refreshButtonGrid();
|
|
ui->lcdScore->display(0);
|
|
}
|
|
}
|
|
|
|
// initialisation functions
|
|
void MainWindow::initialiseGame() {
|
|
game->initialiseGameMatrix(rows, columns);
|
|
initialiseGameGrid();
|
|
ui->lcdScore->display(0);
|
|
}
|
|
|
|
void MainWindow::initialiseGameGrid() {
|
|
QSize button_size(max_width/columns, max_height/rows);
|
|
buttonGrid.clear();
|
|
|
|
for(int i = 0; i < rows; i++)
|
|
{
|
|
std::vector<QPushButton *> button_row;
|
|
for(int j = 0; j < columns; j++)
|
|
{
|
|
QPushButton * button = new QPushButton(this);
|
|
button->setFixedSize(button_size);
|
|
button->setStyleSheet("color: #FFFFFF; Font : 30pt; background-color: " + game->getCell(i,j) + "; border: none;");
|
|
ui->gameGridLayout->addWidget(button, i, j);
|
|
button_row.push_back(button);
|
|
|
|
// Set size text etc. for each button
|
|
|
|
connect(button, &QPushButton::clicked, [=](){
|
|
buttonPressed(i, j);
|
|
});
|
|
}
|
|
buttonGrid.push_back(std::move(button_row));
|
|
}
|
|
}
|
|
|
|
void MainWindow::initialiseWindow() {
|
|
MainWindow::setWindowTitle("ThinkPink | SameGame in PINK by Isifluff");
|
|
MainWindow::setWindowIcon(QIcon(":/icons/ThinkPink.png"));
|
|
}
|
|
|
|
void MainWindow::initialiseMenuBar() {
|
|
QAction *new_game_action = ui->menuPlay->addAction("New Game");
|
|
connect(new_game_action, SIGNAL(triggered()), this, SLOT(newGame()));
|
|
ui->menuPlay->addSeparator();
|
|
}
|
|
|
|
void MainWindow::refreshSizes() {
|
|
max_width = MainWindow::width();
|
|
max_height = MainWindow::height() - ui->menuBar->height() - ui->lcdScore->height() - ui->statusBar->height();
|
|
}
|
|
|
|
MainWindow::MainWindow(QWidget *parent)
|
|
: QMainWindow(parent)
|
|
, ui(new Ui::MainWindow)
|
|
, rows(settings->value("size/rows", 9).toInt())
|
|
, columns(settings->value("size/columns", 9).toInt())
|
|
, max_width(800)
|
|
, max_height(519)
|
|
{
|
|
ui->setupUi(this);
|
|
QPalette pal = QPalette();
|
|
pal.setColor(QPalette::Window, 0xfff0f3);
|
|
pal.setColor(QPalette::Text, 0xa4133c);
|
|
this->setPalette(pal);
|
|
ui->menuPlay->setPalette(pal);
|
|
ui->menuBar->setStyleSheet("QMenuBar::item {color: #a4133c}");
|
|
ui->statusBar->setStyleSheet("color: #a4133c");
|
|
refreshSizes();
|
|
game = new SameGame(rows, columns);
|
|
initialiseGame();
|
|
initialiseWindow();
|
|
initialiseMenuBar();
|
|
}
|
|
|
|
MainWindow::~MainWindow()
|
|
{
|
|
delete game;
|
|
delete ui;
|
|
}
|
|
|