I'm trying to make a chess game in C++ and I already have the chessboard and the chess Pieces. When I try to compile the code it does work and there a window that pop in my screen that shows the chessboard and the pieces. However, when I try to click a piece the whole code just crash without giving me any error or warning. Also, my game is only supported on Qt creator. I really need help to stop my code crashing thank you. P.S: Sorry if there any grammar mistake it's because English is not my first language.
//this is the code for the mainwindow
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QPushButton>
#include <QGridLayout>
#include <QMouseEvent>
#include <QPoint>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class Pieces {
public:
Pieces(int x, int y,char color, char pieces) : x(x), y(y), color(color), pieces(pieces) {};
char getColor() const{
return color;
};
int getX(){
return x;
};
int getY(){
return y;
};
int setX(int x) {
return x;
};
int setY(int y) {
return y;
};
char getPieces() const{
return pieces;
};
private:
int x;
int y;
char color;
char pieces;
};
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
Pieces* pieces;
void chessboard(QPushButton* buttons[8][8], QWidget* parentWidget);
void startingPoint(char team, QPushButton* buttons[8][8]);
bool isKingMoveValid(int newRow, int newCol);
bool isMoveBlocked(int newRow, int newCol);
void onButtonClicked(int row, int col);
void setupBoard();
void moveKing(int dx, int dy);
private:
Ui::MainWindow *ui;
QPushButton* buttons[8][8];
int selectedRow = -1;
int selectedCol = -1;
int col_init = -1;
int row_init = -1;
private slots:
void mousePressEvent(QMouseEvent* event);
};
#endif // MAINWINDOW_H
// this is the code for the main
#include <QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow window;
window.show();
return app.exec();
}
// this is the code for the mainwindow.cpp
#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include <iostream>
#include <QMouseEvent>
#include <QPushButton>
using namespace std;
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
chessboard(buttons, ui->centralwidget);
startingPoint('B', buttons);
startingPoint('W', buttons);
for (int row = 0; row < 8; ++row) {
for (int col = 0; col < 8; ++col) {
connect(buttons[row][col], &QPushButton::clicked, this, [this, row, col]() {
onButtonClicked(row, col);
});
}
}
}
void MainWindow::chessboard(QPushButton* buttons[8][8], QWidget* parentWidget) {
QGridLayout* gridLayout = new QGridLayout(parentWidget);
for (int row = 0; row < 8; ++row) {
for (int col = 0; col < 8; ++col) {
buttons[row][col] = new QPushButton(parentWidget);
buttons[row][col]->setFixedSize(110, 110);
if ((row + col) % 2 == 0) {
buttons[row][col]->setStyleSheet("background-color: white");
} else {
buttons[row][col]->setStyleSheet("background-color: pink");
}
gridLayout->addWidget(buttons[row][col], row, col);
}
}
}
void MainWindow::startingPoint(char team, QPushButton* buttons[8][8]) {
int rowRook, rowPawn;
if (team == 'W') {
rowRook = 0;
rowPawn = 1;
}
else {
rowRook = 7;
rowPawn = 6;
}
Pieces* rook = new Pieces(rowRook, 0, team, 'T');
QPushButton* rookButton = buttons[rook->getX()][rook->getY()];
rookButton->setText(QString(QChar(0x2656)));
rookButton->setFont(QFont("Arial", 50));
Pieces* knight = new Pieces(rowRook, 1, team, 'C');
QPushButton* knightButton = buttons[knight->getX()][knight->getY()];
knightButton->setText(QString(QChar(0x2658)));
knightButton->setFont(QFont("Arial", 50));
Pieces* bishop = new Pieces(rowRook, 2, team, 'F');
QPushButton* bishopButton = buttons[bishop->getX()][bishop->getY()];
bishopButton->setText(QString(QChar(0x2657)));
bishopButton->setFont(QFont("Arial", 50));
Pieces* queen = new Pieces(rowRook, 3, team, 'D');
QPushButton* queenButton = buttons[queen->getX()][queen->getY()];
queenButton->setText(QString(QChar(0x2655)));
queenButton->setFont(QFont("Arial", 50));
Pieces* king = new Pieces(rowRook, 4, team, 'R');
QPushButton* kingButton = buttons[king->getX()][king->getY()];
kingButton->setText(QString(QChar(0x2654)));
kingButton->setFont(QFont("Arial", 50));
Pieces* bishop2 = new Pieces(rowRook, 5, team, 'F');
QPushButton* bishop2Button = buttons[bishop2->getX()][bishop2->getY()];
bishop2Button->setText(QString(QChar(0x2657)));
bishop2Button->setFont(QFont("Arial", 50));
Pieces* knight2 = new Pieces(rowRook, 6, team, 'C');
QPushButton* knight2Button = buttons[knight2->getX()][knight2->getY()];
knight2Button->setText(QString(QChar(0x2658)));
knight2Button->setFont(QFont("Arial", 50));
Pieces* rook2 = new Pieces(rowRook, 7, team, 'T');
QPushButton* rook2Button = buttons[rook2->getX()][rook2->getY()];
rook2Button->setText(QString(QChar(0x2656)));
rook2Button->setFont(QFont("Arial", 50));
for (int i = 0; i < 8; i++) {
Pieces* pawn = new Pieces(rowPawn, i, team, 'P');
QPushButton* pawnButton = buttons[pawn->getX()][pawn->getY()];
pawnButton->setText(QString(QChar(0x265F)));
pawnButton->setFont(QFont("Arial", 50));
}
}
void MainWindow::onButtonClicked(int row, int col) {
if (pieces->getX() == row && pieces->getY() == col) {
buttons[row][col]->setStyleSheet("background-color: green");
col_init = col;
row_init = row;
} else if (col_init != -1 && row_init != -1) {
if ((abs(row_init - row) <= 1) && (abs(col_init - col) <= 1)) {
buttons[row][col]->setText(QString(pieces->getPieces()));
buttons[row][col]->setStyleSheet("color: red; font-size: 100px;");
buttons[row_init][col_init]->setText("");
pieces->setX(row);
pieces->setY(col);
}
col_init = -1;
row_init = -1;
}
}
void MainWindow::mousePressEvent(QMouseEvent* event) {
int row = -1, col = -1;
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (event->button() == Qt::LeftButton && event->pos() == buttons[i][j]->pos()) {
row = i;
col = j;
break;
}
}
if (row != -1 && col != -1) {
break;
}
}
onButtonClicked(row,col);
}
MainWindow::~MainWindow() {
delete ui;
};