Esempio n. 1
0
void b19(int* position)
{
    const char knight  = 'K';       // symbol of Knight
    const char capture = 'X';       // symbol of captured field
    const char empty   = '.';       // symbol of empty field
    int moves[2]       = {1, 2};    // length of knight's moves: one or two cells
    int directions[2]  = {-1, 1};   // direction of knight's moves: up/down in either dimension
    char board[size][size];         // our chessboard

    // initialize board; mark all squares as empty
    for(int rank = size-1; rank >= 0; rank--)
        for(int file = 0; file < size ; file++)
            board[rank][file] = empty;

    int file = position[0];             // file is a chessboard term for a column
    int rank = position[1];             // rank is a chessboard term for a row
    board[rank][file] = knight;         // mark knight's position on board

    //
    // the BIG loop for calculating knight's captured squares
    //
    // for each direction - up or down - for rank ..
    for (int rank_direction = 0; rank_direction <= 1; rank_direction++)
    {
        // .., and for each possible move - 1 or 2 cells, on rank..
        for (int rank_move = 0; rank_move <= 1; rank_move++)
        {
            // .., and for each possible direction for file:
            for (int file_direction = 0; file_direction <= 1; file_direction++)
            {
                // calculate move per file
                // (trick is, if you move knight ONE cell right, you can move just TWO cells up or down)
                int file_move = rank_move? moves[0] : moves[1];
                int file_captured = file + directions[file_direction] * file_move;
                int rank_captured = rank + directions[rank_direction] * moves[rank_move];

                if
                (
                    // if file and rank are on board (1..8)
                    is_on_board(file_captured) &&
                    is_on_board(rank_captured)
                )
                {
                    // then mark this square as captured
                    board[rank_captured][file_captured] = capture;
                }
            }
        }
    }
    print_chessboard(board, position);
}
Esempio n. 2
0
// converts input of 'a' or 'A' to 0 (first file)
int to_file(int file)
{
    const int  uppercase = 'A'; // ASCII code for char 'A'
    const int  lowercase = 'a'; // ASCII code for char 'a'

    if (is_on_board(file, uppercase))
    {
        file -= uppercase;
    }
    else if (is_on_board(file, lowercase))
    {
        file -= lowercase;
    }
    return file;
}
Esempio n. 3
0
int* request_position()
{
    int* position = new int[2];

    position[0] = -1; // filling with incorrect data to start loop
    position[1] = -1;

    string answer;

    while (!is_on_board(position))
    {
        print("Enter knight's position, from a1 to h8: ");
        cin >> answer;
        position[0] = to_file(answer[0]);
        position[1] = to_rank(answer[1]);

        if (!is_on_board(position))
        {
            print("You have entered incorrect data.");
        }
    }
    return position;
}
Esempio n. 4
0
bool is_on_board(int file, int char_code)
{
    return is_on_board(file - char_code);
}
Esempio n. 5
0
// checks if position is on board (a1 till h8 - [0][0] till [7][7])
bool is_on_board(int *position)
{
    return is_on_board(position[0]) &&
           is_on_board(position[1]);
}
Esempio n. 6
0
// -------------------------------------------------------------------
// Determine whether the given square is under attack by a given side
// -------------------------------------------------------------------
bool ChessBoard::is_attacked(bool by_white, int to) const {
    int from, i;
    const int ahead=by_white?-1:1;

    // Check for pawn attacks
    from=to+ahead*Rank-File;
    if (is_on_board(from) && square[from]==make_colour(Pawn, by_white) &&
        abs(which_file(from)-which_file(to))==1)
        return true;
    from=to+ahead*Rank+File;
    if (is_on_board(from) && square[from]==make_colour(Pawn, by_white) &&
        abs(which_file(from)-which_file(to))==1)
        return true;

    // Check for bishop/queen attacks
    static const int b_increment[]={-Rank-File, Rank-File, -Rank+File, 
        Rank+File};
    for(i=0;i<4;i++) {
        for (from=to+b_increment[i]; is_on_board(from) && 
            abs(which_file(from)-which_file(from-b_increment[i]))==1;
            from+=b_increment[i]) {
            if (square[from]==make_colour(Bishop, by_white) ||
                square[from]==make_colour(Queen, by_white))
                return true;
            if (square[from]!=Empty)
                break;
        }
    }

    // Check for rook/queen attacks
    static const int r_increment[]={-Rank, Rank, -File, File};
    for(i=0;i<4;i++) {
        for (from=to+r_increment[i]; is_on_board(from) && 
            abs(which_file(from)-which_file(from-r_increment[i]))<=1;
            from+=r_increment[i]) {
            if (square[from]==make_colour(Rook, by_white) ||
                square[from]==make_colour(Queen, by_white))
                return true;
            if (square[from]!=Empty)
                break;
        }
    }

    // Check for knight attacks
    static const int n_source[] = {-2*Rank-File, -2*Rank+File, -2*File-Rank,
        -2*File+Rank, 2*Rank-File, 2*Rank+File, 2*File-Rank, 2*File+Rank};
    for (i=0;i<8;i++) {
        from=to+n_source[i];
        if (is_on_board(from) && square[from]==make_colour(Knight, by_white) &&
            abs(which_file(from)-which_file(to))<3)
            return true;
    }

    // Check for king attacks
    static const int k_source[] = {-Rank-File, -Rank, -Rank+File, -File,
        File, Rank-File, Rank, Rank+File};
    for (i=0;i<8;i++) {
        from=to+k_source[i];
        if (is_on_board(from) && square[from]==make_colour(King, by_white) &&
            abs(which_file(from)-which_file(to))<=1)
            return true;
    }

    // Check for en passant attacks
    if (to-ahead*Rank==en_passant && 
        (square[to-File]==make_colour(Pawn, by_white) ||
         square[to+File]==make_colour(Pawn, by_white)))
        return true;

    return false;
}