Vla_DOS

Untitled

Aug 7th, 2023
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 24.53 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include <iostream>
  4. #include <vector>
  5. #include <Windows.h>
  6.  
  7.  
  8. namespace ChessApp {
  9.  
  10.     using namespace System;
  11.     using namespace System::ComponentModel;
  12.     using namespace System::Collections;
  13.     using namespace System::Collections::Generic;
  14.  
  15.     using namespace System::Windows::Forms;
  16.     using namespace System::Data;
  17.     using namespace System::Drawing;
  18.  
  19.     // Оголошення класу фігури
  20.     class ChessPiece    {
  21.     protected:
  22.  
  23.     public:
  24.         bool isBlack; // true for white pieces, false for black pieces
  25.         int capturedPositionX;
  26.         int capturedPositionY;
  27.         // Constructor to set the color of the piece
  28.         ChessPiece(bool isBlack) : isBlack(isBlack) {}
  29.  
  30.         virtual bool isValidMove(int fromX, int fromY, int toX, int toY, std::vector<std::vector<ChessPiece*>>& board) = 0;
  31.  
  32.         void Capture(int x, int y)
  33.         {
  34.             capturedPositionX = x;
  35.             capturedPositionY = y;
  36.         }
  37.     };
  38.  
  39.     // Оголошення класу фігури Канцлер
  40.     class Chancellor : public ChessPiece {
  41.     public:
  42.         Chancellor(bool isBlack) : ChessPiece(isBlack) {}
  43.  
  44.         bool isValidMove(int fromX, int fromY, int toX, int toY, std::vector<std::vector<ChessPiece*>>& board) {
  45.             // Chancellor moves like a combination of a Rook and a Knight
  46.             int dx = abs(fromX - toX);
  47.             int dy = abs(fromY - toY);
  48.  
  49.             // Check if the move is like a Knight (L-shaped)
  50.             if ((dx == 2 && dy == 1) || (dx == 1 && dy == 2)) {
  51.                 // A Knight-like move is valid, check if the destination cell is empty or has an opponent's piece
  52.                 ChessPiece* targetPiece = board[toX][toY];
  53.                 return targetPiece == nullptr || (targetPiece->isBlack != isBlack);
  54.             }
  55.  
  56.             // Check if the move is like a Rook (horizontally or vertically)
  57.             if ((dx == 0 && dy > 0) || (dx > 0 && dy == 0)) {
  58.                 // A Rook-like move is valid, check for obstacles along the path
  59.                 int stepX = (toX > fromX) ? 1 : (toX < fromX) ? -1 : 0;
  60.                 int stepY = (toY > fromY) ? 1 : (toY < fromY) ? -1 : 0;
  61.  
  62.                 int x = fromX + stepX;
  63.                 int y = fromY + stepY;
  64.  
  65.                 while (x != toX || y != toY) {
  66.                     if (board[x][y] != nullptr) {
  67.                         // There is an obstacle in the way, move is invalid
  68.                         return false;
  69.                     }
  70.                     x += stepX;
  71.                     y += stepY;
  72.                 }
  73.  
  74.                 // Check if the destination cell is empty or has an opponent's piece
  75.                 ChessPiece* targetPiece = board[toX][toY];
  76.                 return targetPiece == nullptr || (targetPiece->isBlack != isBlack);
  77.             }
  78.  
  79.             return false; // Invalid move for the Chancellor
  80.         }
  81.     };
  82.  
  83.     // Оголошення класу фігури Архієпископ
  84.     class Archbishop : public ChessPiece {
  85.     public:
  86.         Archbishop(bool isBlack) : ChessPiece(isBlack) {}
  87.  
  88.         bool isValidMove(int fromX, int fromY, int toX, int toY, std::vector<std::vector<ChessPiece*>>& board) {
  89.             // Archbishop moves like a combination of a Bishop and a Knight
  90.             int dx = abs(fromX - toX);
  91.             int dy = abs(fromY - toY);
  92.  
  93.             // Check if the move is like a Knight (L-shaped)
  94.             if ((dx == 2 && dy == 1) || (dx == 1 && dy == 2)) {
  95.                 // A Knight-like move is valid, check if the destination cell is empty or has an opponent's piece
  96.                 ChessPiece* targetPiece = board[toX][toY];
  97.                 return targetPiece == nullptr || (targetPiece->isBlack != isBlack);
  98.             }
  99.  
  100.             // Check if the move is like a Bishop (diagonally)
  101.             if (dx == dy) {
  102.                 // A Bishop-like move is valid, check for obstacles along the path
  103.                 int stepX = (toX > fromX) ? 1 : (toX < fromX) ? -1 : 0;
  104.                 int stepY = (toY > fromY) ? 1 : (toY < fromY) ? -1 : 0;
  105.  
  106.                 int x = fromX + stepX;
  107.                 int y = fromY + stepY;
  108.  
  109.                 while (x != toX || y != toY) {
  110.                     if (board[x][y] != nullptr) {
  111.                         // There is an obstacle in the way, move is invalid
  112.                         return false;
  113.                     }
  114.                     x += stepX;
  115.                     y += stepY;
  116.                 }
  117.  
  118.                 // Check if the destination cell is empty or has an opponent's piece
  119.                 ChessPiece* targetPiece = board[toX][toY];
  120.                 return targetPiece == nullptr || (targetPiece->isBlack != isBlack);
  121.             }
  122.  
  123.             return false; // Invalid move for the Archbishop
  124.         }
  125.     };
  126.  
  127.     // Оголошення класу фігури Слон
  128.     class Bishop : public ChessPiece {
  129.     public:
  130.         Bishop(bool isBlack) : ChessPiece(isBlack) {}
  131.  
  132.         bool isValidMove(int fromX, int fromY, int toX, int toY, std::vector<std::vector<ChessPiece*>>& board) {
  133.             // Bishop moves diagonally
  134.             int dx = abs(fromX - toX);
  135.             int dy = abs(fromY - toY);
  136.  
  137.             if (dx != dy) {
  138.                 return false; // Bishop can only move diagonally
  139.             }
  140.  
  141.             // Check if there are any pieces in the path
  142.             int xDir = (toX > fromX) ? 1 : -1;
  143.             int yDir = (toY > fromY) ? 1 : -1;
  144.  
  145.             int x = fromX + xDir;
  146.             int y = fromY + yDir;
  147.  
  148.             while (x != toX && y != toY) {
  149.                 if (x < 0 || x >= 10 || y < 0 || y >= 8) {
  150.                     return false; // Out of bounds
  151.                 }
  152.  
  153.                 if (board[x][y] != nullptr) {
  154.                     return false; // There is a piece in the path
  155.                 }
  156.                 x += xDir;
  157.                 y += yDir;
  158.             }
  159.  
  160.             // Check if the destination position is empty or has an opponent's piece
  161.             if (x < 0 || x >= 10 || y < 0 || y >= 8) {
  162.                 return false; // Out of bounds
  163.             }
  164.  
  165.             ChessPiece* destinationPiece = board[x][y];
  166.             if (destinationPiece != nullptr && destinationPiece->isBlack == isBlack) {
  167.                 return false; // Cannot capture own piece
  168.             }
  169.  
  170.             return true;
  171.         }
  172.  
  173.  
  174.     };
  175.  
  176.     // Оголошення класу фігури Король
  177.     class King : public ChessPiece {
  178.     public:
  179.         King(bool isBlack) : ChessPiece(isBlack) {}
  180.  
  181.         bool isValidMove(int fromX, int fromY, int toX, int toY, std::vector<std::vector<ChessPiece*>>& board) {
  182.             // King moves one square in any direction
  183.             int dx = abs(fromX - toX);
  184.             int dy = abs(fromY - toY);
  185.  
  186.             return (dx <= 1 && dy <= 1);
  187.         }
  188.     };
  189.  
  190.     // Оголошення класу фігури Королева
  191.     class Queen : public ChessPiece {
  192.     public:
  193.         Queen(bool isBlack) : ChessPiece(isBlack) {}
  194.  
  195.         bool isValidMove(int fromX, int fromY, int toX, int toY, std::vector<std::vector<ChessPiece*>>& board) {
  196.             // Queen moves like a combination of a Rook and a Bishop
  197.             int dx = abs(fromX - toX);
  198.             int dy = abs(fromY - toY);
  199.  
  200.             // Check if the move is along a straight line or diagonal
  201.             if (!((fromX == toX && fromY != toY) || (fromX != toX && fromY == toY) || (dx == dy))) {
  202.                 return false;
  203.             }
  204.  
  205.             // Check for obstacles along the path (straight or diagonal)
  206.             int stepX = (toX == fromX) ? 0 : (toX > fromX) ? 1 : -1;
  207.             int stepY = (toY == fromY) ? 0 : (toY > fromY) ? 1 : -1;
  208.  
  209.             int x = fromX + stepX;
  210.             int y = fromY + stepY;
  211.  
  212.             while (x != toX || y != toY) {
  213.                 if (board[x][y] != nullptr) {
  214.                     // There is an obstacle in the way, move is invalid
  215.                     return false;
  216.                 }
  217.                 x += stepX;
  218.                 y += stepY;
  219.             }
  220.  
  221.             return true;
  222.         }
  223.     };
  224.  
  225.     // Оголошення класу фігури Ладья (Rook)
  226.     class Rook : public ChessPiece {
  227.     public:
  228.         Rook(bool isBlack) : ChessPiece(isBlack) {}
  229.  
  230.         bool isValidMove(int fromX, int fromY, int toX, int toY, std::vector<std::vector<ChessPiece*>>& board) override {
  231.             // Rook moves along a straight line (horizontal or vertical)
  232.             int dx = abs(fromX - toX);
  233.             int dy = abs(fromY - toY);
  234.  
  235.             // Check if the move is along a straight line
  236.             if (!((fromX == toX && fromY != toY) || (fromX != toX && fromY == toY))) {
  237.                 return false;
  238.             }
  239.  
  240.             // Check for obstacles along the path
  241.             int stepX = (toX == fromX) ? 0 : (toX > fromX) ? 1 : -1;
  242.             int stepY = (toY == fromY) ? 0 : (toY > fromY) ? 1 : -1;
  243.  
  244.             int x = fromX + stepX;
  245.             int y = fromY + stepY;
  246.  
  247.             while (x != toX || y != toY) {
  248.                 if (board[x][y] != nullptr) {
  249.                     // There is an obstacle in the way, move is invalid
  250.                     return false;
  251.                 }
  252.                 x += stepX;
  253.                 y += stepY;
  254.             }
  255.  
  256.             return true;
  257.         }
  258.     };
  259.  
  260.     // Оголошення класу фігури Конь (Knight)
  261.     class Knight : public ChessPiece {
  262.     public:
  263.         Knight(bool isBlack) : ChessPiece(isBlack) {}
  264.  
  265.         bool isValidMove(int fromX, int fromY, int toX, int toY, std::vector<std::vector<ChessPiece*>>& board) override {
  266.             // Логіка перевірки ходу Коня (Knight)
  267.             // Реалізуйте правила руху для фігури Коня (Knight)
  268.  
  269.             // Рух Коня може бути здійснений у вигляді букви "L" (дві клітинки по одній осі і одна по іншій)
  270.             int dx = abs(fromX - toX);
  271.             int dy = abs(fromY - toY);
  272.             return (dx == 2 && dy == 1) || (dx == 1 && dy == 2);
  273.         }
  274.     };
  275.  
  276.     // Оголошення класу фігури Пішак
  277.     class Pawn : public ChessPiece {
  278.     public:
  279.         Pawn(bool isBlack) : ChessPiece(isBlack) {}
  280.  
  281.         bool isValidMove(int fromX, int fromY, int toX, int toY, std::vector<std::vector<ChessPiece*>>& board) {
  282.             int dx = abs(fromX - toX);
  283.             int dy = abs(fromY - toY);
  284.  
  285.             if (board[fromX][fromY] == nullptr) {
  286.                 return false; // No piece to move
  287.             }
  288.  
  289.             if (isBlack) {
  290.                 if (fromX == toX && fromY == toY - 1 && board[toX][toY] == nullptr) {
  291.                     return true; // Move one square forward
  292.                 }
  293.                 if (fromX == toX - 1 && fromY == toY - 1 && board[toX][toY] != nullptr && !board[toX][toY]->isBlack) {
  294.                     return true; // Capture diagonally to the left
  295.                 }
  296.                 if (fromX == toX + 1 && fromY == toY - 1 && board[toX][toY] != nullptr && !board[toX][toY]->isBlack) {
  297.                     return true; // Capture diagonally to the right
  298.                 }
  299.                 if (fromY == 1 && toY == 3 && fromX == toX && board[toX][toY] == nullptr && board[toX][toY - 1] == nullptr) {
  300.                     return true; // Move two squares forward on the first move
  301.                 }
  302.             }
  303.             else {
  304.                 if (fromX == toX && fromY == toY + 1 && board[toX][toY] == nullptr) {
  305.                     return true; // Move one square forward
  306.                 }
  307.                 if (fromX == toX - 1 && fromY == toY + 1 && board[toX][toY] != nullptr && board[toX][toY]->isBlack) {
  308.                     return true; // Capture diagonally to the left
  309.                 }
  310.                 if (fromX == toX + 1 && fromY == toY + 1 && board[toX][toY] != nullptr && board[toX][toY]->isBlack) {
  311.                     return true; // Capture diagonally to the right
  312.                 }
  313.                 if (fromY == 6 && toY == 4 && fromX == toX && board[toX][toY] == nullptr && board[toX][toY + 1] == nullptr) {
  314.                     return true; // Move two squares forward on the first move
  315.                 }
  316.             }
  317.  
  318.             return false;
  319.         }
  320.  
  321.     };
  322.  
  323.     // Оголошення класу дошки
  324.     class ChessBoard {
  325.     private:
  326.         std::vector<std::vector<ChessPiece*>> board;
  327.  
  328.     public:
  329.         ChessBoard() : board(10, std::vector<ChessPiece*>(8, nullptr)) {
  330.             // Початкове розташування фігур на дошці
  331.  
  332.             board[0][0] = new Rook(true); // Ладья
  333.             board[1][0] = new Knight(true); // Конь
  334.             board[2][0] = new Archbishop(true); // Архієпископ
  335.             board[3][0] = new Bishop(true); // Слон
  336.             board[4][0] = new Queen(true); // Королева
  337.             board[5][0] = new King(true); // Король
  338.             board[6][0] = new Bishop(true); // Слон
  339.             board[7][0] = new Chancellor(true); // Канцлер
  340.             board[8][0] = new Knight(true); // Конь
  341.             board[9][0] = new Rook(true); // Ладья
  342.  
  343.             // Вторая линия фигур (белые пешки)
  344.             for (int y = 0; y < 10; y++) {
  345.                 board[y][1] = new Pawn(true); // Пішаки (другая лінія)
  346.             }
  347.  
  348.             // Седьмая линия фигур (черные пешки)
  349.             for (int y = 0; y < 10; y++) {
  350.                 board[y][6] = new Pawn(false); // Пішаки (сьома лінія)
  351.             }
  352.  
  353.             // Восьмая линия фигур (черные фигуры)
  354.             board[0][7] = new Rook(false); // Ладья
  355.             board[1][7] = new Knight(false); // Конь
  356.             board[2][7] = new Archbishop(false); // Архієпископ
  357.             board[3][7] = new Bishop(false); // Слон
  358.             board[4][7] = new Queen(false); // Королева
  359.             board[5][7] = new King(false); // Король
  360.             board[6][7] = new Bishop(false); // Слон
  361.             board[7][7] = new Chancellor(false); // Канцлер
  362.             board[8][7] = new Knight(false); // Конь
  363.             board[9][7] = new Rook(false); // Ладья
  364.        
  365.         }
  366.  
  367.         ~ChessBoard() {
  368.             // Звільняємо пам'ять від фігур
  369.             for (int i = 0; i < 8; i++) {
  370.                 for (int j = 0; j < 8; j++) {
  371.                     delete board[i][j];
  372.                 }
  373.             }
  374.         }
  375.         ChessPiece* getPiece(int x, int y) {
  376.             // Метод для отримання фігури на позиції (x, y) на дошці
  377.             // Повертає nullptr, якщо клітинка порожня
  378.             if (x >= 0 && x < 10 && y >= 0 && y < 8) {
  379.                 return board[x][y];
  380.             }
  381.             return nullptr;
  382.         }
  383.  
  384.         bool makeMove(int fromX, int fromY, int toX, int toY) {
  385.             // Метод для здійснення ходу
  386.             if (!isValidMove(fromX, fromY, toX, toY)) {
  387.                 return false;
  388.             }
  389.  
  390.             ChessPiece* piece = board[fromX][fromY];
  391.             board[fromX][fromY] = nullptr;
  392.             board[toX][toY] = piece;
  393.             return true;
  394.         }
  395.  
  396.         bool isValidMove(int fromX, int fromY, int toX, int toY) {
  397.             // Метод для перевірки чи дозволений хід фігури з позиції (fromX, fromY) на позицію (toX, toY)
  398.             // Включає логіку перевірки правил руху для конкретної фігури
  399.  
  400.             // Перевірка чи позиції відповідають допустимому діапазону
  401.             if (fromX < 0 || fromX >= 10 || fromY < 0 || fromY >= 8 ||
  402.                 toX < 0 || toX >= 10 || toY < 0 || toY >= 8) {
  403.                 return false;
  404.             }
  405.  
  406.             // Перевірка чи на позиції (fromX, fromY) є фігура
  407.             ChessPiece* piece = board[fromX][fromY];
  408.             if (piece == nullptr) {
  409.                 return false;
  410.             }
  411.  
  412.             // Перевірка чи хід є допустимим для даної фігури
  413.             if (!piece->isValidMove(fromX, fromY, toX, toY, board)) {
  414.                 return false;
  415.             }
  416.             // Перевірка, чи на позиції (toX, toY) є фігура того ж кольору
  417.             ChessPiece* targetPiece = board[toX][toY];
  418.             if (targetPiece != nullptr && targetPiece->isBlack == piece->isBlack) {
  419.                 return false;
  420.             }
  421.             // Тут можна додати інші перевірки, такі як перевірка на шах, коректний хід пішака,
  422.             // правила рокіровки, взяття фігур і т.д.
  423.  
  424.             return true;
  425.         }
  426.         bool movePiece(int fromX, int fromY, int toX, int toY) {
  427.             // Здійснюємо хід фігурою
  428.             if (fromX < 0 || fromX >= 8 || fromY < 0 || fromY >= 8 || toX < 0 || toX >= 8 || toY < 0 || toY >= 8) {
  429.                 return false; // За межами дошки
  430.             }
  431.  
  432.             ChessPiece* piece = board[fromX][fromY];
  433.  
  434.             if (piece == nullptr) {
  435.                 return false; // Немає фігури для переміщення
  436.             }
  437.  
  438.             if (piece->isValidMove(fromX, fromY, toX, toY, board)) {
  439.                 delete board[toX][toY];
  440.                 board[toX][toY] = piece;
  441.                 board[fromX][fromY] = nullptr;
  442.                 return true;
  443.             }
  444.  
  445.             return false;
  446.         }
  447.     };
  448.  
  449.  
  450.     public ref class MyForm : public System::Windows::Forms::Form
  451.     {
  452.     public:
  453.  
  454.         MyForm(void)
  455.         {
  456.             InitializeComponent();
  457.             //
  458.             //TODO: Add the constructor code here
  459.             //
  460.  
  461.             // Ініціалізація гри
  462.             board = new ChessBoard();
  463.  
  464.             // Ініціалізація capturedPieces
  465.             capturedPieces = gcnew array<PictureBox^>(32);
  466.  
  467.             // Ініціалізація panelCaptured
  468.             panelCaptured = gcnew Panel();
  469.             panelCaptured->Location = System::Drawing::Point(10, 500); // Змініть координати на відповідні
  470.             panelCaptured->Size = System::Drawing::Size(300, 100);    // Змініть розмір на відповідний
  471.             panelCaptured->BorderStyle = BorderStyle::FixedSingle;
  472.             this->Controls->Add(panelCaptured);
  473.  
  474.             InitializeChessboard();
  475.  
  476.             // Підписка на події кліків по клітинках
  477.             for (int x = 0; x < 10; x++) {
  478.                 for (int y = 0; y < 8; y++) {
  479.                     boardPictureBoxes[x, y]->Click += gcnew EventHandler(this, &MyForm::Cell_Click);
  480.                 }
  481.             }
  482.  
  483.         }
  484.  
  485.     protected:
  486.  
  487.         ~MyForm()
  488.         {
  489.             if (components)
  490.             {
  491.                 delete components;
  492.             }
  493.         }
  494.  
  495.     private:
  496.         System::ComponentModel::Container^ components;
  497.  
  498.         // Графічний інтерфейс
  499.         array<PictureBox^, 2>^ boardPictureBoxes;
  500.         ChessBoard* board;
  501.         Graphics^ g;
  502.         int selectedX = -1; // Stores the X coordinate of the selected piece
  503.         int selectedY = -1; // Stores the Y coordinate of the selected piece
  504.         // Додаткові поля для зберігання інформації про допустимі ходи для вибраної фігури
  505.         bool validMovesHighlighted = false;
  506.         List<Tuple<int, int>^>^ validMoves = gcnew List<Tuple<int, int>^>();
  507.         array<PictureBox^>^ capturedPieces; // New array to hold the captured pieces
  508.         Panel^ panelCaptured; // Panel to display captured pieces
  509.  
  510.         const int imageWidth = 40; // Зменшені розміри фігур по ширині
  511.         const int imageHeight = 40; // Зменшені розміри фігур по висоті
  512.  
  513.         void InitializeComponent(void)
  514.         {
  515.             this->components = gcnew System::ComponentModel::Container();
  516.             this->Size = System::Drawing::Size(820, 660);
  517.             this->Text = L"MyForm";
  518.             this->AutoScaleDimensions = System::Drawing::SizeF(8, 16);
  519.             this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
  520.  
  521.             // Створення PictureBox для клітинок шахівниці
  522.             boardPictureBoxes = gcnew array<PictureBox^, 2>(10, 8);
  523.             for (int x = 0; x < 10; x++) {
  524.                 for (int y = 0; y < 8; y++) {
  525.                     boardPictureBoxes[x, y] = gcnew PictureBox();
  526.                     boardPictureBoxes[x, y]->Size = System::Drawing::Size(60, 60);
  527.                     boardPictureBoxes[x, y]->Location = System::Drawing::Point(x * 60, y * 60);
  528.                     boardPictureBoxes[x, y]->BackColor = ((x + y) % 2 == 0) ? Color::Bisque : Color::DarkOrange;
  529.                     this->Controls->Add(boardPictureBoxes[x, y]);
  530.                 }
  531.             }
  532.  
  533.  
  534.         }
  535.         // Method to update the captured pieces display
  536.         void DrawCapturedPieces()
  537.         {
  538.             // Clear the captured pieces panel before redrawing
  539.             panelCaptured->Controls->Clear();
  540.  
  541.             int x = 0;
  542.             int y = 0;
  543.  
  544.             // Loop through the captured pieces array and add PictureBoxes to the panel
  545.             for (int i = 0; i < capturedPieces->Length; i++)
  546.             {
  547.                 if (capturedPieces[i] != nullptr)
  548.                 {
  549.                     capturedPieces[i]->Location = System::Drawing::Point(x, y);
  550.                     panelCaptured->Controls->Add(capturedPieces[i]);
  551.  
  552.                     x += capturedPieces[i]->Width + 5;
  553.  
  554.                     // Move to the next row after every 4 pieces
  555.                     if ((i + 1) % 4 == 0)
  556.                     {
  557.                         x = 0;
  558.                         y += capturedPieces[i]->Height + 5;
  559.                     }
  560.                 }
  561.             }
  562.         }
  563.         void UpdateCapturedPiecesDisplay() {
  564.         // Display the captured pieces on the form
  565.         int capturedCount = 0;
  566.         for (int x = 0; x < 10; x++) {
  567.             for (int y = 0; y < 8; y++) {
  568.                 ChessPiece* piece = board->getPiece(x, y);
  569.                 if (piece != nullptr && piece->isBlack != board->getPiece(selectedX, selectedY)->isBlack) {
  570.                     // The piece at (x, y) is an opponent's piece captured by the current player
  571.                     Image^ capturedImage = LoadImageForPiece(piece, x, y);
  572.                     capturedPieces[capturedCount]->Image = capturedImage;
  573.                     capturedCount++;
  574.                 }
  575.             }
  576.         }
  577.     }
  578.  
  579.         Image^ LoadImageForPiece(ChessPiece* piece, int x, int y) {
  580.             Image^ image = nullptr;
  581.  
  582.             if (dynamic_cast<Chancellor*>(piece)) {
  583.                 if (piece->isBlack) {
  584.                     image = Image::FromFile("C:\\chess\\black-chess\\сhancellor.png");
  585.                 }
  586.                 else {
  587.                     image = Image::FromFile("C:\\chess\\white-chess\\сhancellor.png");
  588.                 }
  589.             }
  590.             else if (dynamic_cast<Archbishop*>(piece)) {
  591.                 if (piece->isBlack) {
  592.                     image = Image::FromFile("C:\\chess\\black-chess\\archbishop.png");
  593.                 }
  594.                 else {
  595.                     image = Image::FromFile("C:\\chess\\white-chess\\archbishop.png");
  596.                 }
  597.             }
  598.             else if (dynamic_cast<Pawn*>(piece)) {
  599.                 if (piece->isBlack) {
  600.                     image = Image::FromFile("C:\\chess\\black-chess\\pawn.png");
  601.                 }
  602.                 else {
  603.                     image = Image::FromFile("C:\\chess\\white-chess\\pawn.png");
  604.                 }
  605.             }
  606.             else if (dynamic_cast<Queen*>(piece)) {
  607.                 if (piece->isBlack) {
  608.                     image = Image::FromFile("C:\\chess\\black-chess\\queen.png");
  609.                 }
  610.                 else {
  611.                     image = Image::FromFile("C:\\chess\\white-chess\\queen.png");
  612.                 }
  613.             }
  614.             else if (dynamic_cast<King*>(piece)) {
  615.                 if (piece->isBlack) {
  616.                     image = Image::FromFile("C:\\chess\\black-chess\\king.png");
  617.                 }
  618.                 else {
  619.                     image = Image::FromFile("C:\\chess\\white-chess\\king.png");
  620.                 }
  621.             }
  622.             else if (dynamic_cast<Bishop*>(piece)) {
  623.                 if (piece->isBlack) {
  624.                     image = Image::FromFile("C:\\chess\\black-chess\\bishop.png");
  625.                 }
  626.                 else {
  627.                     image = Image::FromFile("C:\\chess\\white-chess\\bishop.png");
  628.                 }
  629.             }
  630.             else if (dynamic_cast<Knight*>(piece)) {
  631.                 if (piece->isBlack) {
  632.                     image = Image::FromFile("C:\\chess\\black-chess\\knight.png");
  633.                 }
  634.                 else {
  635.                     image = Image::FromFile("C:\\chess\\white-chess\\knight.png");
  636.                 }
  637.             }
  638.             else if (dynamic_cast<Rook*>(piece)) {
  639.                 if (piece->isBlack) {
  640.                     image = Image::FromFile("C:\\chess\\black-chess\\rook.png");
  641.                 }
  642.                 else {
  643.                     image = Image::FromFile("C:\\chess\\white-chess\\rook.png");
  644.                 }
  645.             }
  646.             if (image != nullptr) {
  647.                 // Змініть розмір зображення фігури на imageWidth та imageHeight
  648.                 image = gcnew Bitmap(image, imageWidth, imageHeight);
  649.             }
  650.             return image;
  651.         }
  652.  
  653.         void Cell_Click(Object^ sender, EventArgs^ e) {
  654.             PictureBox^ pictureBox = (PictureBox^)sender;
  655.             int x = pictureBox->Location.X / 60;
  656.             int y = pictureBox->Location.Y / 60;
  657.  
  658.             // If no piece is currently selected, try to select one
  659.             if (selectedX == -1 && selectedY == -1) {
  660.                 ChessPiece* piece = board->getPiece(x, y);
  661.                 if (piece != nullptr) {
  662.                     validMoves = GetValidMoves(x, y);
  663.                     HighlightValidMoves();
  664.                     // Highlight the selected piece (e.g., change the border color of the PictureBox)
  665.                     pictureBox->BorderStyle = BorderStyle::FixedSingle;
  666.  
  667.                     // Update selected coordinates
  668.                     selectedX = x;
  669.                     selectedY = y;
  670.                 }
  671.             }
  672.             else {
  673.                 // A piece is already selected, try to make a move to the clicked cell
  674.                 if (board->isValidMove(selectedX, selectedY, x, y)) {
  675.                     // Attempt to make the move
  676.                     bool moveResult = board->makeMove(selectedX, selectedY, x, y);
  677.  
  678.                     if (moveResult) {
  679.                         // Move was successful, update the graphical representation
  680.                         DrawPiece(selectedX, selectedY); // Clear the source cell
  681.                         DrawPiece(x, y); // Draw the piece on the destination cell
  682.                     }
  683.                 }
  684.  
  685.                 validMoves->Clear();
  686.                 UnhighlightValidMoves();
  687.  
  688.                 // Unhighlight the selected piece (e.g., reset the border color of the PictureBox)
  689.                 boardPictureBoxes[selectedX, selectedY]->BorderStyle = BorderStyle::None;
  690.  
  691.                 // Reset selected coordinates
  692.                 selectedX = -1;
  693.                 selectedY = -1;
  694.             }
  695.             DrawCapturedPieces();
  696.         }
  697.  
  698.         List<Tuple<int, int>^>^ GetValidMoves(int x, int y) {
  699.             List<Tuple<int, int>^>^ moves = gcnew List<Tuple<int, int>^>();
  700.  
  701.             ChessPiece* piece = board->getPiece(x, y);
  702.             if (piece != nullptr) {
  703.                 for (int destX = 0; destX < 10; destX++) {
  704.                     for (int destY = 0; destY < 8; destY++) {
  705.                         if (board->isValidMove(x, y, destX, destY)) {
  706.                             moves->Add(gcnew Tuple<int, int>(destX, destY));
  707.                         }
  708.                     }
  709.                 }
  710.             }
  711.  
  712.             return moves;
  713.         }
  714.  
  715.  
  716.         void HighlightValidMoves() {
  717.             for each (Tuple<int, int> ^ move in validMoves) {
  718.                 int x = move->Item1;
  719.                 int y = move->Item2;
  720.                 boardPictureBoxes[x, y]->BackColor = Color::Green;
  721.             }
  722.         }
  723.  
  724.         void UnhighlightValidMoves() {
  725.             for (int x = 0; x < 10; x++) {
  726.                 for (int y = 0; y < 8; y++) {
  727.                     if ((x + y) % 2 == 0) {
  728.                         boardPictureBoxes[x, y]->BackColor = Color::Bisque;
  729.                     }
  730.                     else {
  731.                         boardPictureBoxes[x, y]->BackColor = Color::DarkOrange;
  732.                     }
  733.                 }
  734.             }
  735.         }
  736.  
  737.  
  738.         void InitializeChessboard() {
  739.             // ... (rest of the code remains unchanged) ...
  740.  
  741.             // Initialize graphical representation of the chessboard
  742.             for (int x = 0; x < 10; x++) {
  743.                 for (int y = 0; y < 8; y++) {
  744.                     // Update the graphical representation of the chessboard
  745.                     DrawPiece(x, y); // Draw the piece or an empty cell at (x, y)
  746.                 }
  747.             }
  748.         }
  749.  
  750.         void DrawPiece(int x, int y) {
  751.             ChessPiece* piece = board->getPiece(x, y);
  752.             if (piece != nullptr) {
  753.                 boardPictureBoxes[x, y]->SizeMode = PictureBoxSizeMode::Zoom;
  754.  
  755.                 Image^ image = LoadImageForPiece(piece, x, y);
  756.                 if (image != nullptr) {
  757.                     // Display the piece on the chessboard
  758.                     boardPictureBoxes[x, y]->Image = image;
  759.                 }
  760.             }
  761.             else {
  762.                 // Display an empty cell on the chessboard
  763.                 boardPictureBoxes[x, y]->Image = nullptr;
  764.                 boardPictureBoxes[x, y]->BackColor = ((x + y) % 2 == 0) ? Color::Bisque : Color::DarkOrange;
  765.             }
  766.         }
  767.     };
  768. }
  769.  
Add Comment
Please, Sign In to add comment