[text] код

Viewer

  1. #ifndef BISHOP_H
  2. #define BISHOP_H
  3.  
  4. #include "chesspiece.h"
  5. class Bishop:public ChessPiece
  6. {
  7. public:
  8.     Bishop(QString team,QGraphicsItem *parent = 0);
  9.     void setImage();
  10.  
  11.     void moves();
  12.  
  13. };
  14.  
  15.  
  16. #endif // BISHOP_H
  17. #include "bishop.h"
  18. #include <QDebug>
  19. #include "game.h"
  20. extern Game *game;
  21.  
  22. Bishop::Bishop(QString team,QGraphicsItem *parent):ChessPiece(team,parent)
  23. {
  24.     setImage();
  25. }
  26.  
  27. void Bishop::setImage()
  28. {
  29.     if(side == "WHITE")
  30.         setPixmap(QPixmap("E:/kursovaya3sem/myProject/media/whitebishop.png"));
  31.     else
  32.         setPixmap(QPixmap("E:/kursovaya3sem/myProject/media/blackbishop.png"));
  33. }
  34.  
  35.  
  36. void Bishop::moves()
  37. {
  38.     location.clear();
  39.     int row = this->getCurrentBox()->rowLoc;
  40.     int col = this->getCurrentBox()->colLoc;
  41.     QString team = this->getSide();
  42.     //For upper Left
  43.  
  44.      for(int i = row-1,j = col-1; i >= 0 && j >=0; i--,j--) {
  45.        if(game->collection[i][j]->getChessPieceColor() == team ) {
  46.            break;
  47.        }
  48.        else
  49.        {
  50.            location.append(game->collection[i][j]);
  51.            if(boxSetting(location.last()) ){
  52.                break;
  53.            }
  54.        }
  55.     }
  56.  
  57.      //For upper right
  58.  
  59.       for(int i = row-1,j = col+1; i >= 0 && j <= 7; i--,j++) {
  60.         if(game->collection[i][j]->getChessPieceColor() == team ) {
  61.             break;
  62.         }
  63.         else
  64.         {
  65.             location.append(game->collection[i][j]);
  66.             if(boxSetting(location.last())){
  67.                 break;
  68.             }
  69.         }
  70.      }
  71.  
  72.       //For downward right
  73.  
  74.        for(int i = row+1,j = col+1; i <= 7 && j <= 7; i++,j++) {
  75.          if(game->collection[i][j]->getChessPieceColor() == team ) {
  76.              break;
  77.  
  78.          }
  79.          else
  80.          {
  81.              location.append(game->collection[i][j]);
  82.              if(boxSetting(location.last())){
  83.                  break;
  84.              }
  85.          }
  86.       }
  87.  
  88.        //For downward left
  89.  
  90.         for(int i = row+1,j = col-1; i <= 7 && j >= 0; i++,j--) {
  91.           if(game->collection[i][j]->getChessPieceColor() == team ) {
  92.               break;
  93.  
  94.           }
  95.           else
  96.           {
  97.               location.append(game->collection[i][j]);
  98.               if(boxSetting(location.last())){
  99.                   break;
  100.               }
  101.  
  102.           }
  103.        }
  104.  
  105. }
  106. #ifndef BUTTON_H
  107. #define BUTTON_H
  108. #include <QGraphicsSceneMouseEvent>
  109. #include <QGraphicsRectItem>
  110.  
  111.  
  112. class Button:public QObject, public QGraphicsRectItem
  113. {
  114.      Q_OBJECT
  115. public:
  116.  
  117.     Button(QString name, QGraphicsItem * parent = NULL);
  118.  
  119.     //public methods
  120.     void setFont(QFont const textFont);
  121.  
  122.     //events
  123.     void mousePressEvent(QGraphicsSceneMouseEvent *event);
  124.     void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
  125.     void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
  126. signals:
  127.     void clicked();
  128. private:
  129.     QGraphicsTextItem *text;
  130. };
  131. #endif // BUTTON_H
  132. #include "button.h"
  133. #include "qfont.h"
  134. #include <QGraphicsTextItem>
  135. #include <QBrush>
  136.  
  137. Button::Button(QString name, QGraphicsItem *parent)
  138.     :QGraphicsRectItem(parent)
  139. {
  140.     //draw the rect
  141.     setRect(0,0,150,50);
  142.     QBrush brush;
  143.     brush.setStyle(Qt::SolidPattern);
  144.     brush.setColor(QColor::fromRgb(qRgb(61, 61, 61)));
  145.     setBrush(brush);
  146.  
  147.     //draw Text
  148.     text = new QGraphicsTextItem(name,this);
  149.     int xPos = rect().width()/2 - text->boundingRect().width()/2-15;
  150.     int yPos = rect().height()/2 - text->boundingRect().height()/2;
  151.     text->setPos(xPos,yPos);
  152.     text->setDefaultTextColor(QColor::fromRgb(qRgb(247, 247, 247)));
  153.  
  154.  
  155.     //Allow responding to hover
  156.     setAcceptHoverEvents(true);
  157. }
  158.  
  159. void Button::setFont(QFont const textFont)
  160. {
  161.     text->setFont(textFont);
  162. }
  163.  
  164. void Button::mousePressEvent(QGraphicsSceneMouseEvent *event)
  165. {
  166.     if(event)
  167.     emit clicked();
  168.  
  169. }
  170.  
  171. void Button::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
  172. {
  173.     //change color
  174.     if(event){
  175.     QBrush brush;
  176.     brush.setStyle(Qt::SolidPattern);
  177.     brush.setColor(QColor::fromRgb(qRgb(127, 127, 127)));
  178.     setBrush(brush);
  179.     }
  180. }
  181. void Button::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) {
  182.     //change color
  183.     if(event){
  184.     QBrush brush;
  185.     brush.setStyle(Qt::SolidPattern);
  186.     brush.setColor(QColor::fromRgb(qRgb(61, 61, 61)));
  187.     setBrush(brush);
  188.     }
  189. }
  190. #ifndef CHESSBOARD_H
  191. #define CHESSBOARD_H
  192. #include <QGraphicsRectItem>
  193. #include "chesspiece.h"
  194. class ChessBoard
  195. {
  196. public:
  197.     ChessBoard();
  198.  
  199.     //drawing public function
  200.     void drawBoxes(int x, int y);
  201.     void addChessPiece();
  202.  
  203.     void setUpWhite();
  204.     void setUpBlack();
  205.     void reset();
  206. private:
  207.     QList <ChessPiece *> white;
  208.     QList <ChessPiece *> black;
  209. };
  210.  
  211. #endif // CHESSBOARD_H
  212.  
  213. #include "chessboard.h"
  214. #include "chessbox.h"
  215. #include "game.h"
  216. #include "queen.h"
  217. #include "rook.h"
  218. #include "pawn.h"
  219. #include "king.h"
  220. #include "knight.h"
  221. #include "bishop.h"
  222.  
  223. extern Game *game;
  224.  
  225. ChessBoard::ChessBoard()
  226. {
  227.     setUpBlack();
  228.     setUpWhite();
  229. }
  230.  
  231. void ChessBoard::drawBoxes(int x,int y)
  232. {
  233.     int SHIFT = 100;
  234.     for(int i = 0; i < 8; i++) {
  235.         for(int j = 0; j < 8; j++)
  236.         {
  237.             ChessBox *box = new ChessBox();
  238.             game->collection[i][j] = box;
  239.             box->rowLoc = i;
  240.             box->colLoc = j;
  241.             box->setPos(x+SHIFT*j,y+SHIFT*i);
  242.             if((i+j)%2==0)
  243.                 box->setOriginalColor(QColor::fromRgb(qRgb(247, 247, 247))); //white
  244.             else
  245.                 box->setOriginalColor(QColor::fromRgb(qRgb(118, 118, 118))); //black
  246.             game->addToScene(box);
  247.         }
  248.     }
  249. }
  250.  
  251. void ChessBoard::addChessPiece() {
  252.     for(int i = 0; i < 8; i++) {
  253.         for(int j = 0; j < 8; j++)
  254.         {
  255.             ChessBox *box =game->collection[i][j];
  256.             if(i < 2) {
  257.                 static int k;
  258.                 box->placePiece(black[k]);
  259.                 game->alivePiece.append(black[k]);
  260.                 game->addToScene(black[k++]);
  261.             }
  262.             if(i > 5) {
  263.                 static int h;
  264.                 box->placePiece(white[h]);
  265.                 game->alivePiece.append(white[h]);
  266.                 game->addToScene(white[h++]);
  267.             }
  268.         }
  269.     }
  270. }
  271.  
  272. void ChessBoard::setUpWhite()
  273. {
  274.     ChessPiece *piece;
  275.     for(int i = 0; i < 8; i++) {
  276.         piece = new Pawn("WHITE");
  277.         white.append(piece);
  278.     }
  279.     piece = new Rook("WHITE");
  280.     white.append(piece);
  281.     piece = new Knight("WHITE");
  282.     white.append(piece);
  283.     piece = new Bishop("WHITE");
  284.     white.append(piece);
  285.     piece = new Queen("WHITE");
  286.     white.append(piece);
  287.     piece = new King("WHITE");
  288.     white.append(piece);
  289.     piece = new Bishop("WHITE");
  290.     white.append(piece);
  291.     piece = new Knight("WHITE");
  292.     white.append(piece);
  293.     piece = new Rook("WHITE");
  294.     white.append(piece);
  295. }
  296.  
  297. void ChessBoard::setUpBlack()
  298. {
  299.     ChessPiece *piece;
  300.     piece = new Rook("BLACK");
  301.     black.append(piece);
  302.     piece = new Knight("BLACK");
  303.     black.append(piece);
  304.     piece = new Bishop("BLACK");
  305.     black.append(piece);
  306.     piece = new Queen("BLACK");
  307.     black.append(piece);
  308.     piece = new King("BLACK");
  309.     black.append(piece);
  310.     piece = new Bishop("BLACK");
  311.     black.append(piece);
  312.     piece = new Knight("BLACK");
  313.     black.append(piece);
  314.     piece = new Rook("BLACK");
  315.     black.append(piece);
  316.     for(int i = 0; i < 8; i++) {
  317.         piece = new Pawn("BLACK");
  318.         black.append(piece);
  319.     }
  320. }
  321.  
  322. void ChessBoard::reset() {
  323.     int k = 0; int h = 0;
  324.     for(int i = 0; i < 8; i++) {
  325.         for(int j = 0; j < 8; j++)
  326.         {
  327.             ChessBox *box =game->collection[i][j];
  328.             box->setHasChessPiece(false);
  329.             box->setChessPieceColor("NONE");
  330.             box->currentPiece = NULL;
  331.             if(i < 2) {
  332.                 box->placePiece(black[k]);
  333.                 black[k]->setIsPlaced(true);
  334.                 black[k]->firstMove = true;
  335.                 game->alivePiece.append(black[k++]);
  336.             }
  337.             if(i > 5) {
  338.                 box->placePiece(white[h]);
  339.                 white[h]->setIsPlaced(true);
  340.                 white[h]->firstMove = true;
  341.                 game->alivePiece.append(white[h++]);
  342.             }
  343.         }
  344.     }
  345. }
  346. #include "chessbox.h"
  347. #include "game.h"
  348. #include <QDebug>
  349. #include "king.h"
  350.  
  351. extern Game *game;
  352.  
  353. ChessBox::ChessBox(QGraphicsItem *parent):QGraphicsRectItem(parent)
  354. {
  355.     //making the Square CHess Box
  356.     setRect(0,0,100,100);
  357.     brush.setStyle(Qt::SolidPattern);
  358.     setZValue(-1);
  359.     setHasChessPiece(false);
  360.     setChessPieceColor("NONE");
  361.     currentPiece = NULL;
  362. }
  363.  
  364. ChessBox::~ChessBox()
  365. {
  366.     delete this;
  367. }
  368.  
  369. void ChessBox::mousePressEvent(QGraphicsSceneMouseEvent *event)
  370. {
  371.         //Deselecting counter part of ChessPiece
  372.         if(currentPiece == game->pieceToMove && currentPiece){
  373.             currentPiece->mousePressEvent(event);
  374.             return;
  375.         }
  376.  
  377.         //if selected
  378.         if(game->pieceToMove){
  379.             //if same team
  380.             if(this->getChessPieceColor() == game->pieceToMove->getSide())
  381.                 return;
  382.             //removing the eaten piece
  383.             QList <ChessBox *> movLoc = game->pieceToMove->moveLocation();
  384.             //TO make sure the selected box is in move zone
  385.             int check = 0;
  386.             for(size_t i = 0, n = movLoc.size(); i < n;i++) {
  387.                 if(movLoc[i] == this) {
  388.                     check++;
  389.                 }
  390.             }
  391.             // if not prsent return
  392.             if(check == 0)
  393.                 return;
  394.             //change the color back to normal
  395.              game->pieceToMove->decolor();
  396.              //make the first move false applicable for pawn only
  397.              game->pieceToMove->firstMove = false;
  398.              //this is to eat or consume the enemy present inn the movable region
  399.             if(this->getHasChessPiece()){
  400.                 this->currentPiece->setIsPlaced(false);
  401.                 this->currentPiece->setCurrentBox(NULL);
  402.                 game->placeInDeadPlace(this->currentPiece);
  403.  
  404.             }
  405.             //changing the new stat and resetting the previous left region
  406.             game->pieceToMove->getCurrentBox()->setHasChessPiece(false);
  407.             game->pieceToMove->getCurrentBox()->currentPiece = NULL;
  408.             game->pieceToMove->getCurrentBox()->resetOriginalColor();
  409.             placePiece(game->pieceToMove);
  410.  
  411.             game->pieceToMove = NULL;
  412.             //changing turn
  413.             game->changeTurn();
  414.             checkForCheck();
  415.         }
  416.         //Selecting couterpart of the chessPiece
  417.         else if(this->getHasChessPiece())
  418.         {
  419.             this->currentPiece->mousePressEvent(event);
  420.         }
  421. }
  422.  
  423. void ChessBox::setColor(QColor color)
  424. {
  425.     brush.setColor(color);
  426.     setBrush(color);
  427. }
  428.  
  429. void ChessBox::placePiece(ChessPiece *piece)
  430. {
  431.     piece->setPos(x()+50- piece->pixmap().width()/2  ,y()+50-piece->pixmap().width()/2);
  432.     piece->setCurrentBox(this);
  433.     setHasChessPiece(true,piece);
  434.     currentPiece = piece;
  435. }
  436.  
  437. void ChessBox::resetOriginalColor()
  438. {
  439.     setColor(originalColor);
  440. }
  441.  
  442. void ChessBox::setOriginalColor(QColor value)
  443. {
  444.     originalColor = value;
  445.     setColor(originalColor);
  446. }
  447.  
  448. bool ChessBox::getHasChessPiece()
  449. {
  450.     return hasChessPiece;
  451. }
  452.  
  453. void ChessBox::setHasChessPiece(bool value, ChessPiece *piece)
  454. {
  455.     hasChessPiece = value;
  456.     if(value)
  457.         setChessPieceColor(piece->getSide());
  458.     else
  459.         setChessPieceColor("NONE");
  460. }
  461.  
  462. void ChessBox::checkForCheck()
  463. {
  464.     int c = 0;
  465.     QList <ChessPiece *> pList = game->alivePiece;
  466.     for(size_t i = 0,n=pList.size(); i < n; i++ ) {
  467.  
  468.         King * p = dynamic_cast<King *> (pList[i]);
  469.         if(p){
  470.             continue;
  471.         }
  472.         pList[i]->moves();
  473.         pList[i]->decolor();
  474.         QList <ChessBox *> bList = pList[i]->moveLocation();
  475.         for(size_t j = 0,n = bList.size(); j < n; j ++) {
  476.             King * p = dynamic_cast<King *> (bList[j]->currentPiece);
  477.             if(p) {
  478.                 if(p->getSide() == pList[i]->getSide())
  479.                     continue;
  480.                 bList[j]->setColor(Qt::darkMagenta);
  481.  
  482. //                if(pList[i]->getCurrentBox()->getChessPieceColor() == "WHITE")
  483. //                    emit game->whiteWon();
  484. //                else
  485. //                   emit  game->blackWon();
  486. //                game->gameOver();
  487.                 //pList[i]->getCurrentBox()->setColor(Qt::darkYellow);
  488.                 if(!game->check->isVisible())
  489.                     game->check->setVisible(true);
  490.                 else{
  491.                     bList[j]->resetOriginalColor();
  492.                     pList[i]->getCurrentBox()->resetOriginalColor();
  493.                     //game->gameOver();
  494.                 }
  495.                 c++;
  496.  
  497.             }
  498.         }
  499.     }
  500.     if(!c){
  501.         game->check->setVisible(false);
  502.         for(size_t i = 0,n=pList.size(); i < n; i++ )
  503.             pList[i]->getCurrentBox()->resetOriginalColor();
  504.     }
  505. }
  506.  
  507. QString ChessBox::getChessPieceColor()
  508. {
  509.     return chessPieceColor;
  510. }
  511.  
  512. void ChessBox::setChessPieceColor(QString value)
  513. {
  514.     chessPieceColor = value;
  515. }
  516. #ifndef CHESSPIECE_H
  517. #define CHESSPIECE_H
  518.  
  519. #include <QGraphicsPixmapItem>
  520. #include "chessbox.h"
  521. #include <QGraphicsSceneMouseEvent>
  522.  
  523. class ChessBox;
  524. class ChessPiece:public QGraphicsPixmapItem
  525. {
  526. public:
  527.     ChessPiece(QString team = "",QGraphicsItem *parent = 0);
  528.  
  529.     void mousePressEvent(QGraphicsSceneMouseEvent *event);
  530.     void setCurrentBox(ChessBox *box);
  531.  
  532.     ChessBox *getCurrentBox() ;
  533.  
  534.     QString getSide() ;
  535.     void setSide( QString value);
  536.     virtual void setImage() = 0;
  537.  
  538.     bool getIsPlaced() ;
  539.     void setIsPlaced(bool value);
  540.  
  541.     QList <ChessBox *> moveLocation();
  542.     virtual void moves() = 0;
  543.     void decolor();
  544.  
  545.     bool firstMove;
  546.  
  547.     bool boxSetting(ChessBox *box);
  548. protected:
  549.     ChessBox *currentBox;
  550.     QString side;
  551.     bool isPlaced;
  552.     QList <ChessBox *> location;
  553.  
  554. };
  555.  
  556. #endif // CHESSPIECE_H
  557. #include "chesspiece.h"
  558. #include "game.h"
  559. #include <QDebug>
  560. #include "king.h"
  561.  
  562. extern Game *game;
  563. ChessPiece::ChessPiece(QString team, QGraphicsItem *parent):QGraphicsPixmapItem(parent)
  564. {
  565.     side = team;
  566.     isPlaced = true;
  567.     firstMove = true;
  568. }
  569.  
  570. void ChessPiece::mousePressEvent(QGraphicsSceneMouseEvent *event)
  571. {
  572.     //Deselect
  573.     if(this == game->pieceToMove){
  574.         game->pieceToMove->getCurrentBox()->resetOriginalColor();
  575.         game->pieceToMove->decolor();
  576.         game->pieceToMove = NULL;
  577.        return;
  578.     }
  579.     //if it is already consumed or not the respective color's turn
  580.     if((!getIsPlaced() )|| ( (game->getTurn() != this->getSide())&& (!game->pieceToMove)) )
  581.         return;
  582.     //selecting
  583.     if(!game->pieceToMove){
  584.         game->pieceToMove = this;
  585.         game->pieceToMove->getCurrentBox()->setColor(Qt::darkGreen);
  586.         game->pieceToMove->moves();
  587.     }
  588.     //Consuming counterPart of the CHessBox
  589.     else if(this->getSide() != game->pieceToMove->getSide()){
  590.         this->getCurrentBox()->mousePressEvent(event);
  591.     }
  592.  
  593.  
  594. }
  595.  
  596. void ChessPiece::setCurrentBox(ChessBox *box)
  597. {
  598.  
  599.     currentBox = box;
  600. }
  601.  
  602. ChessBox *ChessPiece::getCurrentBox()
  603. {
  604.     return currentBox;
  605. }
  606.  
  607. QString ChessPiece::getSide()
  608. {
  609.     return side;
  610. }
  611.  
  612. void ChessPiece::setSide( QString value)
  613. {
  614.     side = value;
  615. }
  616.  
  617. bool ChessPiece::getIsPlaced()
  618. {
  619.     return isPlaced;
  620. }
  621.  
  622. void ChessPiece::setIsPlaced(bool value)
  623. {
  624.     isPlaced = value;
  625.  
  626. }
  627.  
  628. QList<ChessBox *> ChessPiece::moveLocation()
  629. {
  630.     return location;
  631. }
  632.  
  633. void ChessPiece::decolor()
  634. {
  635.     for(size_t i = 0, n = location.size(); i < n;i++) {
  636.          location[i]->resetOriginalColor();
  637.     }
  638. }
  639.  
  640. bool ChessPiece::boxSetting(ChessBox *box)
  641. {
  642.     if(box->getHasChessPiece()) {
  643.         King *q = dynamic_cast<King*>(location.last()->currentPiece);
  644.         if(q){
  645.             box->setColor(Qt::darkMagenta);
  646.         }
  647.         else
  648.             box->setColor(Qt::yellow);
  649.         return true;
  650.     }
  651.     else
  652.         location.last()->setColor(Qt::green);
  653.     return false;
  654. }
  655.  
  656. #ifndef GAME_H
  657. #define GAME_H
  658. #include <QGraphicsView>
  659. #include <QGraphicsScene>
  660. #include <chessboard.h>
  661. #include "chesspiece.h"
  662. class Game:public QGraphicsView
  663. {
  664.     Q_OBJECT
  665. public:
  666.     //Constructors
  667.     Game(QWidget *parent = 0);
  668.  
  669.     //public Methods
  670.     void drawDeadHolder(int x, int y, QColor color);
  671.     void drawChessBoard();
  672.     void displayDeadWhite();
  673.     void displayDeadBlack();
  674.     void placeInDeadPlace(ChessPiece *piece);
  675.     void drawPawnMenu();
  676.  
  677.     //Scene Related
  678.     void addToScene(QGraphicsItem *item);
  679.     void removeFromScene(QGraphicsItem *item);
  680.  
  681.     //getters/setters
  682.     ChessPiece *pieceToMove;
  683.  
  684.     QString getTurn() ;
  685.     void setTurn(QString value);
  686.     void changeTurn();
  687.  
  688.  
  689.  
  690.     ChessBox *collection[8][8];
  691.     QGraphicsTextItem *check;
  692.     QList <ChessPiece *> alivePiece;
  693.     void displayMainMenu();
  694.  
  695.     void gameOver();
  696.     void removeAll();
  697. signals:
  698.     void GameOver();
  699.     void whiteWon();
  700.     void blackWon();
  701. public slots:
  702.     void start();
  703. private:
  704.     QGraphicsScene *gameScene;
  705.     QList <ChessPiece *> whiteDead;
  706.     QList <ChessPiece *> blackDead;
  707.     QGraphicsRectItem * deadHolder;
  708.     QGraphicsRectItem * pawnMenu;
  709.     QString turn;
  710.     ChessBoard *chess;
  711.     QList <QGraphicsItem *> listG;
  712.     QGraphicsTextItem * turnDisplay;
  713.     QGraphicsTextItem * choosePawnText;
  714.  
  715. };
  716.  
  717. #endif // GAME_H
  718. #include "game.h"
  719. #include <QPixmap>
  720. #include "king.h"
  721. #include "button.h"
  722. #include <QDebug>
  723.  
  724. Game::Game(QWidget *parent ):QGraphicsView(parent)
  725. {
  726.     //Making the Scene
  727.     gameScene = new QGraphicsScene();
  728.     gameScene->setSceneRect(0,0,1400,900);
  729.  
  730.     //Making the view
  731.     setFixedSize(1400,900);
  732.     setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  733.     setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  734.     setScene(gameScene);
  735.     QBrush brush;
  736.     brush.setStyle(Qt::SolidPattern);
  737.     brush.setColor(QColor::fromRgb(qRgb(61, 61, 61)));
  738.     setBackgroundBrush(brush);
  739.     pieceToMove = NULL;
  740.  
  741.     //display turn
  742.     turnDisplay = new QGraphicsTextItem();
  743.     turnDisplay->setPos(width()/2-100,10);
  744.     turnDisplay->setZValue(1);
  745.     turnDisplay->setDefaultTextColor(QColor::fromRgb(qRgb(247, 247, 247)));
  746.     QFont fontTurn;
  747.     fontTurn.setFamily(QString::fromUtf8("Courier"));
  748.     fontTurn.setPointSize(18);
  749.     fontTurn.setBold(true);
  750.     fontTurn.setKerning(true);
  751.     turnDisplay->setFont(fontTurn);
  752.     turnDisplay->setPlainText("Turn : WHITE");
  753.  
  754.     //display Check
  755.     check = new QGraphicsTextItem();
  756.     check->setPos(width()/2-100,860);
  757.     check->setZValue(4);
  758.     check->setDefaultTextColor(Qt::red);
  759.     QFont fontCheck;
  760.     fontCheck.setFamily(QString::fromUtf8("Courier"));
  761.     fontCheck.setPointSize(18);
  762.     fontCheck.setBold(true);
  763.     fontCheck.setKerning(true);
  764.     check->setFont(fontCheck);
  765.     check->setPlainText("CHECK!");
  766.     check->setVisible(false);
  767.     setTurn("WHITE");
  768.  
  769. }
  770.  
  771. void Game::drawChessBoard()
  772. {
  773.     chess = new ChessBoard();
  774.     drawDeadHolder(0,0,QColor::fromRgb(qRgb(247, 247, 247)));
  775.     drawDeadHolder(1100,0,QColor::fromRgb(qRgb(247, 247, 247)));
  776.     chess->drawBoxes(width()/2-400,50);
  777. }
  778.  
  779. void Game::displayDeadWhite()
  780. {
  781.     int SHIFT = 50;
  782.     int j = 0;
  783.     int k = 0;
  784.     for(size_t i = 0,n = whiteDead.size(); i<n; i++) {
  785.             if(j == 4){
  786.                 k++;
  787.                 j = 0;
  788.             }
  789.             whiteDead[i]->setPos(40+SHIFT*j++,100+SHIFT*2*k);
  790.     }
  791. }
  792.  
  793. void Game::displayDeadBlack()
  794. {
  795.     int SHIFT = 50;
  796.     int j = 0;
  797.     int k = 0;
  798.     for(size_t i = 0,n = blackDead.size(); i<n; i++) {
  799.         if(j == 4){
  800.             k++;
  801.             j = 0;
  802.         }
  803.         blackDead[i]->setPos(1140+SHIFT*j++,100+SHIFT*2*k);
  804.     }
  805. }
  806.  
  807. void Game::placeInDeadPlace(ChessPiece *piece)
  808. {
  809.     if(piece->getSide() == "WHITE") {
  810.         whiteDead.append(piece);
  811.         King *g = dynamic_cast <King *>(piece);
  812.         if(g){
  813.             check->setPlainText("Black Won");
  814.             emit blackWon();
  815.             gameOver();
  816.         }
  817.         displayDeadWhite();
  818.     }
  819.     else{
  820.         blackDead.append(piece);
  821.         King *g = dynamic_cast <King *>(piece);
  822.         if(g){
  823.             check->setPlainText("White Won");
  824.             emit whiteWon();
  825.             gameOver();
  826.         }
  827.         displayDeadBlack();
  828.     }
  829.     alivePiece.removeAll(piece);
  830. }
  831.  
  832. void Game::addToScene(QGraphicsItem *item)
  833. {
  834.     gameScene->addItem(item);
  835. }
  836.  
  837. void Game::removeFromScene(QGraphicsItem *item)
  838. {
  839.     gameScene->removeItem(item);
  840.  
  841. }
  842.  
  843. QString Game::getTurn()
  844. {
  845.     return turn;
  846. }
  847.  
  848. void Game::setTurn(QString value)
  849. {
  850.     turn = value;
  851. }
  852.  
  853. void Game::changeTurn()
  854. {
  855.     if(getTurn() == "WHITE")
  856.         setTurn("BLACK");
  857.     else
  858.         setTurn("WHITE");
  859.     turnDisplay->setPlainText("Turn : " + getTurn());
  860. }
  861.  
  862. void Game::start()
  863. {
  864.     drawChessBoard();
  865.  
  866.     for(size_t i =0, n = listG.size(); i < n; i++)
  867.         removeFromScene(listG[i]);
  868.  
  869.     addToScene(turnDisplay);
  870.     QGraphicsTextItem* whitePiece = new QGraphicsTextItem();
  871.     whitePiece->setPos(70,10);
  872.     whitePiece->setZValue(1);
  873.     whitePiece->setDefaultTextColor(QColor::fromRgb(qRgb(61, 61, 61)));
  874.  
  875.     QFont fontWhitePiece;
  876.     fontWhitePiece.setFamily(QString::fromUtf8("Courier"));
  877.     fontWhitePiece.setPointSize(20);
  878.     fontWhitePiece.setBold(true);
  879.     fontWhitePiece.setKerning(true);
  880.     whitePiece->setFont(fontWhitePiece);
  881.     whitePiece->setPlainText("WHITE PIECE");
  882.     addToScene(whitePiece);
  883.  
  884.     QGraphicsTextItem *blackPiece = new QGraphicsTextItem();
  885.     blackPiece->setPos(1170,10);
  886.     blackPiece->setZValue(1);
  887.     blackPiece->setDefaultTextColor(QColor::fromRgb(qRgb(61, 61, 61)));
  888.  
  889.     QFont fontBlackPiece;
  890.     fontBlackPiece.setFamily(QString::fromUtf8("Courier"));
  891.     fontBlackPiece.setPointSize(20);
  892.     fontBlackPiece.setBold(true);
  893.     fontBlackPiece.setKerning(true);
  894.     blackPiece->setFont(fontBlackPiece);
  895.     blackPiece->setPlainText("BLACK PIECE");
  896.     addToScene(blackPiece);
  897.  
  898.     addToScene(check);
  899.     chess->addChessPiece();
  900.     //drawPawnMenu();
  901. }
  902.  
  903. void Game::drawDeadHolder(int x, int y,QColor color)
  904. {
  905.     deadHolder = new QGraphicsRectItem(x,y,300,900);
  906.     QBrush brush;
  907.     brush.setStyle(Qt::SolidPattern);
  908.     brush.setColor(color);
  909.     deadHolder->setBrush(brush);
  910.     addToScene(deadHolder);
  911. }
  912.  
  913. void Game::drawPawnMenu()
  914. {
  915.     pawnMenu = new QGraphicsRectItem(300,400,800,200);
  916.     QBrush brush;
  917.     brush.setStyle(Qt::SolidPattern);
  918.     brush.setColor(QColor::fromRgb(qRgb(247, 247, 247)));
  919.     pawnMenu->setBrush(brush);
  920.     addToScene(pawnMenu);
  921.     choosePawnText = new QGraphicsTextItem();
  922.     turnDisplay->setPos(450,420);
  923.     turnDisplay->setZValue(1);
  924.     turnDisplay->setDefaultTextColor(QColor::fromRgb(qRgb(61, 61, 61)));
  925.     QFont font;
  926.     font.setFamily(QString::fromUtf8("Courier"));
  927.     font.setPointSize(18);
  928.     font.setBold(true);
  929.     font.setKerning(true);
  930.     turnDisplay->setFont(font);
  931.     turnDisplay->setPlainText("Choose a piece instead of a pawn");
  932.  
  933.     Button * bishopButton = new Button("bishop");
  934.     Button * knightButton = new Button("knight");
  935.     Button * queenButton = new Button("queen");
  936.     Button * rookButton = new Button("rook");
  937.     int pxPos = 350;
  938.     int pyPos = 500;
  939.     bishopButton->setPos(pxPos,pyPos);
  940.     knightButton->setPos(pxPos+175,pyPos);
  941.     queenButton->setPos(pxPos+350,pyPos);
  942.     rookButton->setPos(pxPos+525,pyPos);
  943.     font.setPointSize(12);
  944.     bishopButton->setFont(font);
  945.     knightButton->setFont(font);
  946.     queenButton->setFont(font);
  947.     rookButton->setFont(font);
  948.     connect(bishopButton,SIGNAL(clicked()) , this , SLOT(start()));
  949.  
  950.     addToScene(bishopButton);
  951.     addToScene(knightButton);
  952.     addToScene(queenButton);
  953.     addToScene(rookButton);
  954.  
  955.     listG.append(bishopButton);
  956.     listG.append(knightButton);
  957.     listG.append(queenButton);
  958.     listG.append(rookButton);
  959.  
  960. }
  961.  
  962. void Game::gameOver()
  963. {
  964. //    removeAll();
  965. //    setTurn("WHITE");
  966. //    alivePiece.clear();
  967. //    chess->reset();
  968.     this->close();
  969.     emit GameOver();
  970. }
  971.  
  972. void Game::removeAll(){
  973.     QList<QGraphicsItem*> itemsList = gameScene->items();
  974.     for(size_t i = 0, n = itemsList.size();i<n;i++) {
  975.         if(itemsList[i]!=check)
  976.           removeFromScene(itemsList[i]);
  977.     }
  978. }
  979. #ifndef KING_H
  980. #define KING_H
  981.  
  982. #include "chesspiece.h"
  983. class King:public ChessPiece
  984. {
  985. public:
  986.     King(QString team,QGraphicsItem *parent = 0);
  987.     void setImage();
  988.     void findUnSafeLocation();
  989.     void moves();
  990.  
  991. };
  992.  
  993. #endif // KING_H
  994. #include "king.h"
  995. #include "game.h"
  996. #include "pawn.h"
  997. extern Game *game;
  998.  
  999. King::King(QString team,QGraphicsItem *parent):ChessPiece(team,parent)
  1000. {
  1001.     setImage();
  1002. }
  1003.  
  1004. void King::setImage()
  1005. {
  1006.     if(side == "WHITE")
  1007.         setPixmap(QPixmap("E:/kursovaya3sem/myProject/media/whiteking.png"));
  1008.     else
  1009.         setPixmap(QPixmap("E:/kursovaya3sem/myProject/media/blackking.png"));
  1010. }
  1011.  
  1012. void King::moves()
  1013. {
  1014.     location.clear();
  1015.     int row = this->getCurrentBox()->rowLoc;
  1016.     int col = this->getCurrentBox()->colLoc;
  1017.     QString team = this->getSide();
  1018.         if(col > 0 && row > 0 && !(game->collection[row-1][col-1]->getChessPieceColor() == team)) {//up left
  1019.             location.append(game->collection[row-1][col-1]);
  1020.             game->collection[row-1][col-1]->setColor(Qt::green);
  1021.             if(location.last()->getHasChessPiece()){
  1022.                 location.last()->setColor(Qt::yellow);
  1023.             }
  1024.         }
  1025.         if(col < 7 && row > 0 && !(game->collection[row-1][col+1]->getChessPieceColor() == team)) { // up right
  1026.             location.append(game->collection[row-1][col+1]);
  1027.             game->collection[row-1][col+1]->setColor(Qt::green);
  1028.             if(location.last()->getHasChessPiece()){
  1029.                 location.last()->setColor(Qt::yellow);
  1030.             }
  1031.         }
  1032.         if(row>0 && !(game->collection[row-1][col]->getChessPieceColor() == team)) {//up
  1033.             location.append(game->collection[row-1][col]);
  1034.             game->collection[row-1][col]->setColor(Qt::green);
  1035.             if(location.last()->getHasChessPiece()){
  1036.                 location.last()->setColor(Qt::yellow);
  1037.             }
  1038.         }
  1039.         if(row<7 && !(game->collection[row+1][col]->getChessPieceColor() == team)) {//down
  1040.             location.append(game->collection[row+1][col]);
  1041.             game->collection[row+1][col]->setColor(Qt::green);
  1042.             if(location.last()->getHasChessPiece()){
  1043.                 location.last()->setColor(Qt::yellow);
  1044.             }
  1045.         }
  1046.         if(col>0 && !(game->collection[row][col-1]->getChessPieceColor() == team)) {// left
  1047.             location.append(game->collection[row][col-1]);
  1048.             game->collection[row][col-1]->setColor(Qt::green);
  1049.             if(location.last()->getHasChessPiece()){
  1050.                 location.last()->setColor(Qt::yellow);
  1051.             }
  1052.         }
  1053.         if(col<7 && !(game->collection[row][col+1]->getChessPieceColor() == team)) {//right
  1054.             location.append(game->collection[row][col+1]);
  1055.             game->collection[row][col+1]->setColor(Qt::green);
  1056.             if(location.last()->getHasChessPiece()){
  1057.                 location.last()->setColor(Qt::yellow);
  1058.             }
  1059.         }
  1060.         if(col > 0 && row < 7  && !(game->collection[row+1][col-1]->getChessPieceColor() == team)) {//down left
  1061.             location.append(game->collection[row+1][col-1]);
  1062.             game->collection[row+1][col-1]->setColor(Qt::green);
  1063.             if(location.last()->getHasChessPiece()){
  1064.                 location.last()->setColor(Qt::yellow);
  1065.             }
  1066.         }
  1067.         if(col < 7 && row < 7 && !(game->collection[row+1][col+1]->getChessPieceColor() == team)) {//down right
  1068.             location.append(game->collection[row+1][col+1]);
  1069.             game->collection[row+1][col+1]->setColor(Qt::green);
  1070.             if(location.last()->getHasChessPiece()){
  1071.                 location.last()->setColor(Qt::yellow);
  1072.             }
  1073.         }
  1074.     //findUnSafeLocation();
  1075. }
  1076.  
  1077. void King::findUnSafeLocation() {
  1078.     QList <ChessPiece *> pList = game->alivePiece;
  1079.     for(size_t i = 0,n = pList.size(); i < n; i++) {
  1080.         if(pList[i]->getSide() != this->getSide())
  1081.         {
  1082.             QList <ChessBox *> bList = pList[i]->moveLocation();
  1083.             for(size_t j = 0, n = bList.size(); j < n; j++) {
  1084.                 Pawn *c = dynamic_cast<Pawn *> (pList[i]) ;
  1085.                 if(c)
  1086.                     continue;
  1087.                 for(size_t k = 0, n = location.size(); k < n; k++) {
  1088.                    if(bList[j] == location [k]) {
  1089.                        location[k]->setColor(Qt::blue);
  1090.                    }
  1091.                 }
  1092.             }
  1093.         }
  1094.     }
  1095.  
  1096. }
  1097. #ifndef KNIGHT_H
  1098. #define KNIGHT_H
  1099.  
  1100. #include "chesspiece.h"
  1101. class Knight:public ChessPiece
  1102. {
  1103. public:
  1104.     Knight(QString team,QGraphicsItem *parent = 0);
  1105.     void setImage();
  1106.  
  1107.     void moves();
  1108.  
  1109. };
  1110.  
  1111. #endif // KNIGHT_H
  1112. #include "knight.h"
  1113. #include "game.h"
  1114. extern Game *game;
  1115.  
  1116. Knight::Knight(QString team,QGraphicsItem *parent):ChessPiece(team,parent)
  1117. {
  1118.     setImage();
  1119. }
  1120.  
  1121. void Knight::setImage()
  1122. {
  1123.     if(side == "WHITE")
  1124.         setPixmap(QPixmap("E:/kursovaya3sem/myProject/media/whiteknight.png"));
  1125.     else
  1126.         setPixmap(QPixmap("E:/kursovaya3sem/myProject/media/blackknight.png"));
  1127. }
  1128.  
  1129.  
  1130.  
  1131. void Knight::moves()
  1132. {
  1133.     int row = this->getCurrentBox()->rowLoc;
  1134.     int col = this->getCurrentBox()->colLoc;
  1135.     int i ,j;
  1136.     QString team  = this->getSide();
  1137.  
  1138.     //There are total 8 places where a night can move
  1139.  
  1140.     //1st up up left
  1141.     i = row - 2;
  1142.     j = col - 1;
  1143.     if(i >=0 && j>=0 && (game->collection[i][j]->getChessPieceColor() != team) ) {
  1144.         location.append(game->collection[i][j]);
  1145.         if(location.last()->getHasChessPiece())
  1146.             location.last()->setColor(Qt::yellow);
  1147.         else
  1148.             location.last()->setColor(Qt::green);
  1149.     }
  1150.  
  1151.     //2nd up up right
  1152.     i = row - 2;
  1153.     j = col + 1;
  1154.     if(i >=0 && j<=7 && (game->collection[i][j]->getChessPieceColor() != team) ) {
  1155.         location.append(game->collection[i][j]);
  1156.         if(location.last()->getHasChessPiece())
  1157.             location.last()->setColor(Qt::yellow);
  1158.         else
  1159.             location.last()->setColor(Qt::green);
  1160.     }
  1161.  
  1162.     //3rd down down left
  1163.     i = row + 2;
  1164.     j = col - 1;
  1165.     if(i <= 7 && j>=0 && (game->collection[i][j]->getChessPieceColor() != team) ) {
  1166.         location.append(game->collection[i][j]);
  1167.         if(location.last()->getHasChessPiece())
  1168.             location.last()->setColor(Qt::yellow);
  1169.         else
  1170.             location.last()->setColor(Qt::green);
  1171.     }
  1172.  
  1173.     //4th  down down right
  1174.     i = row + 2;
  1175.     j = col + 1;
  1176.     if(i <=7 && j<=7 && (game->collection[i][j]->getChessPieceColor() != team) ) {
  1177.         location.append(game->collection[i][j]);
  1178.         if(location.last()->getHasChessPiece())
  1179.             location.last()->setColor(Qt::yellow);
  1180.         else
  1181.             location.last()->setColor(Qt::green);
  1182.     }
  1183.  
  1184.     //5th left left up
  1185.     i = row - 1;
  1186.     j = col - 2;
  1187.     if(i >=0 && j>=0 && (game->collection[i][j]->getChessPieceColor() != team) ) {
  1188.         location.append(game->collection[i][j]);
  1189.         if(location.last()->getHasChessPiece())
  1190.             location.last()->setColor(Qt::yellow);
  1191.         else
  1192.             location.last()->setColor(Qt::green);
  1193.     }
  1194.  
  1195.     //6th left left down
  1196.     i = row + 1;
  1197.     j = col - 2;
  1198.     if(i <=7 && j>=0 && (game->collection[i][j]->getChessPieceColor() != team) ) {
  1199.         location.append(game->collection[i][j]);
  1200.         if(location.last()->getHasChessPiece())
  1201.             location.last()->setColor(Qt::yellow);
  1202.         else
  1203.             location.last()->setColor(Qt::green);
  1204.     }
  1205.  
  1206.     //7th right right up
  1207.     i = row - 1;
  1208.     j = col + 2;
  1209.     if(i >=0 && j<=7 && (game->collection[i][j]->getChessPieceColor() != team) ) {
  1210.         location.append(game->collection[i][j]);
  1211.         if(location.last()->getHasChessPiece())
  1212.             location.last()->setColor(Qt::yellow);
  1213.         else
  1214.             location.last()->setColor(Qt::green);
  1215.     }
  1216.  
  1217.     //8th right right down
  1218.     i = row + 1;
  1219.     j = col +  2;
  1220.     if(i <=7 && j<=7 && (game->collection[i][j]->getChessPieceColor() != team) ) {
  1221.         location.append(game->collection[i][j]);
  1222.         if(location.last()->getHasChessPiece())
  1223.             location.last()->setColor(Qt::yellow);
  1224.         else
  1225.             location.last()->setColor(Qt::green);
  1226.     }
  1227.  
  1228.  
  1229.  
  1230. }
  1231.  
  1232. #ifndef MAINWINDOW_H
  1233. #define MAINWINDOW_H
  1234.  
  1235. #include <QMainWindow>
  1236.  
  1237. QT_BEGIN_NAMESPACE
  1238. namespace Ui { class MainWindow; }
  1239. QT_END_NAMESPACE
  1240.  
  1241. class MainWindow : public QMainWindow
  1242. {
  1243.     Q_OBJECT
  1244.  
  1245. public:
  1246.     MainWindow(QWidget *parent = nullptr);
  1247.     ~MainWindow();
  1248.  
  1249. private slots:
  1250.  
  1251.     void on_buttonEXIT_clicked();
  1252.     void on_buttonPVP_clicked();
  1253.  
  1254. public slots:
  1255.     void whiteWON();
  1256.     void blackWON();
  1257.  
  1258. private:
  1259.     Ui::MainWindow *ui;
  1260. };
  1261. #endif // MAINWINDOW_H
  1262. #include "mainwindow.h"
  1263. #include "ui_mainwindow.h"
  1264. #include "game.h"
  1265. Game *game;
  1266.  
  1267. MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
  1268. {
  1269.     ui->setupUi(this);
  1270. }
  1271.  
  1272. MainWindow::~MainWindow()
  1273. {
  1274.     delete ui;
  1275. }
  1276.  
  1277. void MainWindow::on_buttonEXIT_clicked()
  1278. {
  1279.     this->close();
  1280. }
  1281.  
  1282. void MainWindow::whiteWON()
  1283. {
  1284.     this->setStyleSheet(QString::fromUtf8("background-image:url(E:/kursovaya3sem/myProject/media/whitewon.png); background-position: center; "));
  1285.  
  1286. }
  1287.  
  1288. void MainWindow::blackWON()
  1289. {
  1290.     this->setStyleSheet(QString::fromUtf8("background-image:url(E:/kursovaya3sem/myProject/media/blackwon.png); background-position: center; "));
  1291.  
  1292. }
  1293.  
  1294. void MainWindow::on_buttonPVP_clicked()
  1295. {
  1296.     if(game != nullptr){
  1297.         game->close();
  1298.     }
  1299.     this->ui->buttonPVP->hide();
  1300.     this->hide();
  1301.     game = new Game();
  1302.     game->show();
  1303.     game->start();
  1304.     QObject::connect(game, SIGNAL(GameOver()), this, SLOT(show()));
  1305.     QObject::connect(game, SIGNAL(whiteWon()), this, SLOT(whiteWON()));
  1306.     QObject::connect(game, SIGNAL(blackWon()), this, SLOT(blackWON()));
  1307. }
  1308. #ifndef PAWN_H
  1309. #define PAWN_H
  1310.  
  1311. #include "chesspiece.h"
  1312. class Pawn:public ChessPiece
  1313. {
  1314. public:
  1315.     Pawn(QString team,QGraphicsItem *parent = 0);
  1316.     void setImage();
  1317.     void moves();
  1318.  
  1319.  private:
  1320.  
  1321.  
  1322. };
  1323.  
  1324. #endif // PAWN_H
  1325. #include "pawn.h"
  1326. #include "game.h"
  1327. #include <QDebug>
  1328. #include <typeinfo>
  1329. #include "king.h"
  1330. extern Game * game;
  1331. Pawn::Pawn(QString team,QGraphicsItem *parent):ChessPiece(team,parent)
  1332. {
  1333.     setImage();
  1334.     firstMove = true;
  1335. }
  1336.  
  1337. void Pawn::setImage()
  1338. {
  1339.     if(side == "WHITE")
  1340.         setPixmap(QPixmap("E:/kursovaya3sem/myProject/media/whitepawn.png"));
  1341.     else
  1342.         setPixmap(QPixmap("E:/kursovaya3sem/myProject/media/blackpawn.png"));
  1343. }
  1344.  
  1345. void Pawn::moves()
  1346. {
  1347.     location.clear();
  1348.  
  1349.     int row = this->getCurrentBox()->rowLoc;
  1350.  
  1351.     int col = this->getCurrentBox()->colLoc;
  1352.     if(this->getSide() == "WHITE")  {
  1353.         if(col > 0 && row > 0 && game->collection[row-1][col-1]->getChessPieceColor() == "BLACK") {
  1354.             location.append(game->collection[row-1][col-1]);
  1355.             boxSetting(location.last());
  1356.         }
  1357.         if(col < 7 && row > 0 && game->collection[row-1][col+1]->getChessPieceColor() == "BLACK") {
  1358.             location.append(game->collection[row-1][col+1]);
  1359.             boxSetting(location.last());
  1360.         }
  1361.         if(row>0 && (!game->collection[row-1][col]->getHasChessPiece())) {
  1362.             location.append(game->collection[row-1][col]);
  1363.             boxSetting(location.last());
  1364.             if(firstMove && !game->collection[row-2][col]->getHasChessPiece()){
  1365.                 location.append(game->collection[row-2][col]);
  1366.                 boxSetting(location.last());
  1367.             }
  1368.         }
  1369.  
  1370.     }
  1371.     else{
  1372.         if(col > 0 && row < 7 && game->collection[row+1][col-1]->getChessPieceColor() == "WHITE") {//left
  1373.             location.append(game->collection[row+1][col-1]);
  1374.             boxSetting(location.last());
  1375.         }
  1376.         if(col < 7 && row <  7 && game->collection[row+1][col+1]->getChessPieceColor() == "WHITE") {//right
  1377.             location.append(game->collection[row+1][col+1]);
  1378.             boxSetting(location.last());
  1379.         }
  1380.         if(row<7 && (!game->collection[row+1][col]->getHasChessPiece())) {
  1381.             location.append(game->collection[row+1][col]);
  1382.             boxSetting(location.last());
  1383.             if(firstMove && !game->collection[row+2][col]->getHasChessPiece()){
  1384.                 location.append(game->collection[row+2][col]);
  1385.                 boxSetting(location.last());
  1386.             }
  1387.  
  1388.         }
  1389.  
  1390.     }
  1391.  
  1392. }
  1393. #ifndef QUEEN_H
  1394. #define QUEEN_H
  1395.  
  1396. #include "chesspiece.h"
  1397. class Queen:public ChessPiece
  1398. {
  1399. public:
  1400.     Queen(QString team,QGraphicsItem *parent = 0);
  1401.     void setImage();
  1402.  
  1403.     void moves();
  1404.  
  1405. };
  1406.  
  1407. #endif // QUEEN_H
  1408. #include "queen.h"
  1409. #include "game.h"
  1410. extern Game *game;
  1411. Queen::Queen(QString team,QGraphicsItem *parent):ChessPiece(team,parent)
  1412. {
  1413.     setImage();
  1414. }
  1415.  
  1416. void Queen::setImage()
  1417. {
  1418.     if(side == "WHITE")
  1419.         setPixmap(QPixmap("E:/kursovaya3sem/myProject/media/whitequeen.png"));
  1420.     else
  1421.         setPixmap(QPixmap("E:/kursovaya3sem/myProject/media/blackqueen.png"));
  1422. }
  1423.  
  1424.  
  1425. void Queen::moves()
  1426. {
  1427.     location.clear();
  1428.     int row = this->getCurrentBox()->rowLoc;
  1429.     int col = this->getCurrentBox()->colLoc;
  1430.     QString team = this->getSide();
  1431.     //For up
  1432.  
  1433.      for(int i = row-1,j = col; i >= 0 ; i--) {
  1434.        if(game->collection[i][j]->getChessPieceColor() == team ) {
  1435.            break;
  1436.        }
  1437.        else
  1438.        {
  1439.            location.append(game->collection[i][j]);
  1440.            if(boxSetting(location.last()))
  1441.                break;
  1442.         }
  1443.     }
  1444.  
  1445.      //For Down
  1446.  
  1447.       for(int i = row+1,j = col; i <= 7 ; i++) {
  1448.         if(game->collection[i][j]->getChessPieceColor() == team ) {
  1449.             break;
  1450.         }
  1451.         else
  1452.         {
  1453.             location.append(game->collection[i][j]);
  1454.             if(boxSetting(location.last())){
  1455.                 break;
  1456.             }
  1457.         }
  1458.      }
  1459.  
  1460.       //For left
  1461.  
  1462.        for(int i = row,j = col-1; j >= 0 ; j--) {
  1463.          if(game->collection[i][j]->getChessPieceColor() == team ) {
  1464.              break;
  1465.          }
  1466.          else
  1467.          {
  1468.              location.append(game->collection[i][j]);
  1469.              if(boxSetting(location.last()))
  1470.                 break;
  1471.          }
  1472.         }
  1473.        //For Right
  1474.  
  1475.         for(int i = row,j = col+1; j <= 7 ; j++)
  1476.         {
  1477.               if(game->collection[i][j]->getChessPieceColor() == team ) {
  1478.                   break;
  1479.               }
  1480.               else
  1481.               {
  1482.                   location.append(game->collection[i][j]);
  1483.                   if(boxSetting(location.last()))
  1484.                       break;
  1485.                }
  1486.  
  1487.        }
  1488.  
  1489.         //For upper Left
  1490.  
  1491.          for(int i = row-1,j = col-1; i >= 0 && j >=0; i--,j--) {
  1492.            if(game->collection[i][j]->getChessPieceColor() == team ) {
  1493.                break;
  1494.  
  1495.            }
  1496.            else
  1497.            {
  1498.                location.append(game->collection[i][j]);
  1499.                if(boxSetting(location.last()) ){
  1500.                    break;
  1501.                }
  1502.            }
  1503.         }
  1504.  
  1505.          //For upper right
  1506.  
  1507.           for(int i = row-1,j = col+1; i >= 0 && j <= 7; i--,j++) {
  1508.             if(game->collection[i][j]->getChessPieceColor() == team ) {
  1509.                 break;
  1510.  
  1511.             }
  1512.             else
  1513.             {
  1514.                 location.append(game->collection[i][j]);
  1515.                 if(boxSetting(location.last())){
  1516.                     break;
  1517.                 }
  1518.             }
  1519.          }
  1520.  
  1521.           //For downward right
  1522.  
  1523.            for(int i = row+1,j = col+1; i <= 7 && j <= 7; i++,j++) {
  1524.              if(game->collection[i][j]->getChessPieceColor() == team ) {
  1525.                  break;
  1526.  
  1527.              }
  1528.              else
  1529.              {
  1530.                  location.append(game->collection[i][j]);
  1531.                  if(boxSetting(location.last())){
  1532.                      break;
  1533.                  }
  1534.              }
  1535.           }
  1536.  
  1537.            //For downward left
  1538.  
  1539.             for(int i = row+1,j = col-1; i <= 7 && j >= 0; i++,j--) {
  1540.               if(game->collection[i][j]->getChessPieceColor() == team ) {
  1541.                   break;
  1542.  
  1543.               }
  1544.               else
  1545.               {
  1546.                   location.append(game->collection[i][j]);
  1547.                   if(boxSetting(location.last())){
  1548.                       break;
  1549.                   }
  1550.  
  1551.               }
  1552.            }
  1553.  
  1554.  
  1555. }
  1556. #ifndef ROOK_H
  1557. #define ROOK_H
  1558.  
  1559. #include "chesspiece.h"
  1560. class Rook:public ChessPiece
  1561. {
  1562. public:
  1563.     Rook(QString team, QGraphicsItem *parent = 0);
  1564.  
  1565.     void setImage();
  1566.     void moves();
  1567.  
  1568. };
  1569.  
  1570. #endif // ROOK_H
  1571. #include "rook.h"
  1572. #include "game.h"
  1573.  
  1574. extern Game *game;
  1575. Rook::Rook(QString team,QGraphicsItem *parent):ChessPiece(team,parent)
  1576. {
  1577.     setImage();
  1578. }
  1579.  
  1580.  
  1581. void Rook::setImage()
  1582. {
  1583.     if(side == "WHITE")
  1584.         setPixmap(QPixmap("E:/kursovaya3sem/myProject/media/whiterook.png"));
  1585.     else
  1586.         setPixmap(QPixmap("E:/kursovaya3sem/myProject/media/blackrook.png"));
  1587. }
  1588.  
  1589. void Rook::moves()
  1590. {
  1591.     location.clear();
  1592.     int row = this->getCurrentBox()->rowLoc;
  1593.     int col = this->getCurrentBox()->colLoc;
  1594.     QString team = this->getSide();
  1595.     //For up
  1596.  
  1597.      for(int i = row-1,j = col; i >= 0 ; i--) {
  1598.        if(game->collection[i][j]->getChessPieceColor() == team ) {
  1599.            break;
  1600.        }
  1601.        else
  1602.        {
  1603.            location.append(game->collection[i][j]);
  1604.            if(boxSetting(location.last()))
  1605.                break;
  1606.         }
  1607.     }
  1608.  
  1609.      //For Down
  1610.  
  1611.       for(int i = row+1,j = col; i <= 7 ; i++) {
  1612.         if(game->collection[i][j]->getChessPieceColor() == team ) {
  1613.             break;
  1614.         }
  1615.         else
  1616.         {
  1617.             location.append(game->collection[i][j]);
  1618.             if(boxSetting(location.last())){
  1619.                 break;
  1620.             }
  1621.         }
  1622.      }
  1623.  
  1624.       //For left
  1625.  
  1626.        for(int i = row,j = col-1; j >= 0 ; j--) {
  1627.          if(game->collection[i][j]->getChessPieceColor() == team ) {
  1628.              break;
  1629.          }
  1630.          else
  1631.          {
  1632.              location.append(game->collection[i][j]);
  1633.              if(boxSetting(location.last()))
  1634.                 break;
  1635.          }
  1636.         }
  1637.        //For Right
  1638.  
  1639.         for(int i = row,j = col+1; j <= 7 ; j++)
  1640.         {
  1641.               if(game->collection[i][j]->getChessPieceColor() == team ) {
  1642.                   break;
  1643.               }
  1644.               else
  1645.               {
  1646.                   location.append(game->collection[i][j]);
  1647.                   if(boxSetting(location.last()))
  1648.                       break;
  1649.                }
  1650.  
  1651.        }
  1652.  
  1653.  
  1654. }
  1655.  
  1656.  

Editor

You can edit this paste and save as new:


File Description
  • код
  • Paste Code
  • 11 Dec-2023
  • 42.62 Kb
You can Share it: