Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Raspbian 2 player chess game
Forum Updated to NodeBB v4.3 + New Features

Raspbian 2 player chess game

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
56 Posts 4 Posters 9.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #42

    Rather than using lists that does not allow you to properly reason about how your pieces are put on your chessboard, you should cleanup your code first.

    Use proper naming for your variables. If you want to store the paths to your images in a single structure, use a QMap with a clear meaningful key using for example an enum.

    Write your code in clear separate steps. Start by drawing the board, then put the pawns in place, then the king, queen, etc. Stop trying to make everything as compact as possible. You are losing time and not learning to do things properly. Write code that you and other people can understand.

    Interested in AI ? www.idiap.ch
    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

    1 Reply Last reply
    1
    • Q Offline
      Q Offline
      Quantum1982
      wrote on last edited by
      #43

      Ok. My debugger doesn't work or load. Are there settings that I need to configure ?

      1 Reply Last reply
      0
      • Q Offline
        Q Offline
        Quantum1982
        wrote on last edited by
        #44

        When I put the pixmaps inside a loop, then it loads. But the other pieces don't ?
        ChessOutput.jpg

        JonBJ 1 Reply Last reply
        0
        • Q Quantum1982

          When I put the pixmaps inside a loop, then it loads. But the other pieces don't ?
          ChessOutput.jpg

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #45

          @Quantum1982
          a. Debug your code. That's what developers do when things go wrong.
          b. Deal with the warnings I see in the output window.

          1 Reply Last reply
          0
          • Q Offline
            Q Offline
            Quantum1982
            wrote on last edited by
            #46

            My debugger doesn't load ?

            JonBJ 1 Reply Last reply
            0
            • Q Quantum1982

              My debugger doesn't load ?

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #47

              @Quantum1982
              a. So sort out your debugger. And if you cannot do that yourself we need more than "My debugger doesn't load". Any error messages? How do you know it does not load?

              b. I didn't say you had to use the actual debugger. It's a luxury you should get working, but it's not a requirement for your issue(s). You could do lots of work yourself just by putting in judicious qDebug() statements. And by taking heed of my earlier suggestion to check return results and use Q_ASSERTs. So far your coding is progressing but you need to add such techniques. And it's not just that they solve issues, they also help you to understand what is going on, making future coding better. In the case of the QGraphicsScene::addItme warnings you don't even need any of this, just looking at your code should tell you.

              1 Reply Last reply
              1
              • Q Offline
                Q Offline
                Quantum1982
                wrote on last edited by
                #48

                Ok.

                I get the following error : Unable to create a debugging engine.

                Also, is there a property in the QGraphicsView that I need to set to stop the last column and row from stretching to fit the output size ?

                1 Reply Last reply
                0
                • Q Offline
                  Q Offline
                  Quantum1982
                  wrote on last edited by
                  #49

                  I am now implementing the mouse clocking by the user.
                  I have this so far :

                  class MyGraphicsPixmapItem : public QGraphicsPixmapItem
                  {
                  public:
                  int SelectedSquareX, SelectedSquareY;
                  void mousePressEvent(QMouseEvent *eventPress);
                  };

                  What I am doing is I am getting the position of the mouse cursor when the user elft clicks, then I am storing the board coordinates in SelectedSquareX and SelectedSquareY.

                  But how should I declare the constructor ?

                  jsulmJ 1 Reply Last reply
                  0
                  • Q Quantum1982

                    I am now implementing the mouse clocking by the user.
                    I have this so far :

                    class MyGraphicsPixmapItem : public QGraphicsPixmapItem
                    {
                    public:
                    int SelectedSquareX, SelectedSquareY;
                    void mousePressEvent(QMouseEvent *eventPress);
                    };

                    What I am doing is I am getting the position of the mouse cursor when the user elft clicks, then I am storing the board coordinates in SelectedSquareX and SelectedSquareY.

                    But how should I declare the constructor ?

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #50

                    @Quantum1982 said in Raspbian 2 player chess game:

                    But how should I declare the constructor ?

                    Like https://doc.qt.io/qt-5/qgraphicspixmapitem.html does maybe?

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    1
                    • Q Offline
                      Q Offline
                      Quantum1982
                      wrote on last edited by
                      #51

                      Ok I will look into that.
                      Concerning the drawn chessboard I have the following :ChessOutput.jpg

                      As you can see the last column and row are being stretched to fir the graphicsview area.
                      Is there a setting that I need to configure that willl stop this ?

                      1 Reply Last reply
                      0
                      • Q Offline
                        Q Offline
                        Quantum1982
                        wrote on last edited by
                        #52

                        Ok
                        I have made some progress.
                        This is what I have so far :

                        #ifndef MAINWINDOW_H
                        #define MAINWINDOW_H
                        #endif // MAINWINDOW_H
                        
                        #define BOARD_X1 20
                        #define BOARD_Y1 20
                        #define SQUARE_WIDTH 50
                        #define SQUARE_HEIGHT 50
                        
                        #define WHITE_PAWN 0
                        #define WHITE_ROOK 1
                        #define WHITE_KNIGHT 2
                        #define WHITE_BISHOP 3
                        #define WHITE_QUEEN 4
                        #define WHITE_KING 5
                        
                        #define BLACK_PAWN 6
                        #define BLACK_ROOK 7
                        #define BLACK_KNIGHT 8
                        #define BLACK_BISHOP 9
                        #define BLACK_QUEEN 10
                        #define BLACK_KING 11
                        
                        #include <QMainWindow>
                        #include <QGraphicsView>
                        #include <QGraphicsItem>
                        #include <QLabel>
                        #include <QMouseEvent>
                        
                        class CChessBoard : public QGraphicsView
                        {
                        public:
                        
                        };
                        
                        QT_BEGIN_NAMESPACE
                        namespace Ui { class MainWindow; }
                        QT_END_NAMESPACE
                        
                        
                        
                        class MainWindow : public QMainWindow
                        {
                            Q_OBJECT
                        
                        public:
                            QLabel *WhiteQueen;
                            QString ChessPieces[12];
                        
                            MainWindow(QWidget *parent = nullptr);
                            ~MainWindow();
                        
                        private:
                            Ui::MainWindow *ui;
                            QGraphicsScene *scene;
                            QGraphicsRectItem *rectangle;
                        
                            int ChessPieceIndexes[8][8]; // indexes to the image url
                            void LoadPieceGraphics(void);
                            void SetStartPosition(void);
                        };
                        
                        
                        
                        class MyGraphicsPixmapItem : public QGraphicsPixmapItem
                        {
                           public:
                            int SelectedSquareX, SelectedSquareY;
                            MyGraphicsPixmapItem(QGraphicsItem *parent = nullptr);
                            void mousePressEvent(QMouseEvent *eventPress);
                        };
                        
                        #include "mainwindow.h"
                        #include "ui_mainwindow.h"
                        
                        #include <QImage>
                        #include <QLabel>
                        
                        MainWindow::MainWindow(QWidget *parent)
                            : QMainWindow(parent)
                            , ui(new Ui::MainWindow)
                        {
                            ui->setupUi(this);
                            scene = new QGraphicsScene(this);
                            ui->graphicsView->setScene(scene);
                        
                           // MyGraphicsPixmapItem UserInput = MyGraphicsPixmapItem(QGraphicsItem *parent= nullptr);
                        
                            /* ///////////////////////////////////////////DRAW THE BOARD //////////////////////////////////////////////*/
                            QBrush greenBrush(Qt::green);
                            QBrush blueBrush(Qt::blue);
                            QPen outlinePen(Qt::black);
                            outlinePen.setWidth(2);
                        
                            for (int i=0;i<8;i++)
                                for (int j=0;j<8;j++)
                                    if ((i+j)%2==0)
                                    rectangle = scene->addRect(BOARD_X1+(i*SQUARE_WIDTH),BOARD_Y1+(j*SQUARE_HEIGHT)
                                                               ,BOARD_X1+(i*SQUARE_WIDTH)+SQUARE_WIDTH ,
                                                               BOARD_Y1+(j*SQUARE_HEIGHT)+SQUARE_HEIGHT, outlinePen, blueBrush);
                                    else rectangle = scene->addRect(BOARD_X1+(i*SQUARE_WIDTH),BOARD_Y1+(j*SQUARE_HEIGHT)
                                                                    ,BOARD_X1+(i*SQUARE_WIDTH)+SQUARE_WIDTH ,
                                                                    BOARD_Y1+(j*SQUARE_HEIGHT)+SQUARE_HEIGHT, outlinePen, greenBrush);
                           /* ///////////////////////////////////////////DRAW THE BOARD //////////////////////////////////////////////*/
                        
                            LoadPieceGraphics();
                            SetStartPosition();
                        
                            /*
                            WhiteQueen = new QLabel(this);
                            QPixmap Pixmap(ChessPieces[4]); // White Queen
                        
                            WhiteQueen->setGeometry(BOARD_X1+4*SQUARE_WIDTH,BOARD_Y1+4*SQUARE_HEIGHT,
                                                    BOARD_X1+4*SQUARE_WIDTH+SQUARE_WIDTH,BOARD_Y1+4*SQUARE_HEIGHT+SQUARE_HEIGHT);
                            WhiteQueen->setPixmap(Pixmap);
                            WhiteQueen->setStyleSheet("background-color:rgba(0,0,255,0)");
                        
                            QLabel *BlackQueen = new QLabel(this);
                            Pixmap.load(ChessPieces[10]); // Black Queen
                            BlackQueen->setGeometry(BOARD_X1+4*SQUARE_WIDTH,BOARD_Y1+1*SQUARE_HEIGHT,
                                                    BOARD_X1+4*SQUARE_WIDTH+SQUARE_WIDTH,BOARD_Y1+1*SQUARE_HEIGHT+SQUARE_HEIGHT);
                            BlackQueen->setPixmap(Pixmap);
                            */
                        
                            QPixmap Pixmap(ChessPieces[0]); // White Pawn
                        
                            Pixmap.load(ChessPieces[0]);
                            for (int i=0;i<8;i++)
                            {
                                QGraphicsPixmapItem* item = new QGraphicsPixmapItem(Pixmap);
                                item->setPos(BOARD_X1+(i*SQUARE_WIDTH),BOARD_Y1+(7*SQUARE_HEIGHT));
                                scene->addItem(item);
                            }
                            Pixmap.load(ChessPieces[1]);
                            for (int i=0;i<8;i++)
                            {
                                QGraphicsPixmapItem* item = new QGraphicsPixmapItem(Pixmap);
                                item->setPos(BOARD_X1+(i*SQUARE_WIDTH),BOARD_Y1+(8*SQUARE_HEIGHT));
                                scene->addItem(item);
                            }
                        
                            // White Rooks
                            Pixmap.load(ChessPieces[1]);
                            QGraphicsPixmapItem* item = new QGraphicsPixmapItem(Pixmap);
                            item->setPos(BOARD_X1+(0*SQUARE_WIDTH),BOARD_Y1+(8*SQUARE_HEIGHT));
                            scene->addItem(item);
                        
                            Pixmap.load(ChessPieces[1]);
                            //QGraphicsPixmapItem* item = new QGraphicsPixmapItem(Pixmap);
                            item->setPos(BOARD_X1+(7*SQUARE_WIDTH),BOARD_Y1+(8*SQUARE_HEIGHT));
                            scene->addItem(item);
                        
                            // White Knights
                            Pixmap.load(ChessPieces[2]);
                            item->setPixmap(Pixmap);
                            item->setPos(BOARD_X1+(1*SQUARE_WIDTH),BOARD_Y1+(8*SQUARE_HEIGHT));
                            scene->addItem(item);
                        
                            item->setPos(BOARD_X1+(6*SQUARE_WIDTH),BOARD_Y1+(8*SQUARE_HEIGHT));
                            scene->addItem(item);
                        
                        
                            Pixmap.load(ChessPieces[6]); // Black Pawn
                            for (int i=0;i<8;i++)
                            {
                                QGraphicsPixmapItem* item = new QGraphicsPixmapItem(Pixmap);
                                item->setPos(BOARD_X1+(i*SQUARE_WIDTH),BOARD_Y1+(1*SQUARE_HEIGHT));
                                scene->addItem(item);
                            }
                        
                            Pixmap.load(ChessPieces[7]); // Black Rook
                            item->setPixmap(Pixmap);
                            item->setPos(BOARD_X1+(0*SQUARE_WIDTH),BOARD_Y1+(0*SQUARE_HEIGHT));
                            scene->addItem(item);
                        
                            item->setPixmap(Pixmap);
                            item->setPos(BOARD_X1+(7*SQUARE_WIDTH),BOARD_Y1+(0*SQUARE_HEIGHT));
                            scene->addItem(item);
                        
                            Pixmap.load(ChessPieces[8]); // Black Knight
                            item->setPixmap(Pixmap);
                            item->setPos(BOARD_X1+(1*SQUARE_WIDTH),BOARD_Y1+(0*SQUARE_HEIGHT));
                            scene->addItem(item);
                        
                            Pixmap.load(ChessPieces[9]); // Black Bishop
                            item->setPixmap(Pixmap);
                            item->setPos(BOARD_X1+(2*SQUARE_WIDTH),BOARD_Y1+(0*SQUARE_HEIGHT));
                            scene->addItem(item);
                        
                        
                        }
                        
                        MainWindow::~MainWindow()
                        {
                            delete ui;
                        }
                        
                        void MainWindow::LoadPieceGraphics(void)
                        {
                            ChessPieces[0] = "C:/Users/User/Desktop/WhitePawn.jpg";
                            ChessPieces[1] = "C:/Users/User/Desktop/WhiteRook.jpg";
                            ChessPieces[2] = "C:/Users/User/Desktop/WhiteKnight.jpg";
                            ChessPieces[3] = "C:/Users/User/Desktop/WhiteBishop.jpg";
                            ChessPieces[4] = "C:/Users/User/Desktop/WhiteQueen.jpg";
                            ChessPieces[5] = "C:/Users/User/Desktop/WhiteKing.jpg";
                            ChessPieces[6] = "C:/Users/User/Desktop/BlackPawn.jpg";
                            ChessPieces[7] = "C:/Users/User/Desktop/BlackRook.jpg";
                            ChessPieces[8] = "C:/Users/User/Desktop/BlackKnight.jpg";
                            ChessPieces[9] = "C:/Users/User/Desktop/BlackBishop.jpg";
                            ChessPieces[10] = "C:/Users/User/Desktop/BlackQueen.jpg";
                            ChessPieces[11] = "C:/Users/User/Desktop/BlackKing.jpg";
                        }
                        
                        void MainWindow::SetStartPosition(void)
                        {
                            for (int i=0;i<8;i++)
                                ChessPieceIndexes[i][1] = WHITE_PAWN; // White Pawns
                            ChessPieceIndexes[0][0] = WHITE_ROOK; // White Rook
                            ChessPieceIndexes[7][0] = WHITE_ROOK; // White Rook
                        
                            ChessPieceIndexes[1][0] = WHITE_KNIGHT; // White Knight
                            ChessPieceIndexes[7][0] = WHITE_KNIGHT; // White Knight
                        
                            ChessPieceIndexes[2][0] = WHITE_BISHOP; // White Bishop
                            ChessPieceIndexes[6][0] = WHITE_BISHOP; // White Bishop
                        
                            ChessPieceIndexes[3][0] = WHITE_QUEEN; // White Queen
                            ChessPieceIndexes[4][0] = WHITE_KING; // White King
                        
                            for (int i=0;i<8;i++)
                                ChessPieceIndexes[i][6] = BLACK_PAWN; // Black Pawns
                            ChessPieceIndexes[0][7] = BLACK_ROOK; // Black Rook
                            ChessPieceIndexes[7][7] = BLACK_ROOK; // Black Rook
                        
                            ChessPieceIndexes[1][7] = BLACK_KNIGHT; // Black Knight
                            ChessPieceIndexes[7][7] = BLACK_KNIGHT; // Black Knight
                        
                            ChessPieceIndexes[2][7] = BLACK_BISHOP; // Black Bishop
                            ChessPieceIndexes[6][7] = BLACK_BISHOP; // Black Bishop
                        
                            ChessPieceIndexes[3][7] = BLACK_QUEEN; // Black Queen
                            ChessPieceIndexes[4][7] = BLACK_KING; // Black King
                        
                        }
                        
                        
                        void MyGraphicsPixmapItem::mousePressEvent(QMouseEvent *eventPress)
                        {
                            SelectedSquareX = eventPress->pos().x();
                            SelectedSquareY = eventPress->pos().y();
                        
                        }
                        
                        [enter image description here][1]
                        
                        
                          [1]: https://i.stack.imgur.com/XgstN.jpg
                        

                        There are 3 issues which need clarification :

                        1. How do I stop the streching of the last column and row of the chess board ?

                        2. How do I make the chess pieces transparent ?

                        3. How do I implement the mouse click event so that a square gets highlighted when the user presses the left mouse button ?

                        1 Reply Last reply
                        0
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #53
                          1. I can't tell right now
                          2. Make the original image background transparent
                          3. You can use a QRubberBand for that for exampl

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply
                          0
                          • Q Offline
                            Q Offline
                            Quantum1982
                            wrote on last edited by
                            #54

                            Thanks. I see that QGraphicsPixmapItem has a ShapeMode property. Can one set transparency with this ?

                            SGaistS 1 Reply Last reply
                            0
                            • Q Offline
                              Q Offline
                              Quantum1982
                              wrote on last edited by
                              #55

                              Also, do you have an idea as to why the pixmap items are not being added to the scene ?

                              1 Reply Last reply
                              0
                              • Q Quantum1982

                                Thanks. I see that QGraphicsPixmapItem has a ShapeMode property. Can one set transparency with this ?

                                SGaistS Offline
                                SGaistS Offline
                                SGaist
                                Lifetime Qt Champion
                                wrote on last edited by
                                #56

                                @Quantum1982 said in Raspbian 2 player chess game:

                                Thanks. I see that QGraphicsPixmapItem has a ShapeMode property. Can one set transparency with this ?

                                If you think of MaskShape, it still mentions that the source must have an alpha channel. It's also expensive.

                                @Quantum1982 said in Raspbian 2 player chess game:

                                Also, do you have an idea as to why the pixmap items are not being added to the scene ?

                                Check the coordinates of your items.

                                Interested in AI ? www.idiap.ch
                                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                1 Reply Last reply
                                0

                                • Login

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • Users
                                • Groups
                                • Search
                                • Get Qt Extensions
                                • Unsolved