Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #define BOARD_SIZE 8 // Size of the chess board
- class Queen {
- public:
- // Queen position
- int x;
- int y;
- // Default constructor places queen at (0, 0)
- Queen() {
- this->x = 0;
- this->y = 0;
- }
- // Set X coordinate of queen
- bool setX(int nx) {
- if (nx >= BOARD_SIZE || nx < 0) {
- return false;
- }
- x = nx;
- return true;
- }
- // Set Y coordinate of queen
- bool setY(int ny) {
- if (ny >= BOARD_SIZE || ny < 0) {
- return false;
- }
- y = ny;
- return true;
- }
- void printTargets(Queen* board[BOARD_SIZE][BOARD_SIZE]) {
- // Check horizontal
- for (int x = this->x + 1; x < BOARD_SIZE; x++) {
- if (board[this->y][x] != NULL) {
- printf("Queen at (%d; %d) can attack Queen at (%d; %d)\n", this->x, this->y, x, this->y);
- break;
- }
- }
- for (int x = this->x - 1; x > 0; x--) {
- if (board[this->y][x] != NULL) {
- printf("Queen at (%d; %d) can attack Queen at (%d; %d)\n", this->x, this->y, x, this->y);
- break;
- }
- }
- // Check vertical
- for (int y = this->y + 1; y < BOARD_SIZE; y++) {
- if (board[y][this->x] != NULL) {
- printf("Queen at (%d; %d) can attack Queen at (%d; %d)\n", this->x, this->y, this->x, y);
- break;
- }
- }
- for (int y = this->y - 1; y > 0; y--) {
- if (board[y][this->x] != NULL) {
- printf("Queen at (%d; %d) can attack Queen at (%d; %d)\n", this->x, this->y, this->x, y);
- break;
- }
- }
- // Check first diagonal
- for (int m = 1; m < BOARD_SIZE; m++) {
- if (this->x + m >= BOARD_SIZE || this->y + m >= BOARD_SIZE) {
- break;
- }
- if (board[this->y + m][this->x + m] != NULL) {
- printf("Queen at (%d; %d) can attack Queen at (%d; %d)\n", this->x, this->y, this->x + m, this->y + m);
- break;
- }
- }
- for (int m = 1; m < BOARD_SIZE; m++) {
- if (this->x - m < 0 || this->y - m < 0) {
- break;
- }
- if (board[this->y - m][this->x - m] != NULL) {
- printf("Queen at (%d; %d) can attack Queen at (%d; %d)\n", this->x, this->y, this->x - m, this->y - m);
- break;
- }
- }
- // Check second diagonal
- for (int m = 1; m < BOARD_SIZE; m++) {
- if (this->x - m < 0 || this->y + m >= BOARD_SIZE) {
- break;
- }
- if (board[this->y + m][this->x - m] != NULL) {
- printf("Queen at (%d; %d) can attack Queen at (%d; %d)\n", this->x, this->y, this->x - m, this->y + m);
- break;
- }
- }
- for (int m = 1; m < BOARD_SIZE; m++) {
- if (this->x + m >= BOARD_SIZE || this->y - m < 0) {
- break;
- }
- if (board[this->y - m][this->x + m] != NULL) {
- printf("Queen at (%d; %d) can attack Queen at (%d; %d)\n", this->x, this->y, this->x + m, this->y - m);
- break;
- }
- }
- }
- };
- int main()
- {
- // Make an 2D array of chess board
- Queen* board[BOARD_SIZE][BOARD_SIZE];
- // NULL array
- for (int i = 0; i < BOARD_SIZE; i++) {
- for (int j = 0; j < BOARD_SIZE; j++) {
- board[i][j] = 0;
- }
- }
- int queens_amount; // Amount of queens at the board
- // Read amount of queens from user
- std::cout << "Enter amount of queens: ";
- std::cin >> queens_amount;
- // Check if amount of queens is valid value
- if (queens_amount > BOARD_SIZE * BOARD_SIZE) {
- printf("%d is the maximum amount of queens for board size of %d", BOARD_SIZE * BOARD_SIZE, BOARD_SIZE);
- return 0;
- }
- if (queens_amount <= 0) {
- std::cout << "Board cannot be empty";
- }
- // Read queens coordinates
- for (int i = 0; i < queens_amount; i++) {
- Queen* new_queen = new Queen; // Create new queen
- int temp; // Temporary variable for reading coordinates values
- while (true) {
- std::cout << "\nQueen #" << i + 1 << '\n';
- // Read X coordinate
- std::cout << "X = ";
- std::cin >> temp;
- if (!new_queen->setX(temp)) {
- std::cout << "Wrong X coordinates! (max = " << BOARD_SIZE - 1 << ", min = 0\n\n";
- continue;
- }
- // Read Y coordinate
- std::cout << "Y = ";
- std::cin >> temp;
- if (!new_queen->setY(temp)) {
- std::cout << "Wrong Y coordinates! (max = " << BOARD_SIZE - 1 << ", min = 0\n\n";
- continue;
- }
- // Check if there is no queen at given position
- if (board[new_queen->y][new_queen->x] != NULL) {
- std::cout << "There is already queen at that position!\n\n";
- continue;
- }
- // Add created queen to array of queens
- board[new_queen->y][new_queen->x] = new_queen;
- break;
- }
- }
- // Print board
- std::cout << "\n\nBoard:\n";
- for (int i = 0; i < BOARD_SIZE; i++) {
- for (int j = 0; j < BOARD_SIZE; j++) {
- std::cout << ((board[i][j] != NULL) ? "O " : "_ ");
- }
- std::cout << "\n";
- }
- std::cout << "\n\n";
- // Print all attack targets for all queens
- for (int y = 0; y < BOARD_SIZE; y++) {
- for (int x = 0; x < BOARD_SIZE; x++) {
- // If there is queen at that position - print all it targets
- if (board[y][x] != NULL) {
- board[y][x]->printTargets(board);
- std::cout << "\n";
- }
- }
- }
- // Free allocated memory
- for (int i = 0; i < BOARD_SIZE; i++) {
- for (int j = 0; j < BOARD_SIZE; j++) {
- if (board[i][j] != NULL) {
- free(board[i][j]);
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment