Vla_DOS

Untitled

Apr 21st, 2023
655
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.24 KB | None | 0 0
  1. #include <iostream>
  2. #define BOARD_SIZE 8    // Size of the chess board
  3.  
  4.  
  5. class Queen {
  6. public:
  7.     // Queen position
  8.     int x;
  9.     int y;
  10.  
  11.     // Default constructor places queen at (0, 0)
  12.     Queen() {
  13.         this->x = 0;
  14.         this->y = 0;
  15.     }
  16.  
  17.     // Set X coordinate of queen
  18.     bool setX(int nx) {
  19.         if (nx >= BOARD_SIZE || nx < 0) {
  20.             return false;
  21.         }
  22.  
  23.         x = nx;
  24.         return true;
  25.     }
  26.  
  27.     // Set Y coordinate of queen
  28.     bool setY(int ny) {
  29.         if (ny >= BOARD_SIZE || ny < 0) {
  30.             return false;
  31.         }
  32.  
  33.         y = ny;
  34.         return true;
  35.     }
  36.  
  37.     void printTargets(Queen* board[BOARD_SIZE][BOARD_SIZE]) {
  38.         // Check horizontal
  39.         for (int x = this->x + 1; x < BOARD_SIZE; x++) {
  40.             if (board[this->y][x] != NULL) {
  41.                 printf("Queen at (%d; %d) can attack Queen at (%d; %d)\n", this->x, this->y, x, this->y);
  42.                 break;
  43.             }
  44.         }
  45.  
  46.         for (int x = this->x - 1; x > 0; x--) {
  47.             if (board[this->y][x] != NULL) {
  48.                 printf("Queen at (%d; %d) can attack Queen at (%d; %d)\n", this->x, this->y, x, this->y);
  49.                 break;
  50.             }
  51.         }
  52.  
  53.         // Check vertical
  54.         for (int y = this->y + 1; y < BOARD_SIZE; y++) {
  55.             if (board[y][this->x] != NULL) {
  56.                 printf("Queen at (%d; %d) can attack Queen at (%d; %d)\n", this->x, this->y, this->x, y);
  57.                 break;
  58.             }
  59.         }
  60.  
  61.         for (int y = this->y - 1; y > 0; y--) {
  62.             if (board[y][this->x] != NULL) {
  63.                 printf("Queen at (%d; %d) can attack Queen at (%d; %d)\n", this->x, this->y, this->x, y);
  64.                 break;
  65.             }
  66.         }
  67.  
  68.         // Check first diagonal
  69.         for (int m = 1; m < BOARD_SIZE; m++) {
  70.             if (this->x + m >= BOARD_SIZE || this->y + m >= BOARD_SIZE) {
  71.                 break;
  72.             }
  73.  
  74.             if (board[this->y + m][this->x + m] != NULL) {
  75.                 printf("Queen at (%d; %d) can attack Queen at (%d; %d)\n", this->x, this->y, this->x + m, this->y + m);
  76.                 break;
  77.             }
  78.         }
  79.  
  80.         for (int m = 1; m < BOARD_SIZE; m++) {
  81.             if (this->x - m < 0 || this->y - m < 0) {
  82.                 break;
  83.             }
  84.  
  85.             if (board[this->y - m][this->x - m] != NULL) {
  86.                 printf("Queen at (%d; %d) can attack Queen at (%d; %d)\n", this->x, this->y, this->x - m, this->y - m);
  87.                 break;
  88.             }
  89.         }
  90.  
  91.         // Check second diagonal
  92.         for (int m = 1; m < BOARD_SIZE; m++) {
  93.             if (this->x - m < 0 || this->y + m >= BOARD_SIZE) {
  94.                 break;
  95.             }
  96.  
  97.             if (board[this->y + m][this->x - m] != NULL) {
  98.                 printf("Queen at (%d; %d) can attack Queen at (%d; %d)\n", this->x, this->y, this->x - m, this->y + m);
  99.                 break;
  100.             }
  101.         }
  102.  
  103.         for (int m = 1; m < BOARD_SIZE; m++) {
  104.             if (this->x + m >= BOARD_SIZE || this->y - m < 0) {
  105.                 break;
  106.             }
  107.  
  108.             if (board[this->y - m][this->x + m] != NULL) {
  109.                 printf("Queen at (%d; %d) can attack Queen at (%d; %d)\n", this->x, this->y, this->x + m, this->y - m);
  110.                 break;
  111.             }
  112.         }
  113.     }
  114. };
  115.  
  116.  
  117. int main()
  118. {
  119.     // Make an 2D array of chess board
  120.     Queen* board[BOARD_SIZE][BOARD_SIZE];
  121.  
  122.     // NULL array
  123.     for (int i = 0; i < BOARD_SIZE; i++) {
  124.         for (int j = 0; j < BOARD_SIZE; j++) {
  125.             board[i][j] = 0;
  126.         }
  127.     }
  128.  
  129.     int queens_amount;  // Amount of queens at the board
  130.  
  131.     // Read amount of queens from user
  132.     std::cout << "Enter amount of queens: ";
  133.     std::cin >> queens_amount;
  134.  
  135.     // Check if amount of queens is valid value
  136.     if (queens_amount > BOARD_SIZE * BOARD_SIZE) {
  137.         printf("%d is the maximum amount of queens for board size of %d", BOARD_SIZE * BOARD_SIZE, BOARD_SIZE);
  138.         return 0;
  139.     }
  140.  
  141.     if (queens_amount <= 0) {
  142.         std::cout << "Board cannot be empty";
  143.     }
  144.  
  145.     // Read queens coordinates
  146.     for (int i = 0; i < queens_amount; i++) {
  147.         Queen* new_queen = new Queen;   // Create new queen
  148.         int temp;                       // Temporary variable for reading coordinates values
  149.  
  150.         while (true) {
  151.             std::cout << "\nQueen #" << i + 1 << '\n';
  152.  
  153.             // Read X coordinate
  154.             std::cout << "X = ";
  155.             std::cin >> temp;
  156.             if (!new_queen->setX(temp)) {
  157.                 std::cout << "Wrong X coordinates! (max = " << BOARD_SIZE - 1 << ", min = 0\n\n";
  158.                 continue;
  159.             }
  160.  
  161.             // Read Y coordinate
  162.             std::cout << "Y = ";
  163.             std::cin >> temp;
  164.             if (!new_queen->setY(temp)) {
  165.                 std::cout << "Wrong Y coordinates! (max = " << BOARD_SIZE - 1 << ", min = 0\n\n";
  166.                 continue;
  167.             }
  168.  
  169.             // Check if there is no queen at given position
  170.             if (board[new_queen->y][new_queen->x] != NULL) {
  171.                 std::cout << "There is already queen at that position!\n\n";
  172.                 continue;
  173.             }
  174.  
  175.             // Add created queen to array of queens
  176.             board[new_queen->y][new_queen->x] = new_queen;
  177.             break;
  178.         }
  179.     }
  180.  
  181.     // Print board
  182.     std::cout << "\n\nBoard:\n";
  183.     for (int i = 0; i < BOARD_SIZE; i++) {
  184.         for (int j = 0; j < BOARD_SIZE; j++) {
  185.             std::cout << ((board[i][j] != NULL) ? "O " : "_ ");
  186.         }
  187.         std::cout << "\n";
  188.     }
  189.     std::cout << "\n\n";
  190.  
  191.     // Print all attack targets for all queens
  192.     for (int y = 0; y < BOARD_SIZE; y++) {
  193.         for (int x = 0; x < BOARD_SIZE; x++) {
  194.             // If there is queen at that position - print all it targets
  195.             if (board[y][x] != NULL) {
  196.                 board[y][x]->printTargets(board);
  197.                 std::cout << "\n";
  198.             }
  199.         }
  200.     }
  201.  
  202.     // Free allocated memory
  203.     for (int i = 0; i < BOARD_SIZE; i++) {
  204.         for (int j = 0; j < BOARD_SIZE; j++) {
  205.             if (board[i][j] != NULL) {
  206.                 free(board[i][j]);
  207.             }
  208.         }
  209.     }
  210.     return 0;
  211. }
Advertisement
Add Comment
Please, Sign In to add comment