#include <iostream> #include<string> #include <vector> using namespace std; // Define…
- User
#include #include #include
using namespace std;
// Define the chess board char board[8][8] = { {'R', 'N', 'B', 'Q', 'K', 'B', 'N', 'R'}, {'P', 'P', 'P', 'P', 'P', 'P', 'P', 'P'}, {' ', '.', ' ', '.', ' ', '.', ' ', '.'}, {'.', ' ', '.', ' ', '.', ' ', '.', ' '}, {' ', '.', ' ', '.', ' ', '.', ' ', '.'}, {'.', ' ', '.', ' ', '.', ' ', '.', ' '}, {'p', 'p', 'p', 'p', 'p', 'p', 'p', 'p'}, {'r', 'n', 'b', 'q', 'k', 'b', 'n', 'r'} };
bool is_check(char board[8][8], bool white_to_move); bool is_checkmate(char board[8][8], bool white_to_move); bool is_check(char board[8][8], bool white_to_move); // Function to display the chess board void print_board() { cout << endl << " a b c d e f g h" << endl; for (int i = 0; i < 8; i++) { cout << i + 1 << " "; for (int j = 0; j < 8; j++) { cout << board[i][j] << " "; } cout << i + 1 << endl; } cout << " a b c d e f g h" << endl << endl; }
// Function to check if a move is valid bool is_valid_move(int from_x, int from_y, int to_x, int to_y) { // Check if the move is within the board limits if (from_x < 0 || from_x > 7 || from_y < 0 || from_y > 7 || to_x < 0 || to_x > 7 || to_y < 0 || to_y > 7) { return false; } // Check if the piece at the starting position is the player's own piece if (board[from_x][from_y] >= 'A' && board[from_x][from_y] <= 'Z') { return false; } // Check if the piece at the starting position is not an empty space if (board[from_x][from_y] == ' ' || board[from_x][from_y] == '.') { return false; } // Check if the piece at the destination position is the player's own piece if (board[to_x][to_y] >= 'a' && board[to_x][to_y] <= 'z') { return false; } // Check if the move is legal for the piece char piece = board[from_x][from_y]; if (piece == 'P') { if (to_x != from_x - 1) { return false; } if (to_y != from_y) { if (to_y != from_y - 1 && to_y != from_y + 1) { return false; } if (board[to_x][to_y] == ' ') { return false; } } else { if (to_x == 0) { // Promotion to Queen board[to_x][to_y] = 'Q'; } } } else if (piece == 'p') { if (to_x != from_x + 1) { return false; } if (to_y != from_y) { if (to_y != from_y - 1 && to_y != from_y + 1) { return false; } if (board[to_x][to_y] == ' ') { return false; } } else { if (to_x == 7) { // Promotion to Queen board[to_x][to_y] = 'q'; } } } else if (piece == 'R' || piece == 'r') { if (from_x != to_x && from_y != to_y) { return false; } if (from_x == to_x) { int start = min(from_y, to_y); int end = max(from_y, to_y); for (int i = start + 1; i < end; i++) { if (board[from_x][i] != ' ' && board[from_x][i] != '.') { return false; } } } else { int start = min(from_x, to_x); int end = max(from_x, to_x); for (int i = start + 1; i < end; i++) { if (board[i][from_y] != ' ' && board[i][from_y] != '.') { return false; } } } } else if (piece == 'N' || piece == 'n') { if ((to_x == from_x - 1 && to_y == from_y - 2) || (to_x == from_x - 2 && to_y == from_y - 1) || (to_x == from_x - 2 && to_y == from_y + 1) || (to_x == from_x - 1 && to_y == from_y + 2) || (to_x == from_x + 1 && to_y == from_y + 2) || (to_x == from_x + 2 && to_y == from_y + 1) || (to_x == from_x + 2 && to_y == from_y - 1) || (to_x == from_x + 1 && to_y == from_y - 2)) { // Valid move } else { return false; } } else if (piece == 'B' || piece == 'b') { if (abs(to_x - from_x) != abs(to_y - from_y)) { return false; } int x_dir = (to_x > from_x) ? 1 : -1; int y_dir = (to_y > from_y) ? 1 : -1; for (int i = from_x + x_dir, j = from_y + y_dir; i != to_x; i += x_dir, j += y_dir) { if (board[i][j] != ' ' && board[i][j] != '.') { return false; } } } else if (piece == 'Q' || piece == 'q') { if (from_x == to_x || from_y == to_y) { int start = min(from_y, to_y); int end = max(from_y, to_y); if (from_x == to_x) { for (int i = start + 1; i < end; i++) { if (board[from_x][i] != ' ' && board[from_x][i] != '.') { return false; } } } else { for (int i = start + 1; i < end; i++) { if (board[i][from_y] != ' ' && board[i][from_y] != '.') { return false; } } } } else if (abs(to_x - from_x) == abs(to_y - from_y)) { int x_dir = (to_x > from_x) ? 1 : -1; int y_dir = (to_y > from_y) ? 1 : -1; for (int i = from_x + x_dir, j = from_y + y_dir; i != to_x; i += x_dir, j += y_dir) { if (board[i][j] != ' ' && board[i][j] != '.') { return false; } } } else { return false; } } else if (piece == 'K' || piece == 'k') { if (abs(to_x - from_x) <= 1 && abs(to_y - from_y) <= 1) { // valid move for king } else { return false; } } else { return false; }
- Assistant