Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Piece implementation #25

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions embeddings/Wizard-Chess.pro
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++17

# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
main.cpp \
mainwindow.cpp
Expand All @@ -18,11 +14,13 @@ HEADERS += \
FORMS += \
mainwindow.ui

OTHER_FILES += \
black_bishop.png

RESOURCES += \
resources.qrc

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

RESOURCES += \
GryffindorLogo.qrc \
Resources/QTGryffindorLogo.qrc
Binary file added embeddings/black_bishop.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions embeddings/displaypiece.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "displaypiece.h"

DisplayPiece::DisplayPiece()
{

}
Binary file added embeddings/images/black_bishop.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added embeddings/images/black_king.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added embeddings/images/black_knight.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added embeddings/images/black_pawn.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added embeddings/images/black_queen.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added embeddings/images/black_rook.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added embeddings/images/chessboardBackground.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added embeddings/images/white_bishop.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added embeddings/images/white_king.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added embeddings/images/white_knight.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added embeddings/images/white_pawn.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added embeddings/images/white_queen.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added embeddings/images/white_rook.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
144 changes: 144 additions & 0 deletions embeddings/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ MainWindow::MainWindow(QWidget *parent)
{
ui->setupUi(this);
ui->stackedWidget->setCurrentIndex(0);
setupBoard();
setupInitialPositions();
}

MainWindow::~MainWindow()
Expand Down Expand Up @@ -54,6 +56,148 @@ void MainWindow::on_pushButton_EndGame_clicked()
ui->stackedWidget->setCurrentIndex(8);
}

void MainWindow::setupBoard()
{
// Map each push button to its chessboard position
for (char col = 'A'; col <= 'H'; ++col) {
for (int row = 1; row <= 8; ++row) {
QString position = QString(col) + QString::number(row);
QString buttonName = "pushButton_" + position;

// Find the button in the UI and connect its signal
QPushButton* button = findChild<QPushButton*>(buttonName);
if (button) {
boardMap[position] = button;
connect(button, &QPushButton::clicked, this, &MainWindow::onTileClicked);
}
}
}
}


// Add chess pieces to their starting positions
void MainWindow::setupInitialPositions()
{
// ui->stackedWidget->setCurrentIndex(5);
// QLabel *background = new QLabel(this);
// background->setAlignment(Qt::AlignTop | Qt:: AlignRight);
// background->setGeometry(QRect(15, 20, 300, 320));
// background->setStyleSheet("background-image:/images/images/chessBoardBackground");
// Add white pieces
pieces.append(ChessPiece("rook", "white", "A1"));
pieces.append(ChessPiece("knight", "white", "B1"));
pieces.append(ChessPiece("bishop", "white", "C1"));
pieces.append(ChessPiece("queen", "white", "D1"));
pieces.append(ChessPiece("king", "white", "E1"));
pieces.append(ChessPiece("bishop", "white", "F1"));
pieces.append(ChessPiece("knight", "white", "G1"));
pieces.append(ChessPiece("rook", "white", "H1"));
for (char col = 'A'; col <= 'H'; ++col)
pieces.append(ChessPiece("pawn", "white", QString(col) + "2"));

// Add black pieces
pieces.append(ChessPiece("rook", "black", "A8"));
pieces.append(ChessPiece("knight", "black", "B8"));
pieces.append(ChessPiece("bishop", "black", "C8"));
pieces.append(ChessPiece("queen", "black", "D8"));
pieces.append(ChessPiece("king", "black", "E8"));
pieces.append(ChessPiece("bishop", "black", "F8"));
pieces.append(ChessPiece("knight", "black", "G8"));
pieces.append(ChessPiece("rook", "black", "H8"));
for (char col = 'A'; col <= 'H'; ++col)
pieces.append(ChessPiece("pawn", "black", QString(col) + "7"));

// Place pieces on the board
for (ChessPiece &piece : pieces) {
placePieceOnTile(piece.position, piece.type, piece.color);
}
// ui->stackedWidget->setCurrentIndex(0);
}


void MainWindow::placePieceOnTile(const QString& position, const QString& pieceType, const QString& color)
{
QPushButton* button = boardMap[position];
if (button) {
QString imagePath = ":/images/images/" + color + "_" + pieceType + ".png";
qDebug() << "Attempting to open file at:" << imagePath;

QFile file(imagePath);
if (!file.open(QIODevice::ReadOnly)) {
qDebug() << "Failed to open file at:" << imagePath;
return;
}

QByteArray byteArray = file.readAll();
qDebug() << "File read successfully. Bytes length:" << byteArray.length();

QImage img = QImage::fromData(byteArray);
if (img.isNull()) {
qDebug() << "Failed to load QImage instance for:" << imagePath;
return;
}

QPixmap pixmap = QPixmap::fromImage(img);
QIcon icon(pixmap);
icon.addPixmap(pixmap, QIcon::Normal, QIcon::Off);


button->setIcon(icon);
button->setIconSize(QSize(30, 30));
button->setStyleSheet("QPushButton { padding-left: 15px; }, setStyleSheet(QPushButton{background: transparent;});");
}
}




// Handle tile clicks
void MainWindow::onTileClicked()
{
QPushButton* clickedButton = qobject_cast<QPushButton*>(sender());
if (!clickedButton) return;

QString clickedPosition;
for (auto it = boardMap.begin(); it != boardMap.end(); ++it) {
if (it.value() == clickedButton) {
clickedPosition = it.key();
break;
}
}

if (selectedPiece) {
// Moving the selected piece
if (isValidMove(selectedPiece->type, selectedPiece->position, clickedPosition)) {
// Update the piece's position
placePieceOnTile(clickedPosition, selectedPiece->type, selectedPiece->color);
boardMap[selectedPiece->position]->setIcon(QIcon()); // Clear the old tile
selectedPiece->position = clickedPosition;

// Deselect the piece
selectedPiece = nullptr;
} else {
qDebug() << "Invalid move!";
}
} else {
// Selecting a piece
for (ChessPiece &piece : pieces) {
if (piece.position == clickedPosition) {
selectedPiece = &piece;
break;
}
}
}
}


bool MainWindow::isValidMove(const QString& pieceType, const QString& from, const QString& to)
{
// Placeholder for move validation logic
return true; // Allow all moves for now
}



// End
void MainWindow::on_pushButton_home_end_clicked()
{
Expand Down
33 changes: 29 additions & 4 deletions embeddings/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,27 @@
#define MAINWINDOW_H

#include <QMainWindow>
#include <QPushButton>
#include <QMap>
#include <QList>
#include <QFile>
#include <QDir>

QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACE

class ChessPiece {
public:
QString type; // "pawn", "rook", etc.
QString color; // "white" or "black"
QString position; // Chess notation, e.g., "A1", "B2"

ChessPiece(QString t, QString c, QString p) : type(t), color(c), position(p) {}
};

class MainWindow : public QMainWindow
{
Q_OBJECT
Expand All @@ -17,23 +31,23 @@ class MainWindow : public QMainWindow
MainWindow(QWidget *parent = nullptr);
~MainWindow();



private slots:

//void on_pushButton_clicked();
void onTileClicked();

// <<<<<<< HEAD
void on_pushButton_home_about_clicked();

void on_pushButton_home_end_clicked();
// =======

void on_pushButton_start_clicked();

void on_pushButton_tutorial_clicked();

void on_pushButton_settings_clicked();

void on_pushButton_about_clicked();
// >>>>>>> ec80273e3b344ef1a23e998207c077d80fe31758

void on_pushButton_home_settings_clicked();

Expand All @@ -53,5 +67,16 @@ private slots:

private:
Ui::MainWindow *ui;
QMap<QString, QPushButton*> boardMap; // Map positions (A1, B2) to QPushButtons
QList<ChessPiece> pieces; // List of chess pieces
QString selectedPiecePosition; // Position of selected piece
ChessPiece* selectedPiece = nullptr; // Currently selected piece

void setupBoard();
void setupInitialPositions();
void placePieceOnTile(const QString& position, const QString& pieceType, const QString& color);
bool isValidMove(const QString& pieceType, const QString& from, const QString& to);

};

#endif // MAINWINDOW_H
Loading