Conquer Chess
Chess and Starcraft combined
Loading...
Searching...
No Matches
Functions
square.cpp File Reference
#include "square.h"
#include "game_coordinate.h"
#include "game_rect.h"
#include "helper.h"
#include <cassert>
#include <cmath>
#include <iostream>
#include <iterator>
#include <regex>
#include <sstream>
Include dependency graph for square.cpp:

Functions

bool are_adjacent (const square &a, const square &b) noexcept
 Are the squares adjacent (i.e.
 
bool are_adjacent_for_knight (const square &a, const square &b) noexcept
 Are the squares adjacent for a knight.
 
bool are_all_unique (std::vector< square > squares)
 Are all the squares unique?
 
bool are_at_knights_jump_distance (const square &a, const square &b) noexcept
 Are the squares seperated by the jump of a knight?
 
bool are_en_passant_capture_squares (const square &attacker_square, const square &target_square, const chess_color player_color)
 Are these possible en-passant capture squares?
 
bool are_on_adjacent_diagonal (const square &a, const square &b) noexcept
 Are the squares adjacent on the same diagonal, e.g. d1 and e2.
 
bool are_on_same_diagonal (const square &a, const square &b) noexcept
 Are the squares on the same diagonal, e.g. d1 and a4.
 
bool are_on_same_file (const square &a, const square &b) noexcept
 Are the squares on the same file, e.g. e2 and e4.
 
bool are_on_same_half_diagonal (const square &a, const square &b) noexcept
 Are the squares on the same half-diagonal, also 'the jump of a knight' e.g.
 
bool are_on_same_rank (const square &a, const square &b) noexcept
 Are the squares on the same rank, e.g. a1 and a8.
 
std::vector< std::pair< int, int > > collect_all_bishop_delta_pairs () noexcept
 Collect all the delta squares for a bishop.
 
std::vector< std::vector< square > > collect_all_bishop_target_squares (const square &s) noexcept
 Collect all the possible target squares for a bishop in all directions.
 
std::vector< squarecollect_all_king_target_squares (const square &s) noexcept
 Collect all the possible target squares for a king.
 
std::vector< std::pair< int, int > > collect_all_knight_delta_pairs () noexcept
 Collect all the delta squares for a knight.
 
std::vector< squarecollect_all_knight_target_squares (const square &s) noexcept
 Collect all the possible target squares for a knight.
 
std::vector< std::pair< int, int > > collect_all_queen_delta_pairs () noexcept
 Collect all the delta squares for a queen.
 
std::vector< std::vector< square > > collect_all_queen_target_squares (const square &s) noexcept
 Collect all the possible target squares for a queen in all directions.
 
std::vector< std::pair< int, int > > collect_all_rook_delta_pairs () noexcept
 Collect all the delta squares for a rook.
 
std::vector< std::vector< square > > collect_all_rook_target_squares (const square &s) noexcept
 Collect all the possible target squares for a rook in all directions.
 
std::vector< squareconcatenate (const std::vector< square > &a, const std::vector< square > &b)
 Concatenate the vectors.
 
square create_random_square (std::default_random_engine &rng_engine)
 Create a random square.
 
std::vector< char > get_all_files () noexcept
 Get all the files, i.e. 'a' to and including 'h'.
 
std::vector< int > get_all_ranks () noexcept
 Get all the ranks, i.e. 1 to and including 8.
 
std::vector< int > get_all_ranks_in_reversed_order () noexcept
 Get all the ranks in reversed order, i.e. 8 to and including 1.
 
square get_behind (const square &pawn_square, const chess_color color)
 Get the square behind a pawn.
 
square get_initial_king_square (const chess_color player_color) noexcept
 Get the intial king square.
 
square get_initial_rook_square (const chess_color player_color, const castling_type t) noexcept
 Get the default rook square, for a castling direction.
 
square get_en_passant_capture_square (const square &attacker_square, const square &target_square)
 Get the square that may be captured by en-passant.
 
char get_file (const square &s) noexcept
 Get the file of a square, e.g. 'd' from 'd4'.
 
std::vector< squareget_intermediate_squares (const square &from, const square &to)
 Get the intermediate squares, in an inclusive way: the first square will be 'from', to last square will be 'to'.
 
square get_king_target_square (const chess_color player_color, const piece_action_type action)
 Get the king target square when castling.
 
int get_rank (const square &s) noexcept
 Get the rank of a square, e.g. '3' from 'e3'.
 
square get_rook_target_square (const chess_color player_color, const piece_action_type action)
 Get the rook target square when castling.
 
square get_rotated_square (const square &position) noexcept
 
bool is_occupied (const square &s, const std::vector< square > &occupied_squares) noexcept
 Is the square 's' occupied?
 
bool is_valid_square_xy (const int x, const int y) noexcept
 Can the x and y be used to create a valid square?
 
bool is_invalid_square_xy (const int x, const int y) noexcept
 Can the x and y not be used to create a valid square?
 
void test_square ()
 Test this class and its free functions.
 
chess_color to_color (const square &s) noexcept
 What is the color of this square?
 
game_coordinate to_coordinat (const square &s) noexcept
 
game_coordinate to_coordinat (const std::string &pos) noexcept
 
game_rect to_game_rect (const square &s) noexcept
 Convert a square to a rectangle For example, a1 == ((0,0)-(1,1)) (notation is top-left, then bottom-left) For example, b1 == ((0,1)-(1,2)) (notation is top-left, then bottom-left)
 
std::vector< squareto_squares (std::vector< std::pair< int, int > > xys)
 Convert the pairs of x-y-coordinats to valid squares.
 
std::string to_str (const square &s) noexcept
 
std::string to_str (const std::vector< square > &squares) noexcept
 
bool operator== (const square &lhs, const square &rhs) noexcept
 
bool operator!= (const square &lhs, const square &rhs) noexcept
 
bool operator< (const square &lhs, const square &rhs) noexcept
 
std::ostream & operator<< (std::ostream &os, const square &s) noexcept
 
std::ostream & operator<< (std::ostream &os, const std::vector< square > &s) noexcept
 

Function Documentation

◆ are_adjacent()

bool are_adjacent ( const square a,
const square b 
)
noexcept

Are the squares adjacent (i.e.

for a king, not a knight)

See also
are_adjacent_for_knight to determine if squares are adjacent for a knight

◆ are_adjacent_for_knight()

bool are_adjacent_for_knight ( const square a,
const square b 
)
noexcept

Are the squares adjacent for a knight.

See also
are_adjacent to determine if squares are adjacent for a king

◆ are_all_unique()

bool are_all_unique ( std::vector< square squares)

Are all the squares unique?

◆ are_at_knights_jump_distance()

bool are_at_knights_jump_distance ( const square a,
const square b 
)
noexcept

Are the squares seperated by the jump of a knight?

e.g. b1 and c3

@seealso see are_on_same_half_diagonal} to see if the squares are seperated by multiple knight jumps in the same direction.

◆ are_en_passant_capture_squares()

bool are_en_passant_capture_squares ( const square attacker_square,
const square target_square,
const chess_color  player_color 
)

Are these possible en-passant capture squares?

For example, a5xb6 (attacker square and target square respectively)

◆ are_on_adjacent_diagonal()

bool are_on_adjacent_diagonal ( const square a,
const square b 
)
noexcept

Are the squares adjacent on the same diagonal, e.g. d1 and e2.

◆ are_on_same_diagonal()

bool are_on_same_diagonal ( const square a,
const square b 
)
noexcept

Are the squares on the same diagonal, e.g. d1 and a4.

◆ are_on_same_file()

bool are_on_same_file ( const square a,
const square b 
)
noexcept

Are the squares on the same file, e.g. e2 and e4.

◆ are_on_same_half_diagonal()

bool are_on_same_half_diagonal ( const square a,
const square b 
)
noexcept

Are the squares on the same half-diagonal, also 'the jump of a knight' e.g.

b1 and d5

@seealso see are_at_knights_jump_distance} to see if the squares are separated by one knight jump

◆ are_on_same_rank()

bool are_on_same_rank ( const square a,
const square b 
)
noexcept

Are the squares on the same rank, e.g. a1 and a8.

◆ collect_all_bishop_delta_pairs()

std::vector< std::pair< int, int > > collect_all_bishop_delta_pairs ( )
noexcept

Collect all the delta squares for a bishop.

◆ collect_all_bishop_target_squares()

std::vector< std::vector< square > > collect_all_bishop_target_squares ( const square s)
noexcept

Collect all the possible target squares for a bishop in all directions.

◆ collect_all_king_target_squares()

std::vector< square > collect_all_king_target_squares ( const square s)
noexcept

Collect all the possible target squares for a king.

◆ collect_all_knight_delta_pairs()

std::vector< std::pair< int, int > > collect_all_knight_delta_pairs ( )
noexcept

Collect all the delta squares for a knight.

◆ collect_all_knight_target_squares()

std::vector< square > collect_all_knight_target_squares ( const square s)
noexcept

Collect all the possible target squares for a knight.

◆ collect_all_queen_delta_pairs()

std::vector< std::pair< int, int > > collect_all_queen_delta_pairs ( )
noexcept

Collect all the delta squares for a queen.

◆ collect_all_queen_target_squares()

std::vector< std::vector< square > > collect_all_queen_target_squares ( const square s)
noexcept

Collect all the possible target squares for a queen in all directions.

◆ collect_all_rook_delta_pairs()

std::vector< std::pair< int, int > > collect_all_rook_delta_pairs ( )
noexcept

Collect all the delta squares for a rook.

◆ collect_all_rook_target_squares()

std::vector< std::vector< square > > collect_all_rook_target_squares ( const square s)
noexcept

Collect all the possible target squares for a rook in all directions.

◆ concatenate()

std::vector< square > concatenate ( const std::vector< square > &  a,
const std::vector< square > &  b 
)

Concatenate the vectors.

◆ create_random_square()

square create_random_square ( std::default_random_engine &  rng_engine)

Create a random square.

◆ get_all_files()

std::vector< char > get_all_files ( )
noexcept

Get all the files, i.e. 'a' to and including 'h'.

◆ get_all_ranks()

std::vector< int > get_all_ranks ( )
noexcept

Get all the ranks, i.e. 1 to and including 8.

◆ get_all_ranks_in_reversed_order()

std::vector< int > get_all_ranks_in_reversed_order ( )
noexcept

Get all the ranks in reversed order, i.e. 8 to and including 1.

◆ get_behind()

square get_behind ( const square pawn_square,
const chess_color  color 
)

Get the square behind a pawn.

Examples:

  • for white, behind e4 would be e3.
  • for black, behind e5 would be e6.

◆ get_en_passant_capture_square()

square get_en_passant_capture_square ( const square attacker_square,
const square target_square 
)

Get the square that may be captured by en-passant.

Assumes that the squares are valid, using are_en_passant_capture_squares}

◆ get_file()

char get_file ( const square s)
noexcept

Get the file of a square, e.g. 'd' from 'd4'.

◆ get_initial_king_square()

square get_initial_king_square ( const chess_color  player_color)
noexcept

Get the intial king square.

Which is:

  • e1 for white
  • e8 for black

◆ get_initial_rook_square()

square get_initial_rook_square ( const chess_color  player_color,
const castling_type  t 
)
noexcept

Get the default rook square, for a castling direction.

These are:

  • h1 for white for a kingside castle
  • a1 for white for a queenside castle
  • h8 for black for a kingside castle
  • a8 for black for a queenside castle

◆ get_intermediate_squares()

std::vector< square > get_intermediate_squares ( const square from,
const square to 
)

Get the intermediate squares, in an inclusive way: the first square will be 'from', to last square will be 'to'.

◆ get_king_target_square()

square get_king_target_square ( const chess_color  player_color,
const piece_action_type  action 
)

Get the king target square when castling.

Color Castling type Target square
Black Kingside e8 -> g8
Black Queenside e8 -> c8
White Kingside e1 -> g1
White Queenside e1 -> c1

◆ get_rank()

int get_rank ( const square s)
noexcept

Get the rank of a square, e.g. '3' from 'e3'.

◆ get_rook_target_square()

square get_rook_target_square ( const chess_color  player_color,
const piece_action_type  action 
)

Get the rook target square when castling.

Color Castling type Target square
Black Kingside h8 -> f8
Black Queenside a8 -> d8
White Kingside h1 -> f1
White Queenside a1 -> d1

◆ get_rotated_square()

square get_rotated_square ( const square position)
noexcept

◆ is_invalid_square_xy()

bool is_invalid_square_xy ( const int  x,
const int  y 
)
noexcept

Can the x and y not be used to create a valid square?

◆ is_occupied()

bool is_occupied ( const square s,
const std::vector< square > &  occupied_squares 
)
noexcept

Is the square 's' occupied?

◆ is_valid_square_xy()

bool is_valid_square_xy ( const int  x,
const int  y 
)
noexcept

Can the x and y be used to create a valid square?

◆ operator!=()

bool operator!= ( const square lhs,
const square rhs 
)
noexcept

◆ operator<()

bool operator< ( const square lhs,
const square rhs 
)
noexcept

◆ operator<<() [1/2]

std::ostream & operator<< ( std::ostream &  os,
const square s 
)
noexcept

◆ operator<<() [2/2]

std::ostream & operator<< ( std::ostream &  os,
const std::vector< square > &  s 
)
noexcept

◆ operator==()

bool operator== ( const square lhs,
const square rhs 
)
noexcept

◆ test_square()

void test_square ( )

Test this class and its free functions.

◆ to_color()

chess_color to_color ( const square s)
noexcept

What is the color of this square?

◆ to_coordinat() [1/2]

game_coordinate to_coordinat ( const square s)
noexcept

◆ to_coordinat() [2/2]

game_coordinate to_coordinat ( const std::string &  pos)
noexcept

◆ to_game_rect()

game_rect to_game_rect ( const square s)
noexcept

Convert a square to a rectangle For example, a1 == ((0,0)-(1,1)) (notation is top-left, then bottom-left) For example, b1 == ((0,1)-(1,2)) (notation is top-left, then bottom-left)

◆ to_squares()

std::vector< square > to_squares ( std::vector< std::pair< int, int > >  xys)

Convert the pairs of x-y-coordinats to valid squares.

◆ to_str() [1/2]

std::string to_str ( const square s)
noexcept

◆ to_str() [2/2]

std::string to_str ( const std::vector< square > &  squares)
noexcept