Chess 0.0.1
A library written in c
Loading...
Searching...
No Matches
square.h File Reference

Defines the chess square type and related utility functions for representing and manipulating chess squares. More...

#include <chess/color.h>
#include <chess/file.h>
#include <chess/macros.h>
#include <chess/rank.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>

Go to the source code of this file.

Typedefs

typedef struct ChessPosition ChessPosition
 Forward declaration of ChessPosition.
 

Enumerations

enum  ChessSquare : uint8_t {
  CHESS_SQUARE_A1 = CHESS_FILE_A | CHESS_RANK_1 << 4U ,
  CHESS_SQUARE_B1 = CHESS_FILE_B | CHESS_RANK_1 << 4U ,
  CHESS_SQUARE_C1 = CHESS_FILE_C | CHESS_RANK_1 << 4U ,
  CHESS_SQUARE_D1 = CHESS_FILE_D | CHESS_RANK_1 << 4U ,
  CHESS_SQUARE_E1 = CHESS_FILE_E | CHESS_RANK_1 << 4U ,
  CHESS_SQUARE_F1 = CHESS_FILE_F | CHESS_RANK_1 << 4U ,
  CHESS_SQUARE_G1 = CHESS_FILE_G | CHESS_RANK_1 << 4U ,
  CHESS_SQUARE_H1 = CHESS_FILE_H | CHESS_RANK_1 << 4U ,
  CHESS_SQUARE_A2 = CHESS_FILE_A | CHESS_RANK_2 << 4U ,
  CHESS_SQUARE_B2 = CHESS_FILE_B | CHESS_RANK_2 << 4U ,
  CHESS_SQUARE_C2 = CHESS_FILE_C | CHESS_RANK_2 << 4U ,
  CHESS_SQUARE_D2 = CHESS_FILE_D | CHESS_RANK_2 << 4U ,
  CHESS_SQUARE_E2 = CHESS_FILE_E | CHESS_RANK_2 << 4U ,
  CHESS_SQUARE_F2 = CHESS_FILE_F | CHESS_RANK_2 << 4U ,
  CHESS_SQUARE_G2 = CHESS_FILE_G | CHESS_RANK_2 << 4U ,
  CHESS_SQUARE_H2 = CHESS_FILE_H | CHESS_RANK_2 << 4U ,
  CHESS_SQUARE_A3 = CHESS_FILE_A | CHESS_RANK_3 << 4U ,
  CHESS_SQUARE_B3 = CHESS_FILE_B | CHESS_RANK_3 << 4U ,
  CHESS_SQUARE_C3 = CHESS_FILE_C | CHESS_RANK_3 << 4U ,
  CHESS_SQUARE_D3 = CHESS_FILE_D | CHESS_RANK_3 << 4U ,
  CHESS_SQUARE_E3 = CHESS_FILE_E | CHESS_RANK_3 << 4U ,
  CHESS_SQUARE_F3 = CHESS_FILE_F | CHESS_RANK_3 << 4U ,
  CHESS_SQUARE_G3 = CHESS_FILE_G | CHESS_RANK_3 << 4U ,
  CHESS_SQUARE_H3 = CHESS_FILE_H | CHESS_RANK_3 << 4U ,
  CHESS_SQUARE_A4 = CHESS_FILE_A | CHESS_RANK_4 << 4U ,
  CHESS_SQUARE_B4 = CHESS_FILE_B | CHESS_RANK_4 << 4U ,
  CHESS_SQUARE_C4 = CHESS_FILE_C | CHESS_RANK_4 << 4U ,
  CHESS_SQUARE_D4 = CHESS_FILE_D | CHESS_RANK_4 << 4U ,
  CHESS_SQUARE_E4 = CHESS_FILE_E | CHESS_RANK_4 << 4U ,
  CHESS_SQUARE_F4 = CHESS_FILE_F | CHESS_RANK_4 << 4U ,
  CHESS_SQUARE_G4 = CHESS_FILE_G | CHESS_RANK_4 << 4U ,
  CHESS_SQUARE_H4 = CHESS_FILE_H | CHESS_RANK_4 << 4U ,
  CHESS_SQUARE_A5 = CHESS_FILE_A | CHESS_RANK_5 << 4U ,
  CHESS_SQUARE_B5 = CHESS_FILE_B | CHESS_RANK_5 << 4U ,
  CHESS_SQUARE_C5 = CHESS_FILE_C | CHESS_RANK_5 << 4U ,
  CHESS_SQUARE_D5 = CHESS_FILE_D | CHESS_RANK_5 << 4U ,
  CHESS_SQUARE_E5 = CHESS_FILE_E | CHESS_RANK_5 << 4U ,
  CHESS_SQUARE_F5 = CHESS_FILE_F | CHESS_RANK_5 << 4U ,
  CHESS_SQUARE_G5 = CHESS_FILE_G | CHESS_RANK_5 << 4U ,
  CHESS_SQUARE_H5 = CHESS_FILE_H | CHESS_RANK_5 << 4U ,
  CHESS_SQUARE_A6 = CHESS_FILE_A | CHESS_RANK_6 << 4U ,
  CHESS_SQUARE_B6 = CHESS_FILE_B | CHESS_RANK_6 << 4U ,
  CHESS_SQUARE_C6 = CHESS_FILE_C | CHESS_RANK_6 << 4U ,
  CHESS_SQUARE_D6 = CHESS_FILE_D | CHESS_RANK_6 << 4U ,
  CHESS_SQUARE_E6 = CHESS_FILE_E | CHESS_RANK_6 << 4U ,
  CHESS_SQUARE_F6 = CHESS_FILE_F | CHESS_RANK_6 << 4U ,
  CHESS_SQUARE_G6 = CHESS_FILE_G | CHESS_RANK_6 << 4U ,
  CHESS_SQUARE_H6 = CHESS_FILE_H | CHESS_RANK_6 << 4U ,
  CHESS_SQUARE_A7 = CHESS_FILE_A | CHESS_RANK_7 << 4U ,
  CHESS_SQUARE_B7 = CHESS_FILE_B | CHESS_RANK_7 << 4U ,
  CHESS_SQUARE_C7 = CHESS_FILE_C | CHESS_RANK_7 << 4U ,
  CHESS_SQUARE_D7 = CHESS_FILE_D | CHESS_RANK_7 << 4U ,
  CHESS_SQUARE_E7 = CHESS_FILE_E | CHESS_RANK_7 << 4U ,
  CHESS_SQUARE_F7 = CHESS_FILE_F | CHESS_RANK_7 << 4U ,
  CHESS_SQUARE_G7 = CHESS_FILE_G | CHESS_RANK_7 << 4U ,
  CHESS_SQUARE_H7 = CHESS_FILE_H | CHESS_RANK_7 << 4U ,
  CHESS_SQUARE_A8 = CHESS_FILE_A | CHESS_RANK_8 << 4U ,
  CHESS_SQUARE_B8 = CHESS_FILE_B | CHESS_RANK_8 << 4U ,
  CHESS_SQUARE_C8 = CHESS_FILE_C | CHESS_RANK_8 << 4U ,
  CHESS_SQUARE_D8 = CHESS_FILE_D | CHESS_RANK_8 << 4U ,
  CHESS_SQUARE_E8 = CHESS_FILE_E | CHESS_RANK_8 << 4U ,
  CHESS_SQUARE_F8 = CHESS_FILE_F | CHESS_RANK_8 << 4U ,
  CHESS_SQUARE_G8 = CHESS_FILE_G | CHESS_RANK_8 << 4U ,
  CHESS_SQUARE_H8 = CHESS_FILE_H | CHESS_RANK_8 << 4U ,
  CHESS_SQUARE_NONE = CHESS_FILE_NONE | CHESS_RANK_NONE << 4U
}
 Represents the squares on a chess board, from A1 to H8. More...
 

Functions

void chess_square_debug (ChessSquare square)
 Prints a debug representation of the given square.
 
bool chess_square_is_valid (ChessSquare square)
 Checks if the given square is valid (A1-H8).
 
ChessSquare chess_square_new (ChessFile file, ChessRank rank)
 Creates a new square from a file and rank.
 
ChessFile chess_square_file (ChessSquare square)
 Gets the file of the given square.
 
ChessRank chess_square_rank (ChessSquare square)
 Gets the rank of the given square.
 
ChessColor chess_square_color (ChessSquare square)
 Gets the color of the given square.
 
size_t chess_square_from_algebraic (ChessSquare *square, const char *string)
 Parses a square from algebraic notation (e.g., "a1", "b1", ..., "h8").
 
size_t chess_square_to_algebraic (ChessSquare square, char *string, size_t string_size)
 Converts a square to algebraic notation (e.g., "a1", "b1", ..., "h8").
 
bool chess_square_is_attacked (const ChessPosition *position, ChessSquare square, ChessColor color)
 Checks if the given square is attacked by a given color in a position.
 

Detailed Description

Defines the chess square type and related utility functions for representing and manipulating chess squares.

Author
Tarek Saeed
Date
2025-06-14

Enumeration Type Documentation

◆ ChessSquare

enum ChessSquare : uint8_t

Represents the squares on a chess board, from A1 to H8.

Enumerator
CHESS_SQUARE_A1 

Square A1

CHESS_SQUARE_B1 

Square B1

CHESS_SQUARE_C1 

Square C1

CHESS_SQUARE_D1 

Square D1

CHESS_SQUARE_E1 

Square E1

CHESS_SQUARE_F1 

Square F1

CHESS_SQUARE_G1 

Square G1

CHESS_SQUARE_H1 

Square H1

CHESS_SQUARE_A2 

Square A2

CHESS_SQUARE_B2 

Square B2

CHESS_SQUARE_C2 

Square C2

CHESS_SQUARE_D2 

Square D2

CHESS_SQUARE_E2 

Square E2

CHESS_SQUARE_F2 

Square F2

CHESS_SQUARE_G2 

Square G2

CHESS_SQUARE_H2 

Square H2

CHESS_SQUARE_A3 

Square A3

CHESS_SQUARE_B3 

Square B3

CHESS_SQUARE_C3 

Square C3

CHESS_SQUARE_D3 

Square D3

CHESS_SQUARE_E3 

Square E3

CHESS_SQUARE_F3 

Square F3

CHESS_SQUARE_G3 

Square G3

CHESS_SQUARE_H3 

Square H3

CHESS_SQUARE_A4 

Square A4

CHESS_SQUARE_B4 

Square B4

CHESS_SQUARE_C4 

Square C4

CHESS_SQUARE_D4 

Square D4

CHESS_SQUARE_E4 

Square E4

CHESS_SQUARE_F4 

Square F4

CHESS_SQUARE_G4 

Square G4

CHESS_SQUARE_H4 

Square H4

CHESS_SQUARE_A5 

Square A5

CHESS_SQUARE_B5 

Square B5

CHESS_SQUARE_C5 

Square C5

CHESS_SQUARE_D5 

Square D5

CHESS_SQUARE_E5 

Square E5

CHESS_SQUARE_F5 

Square F5

CHESS_SQUARE_G5 

Square G5

CHESS_SQUARE_H5 

Square H5

CHESS_SQUARE_A6 

Square A6

CHESS_SQUARE_B6 

Square B6

CHESS_SQUARE_C6 

Square C6

CHESS_SQUARE_D6 

Square D6

CHESS_SQUARE_E6 

Square E6

CHESS_SQUARE_F6 

Square F6

CHESS_SQUARE_G6 

Square G6

CHESS_SQUARE_H6 

Square H6

CHESS_SQUARE_A7 

Square A7

CHESS_SQUARE_B7 

Square B7

CHESS_SQUARE_C7 

Square C7

CHESS_SQUARE_D7 

Square D7

CHESS_SQUARE_E7 

Square E7

CHESS_SQUARE_F7 

Square F7

CHESS_SQUARE_G7 

Square G7

CHESS_SQUARE_H7 

Square H7

CHESS_SQUARE_A8 

Square A8

CHESS_SQUARE_B8 

Square B8

CHESS_SQUARE_C8 

Square C8

CHESS_SQUARE_D8 

Square D8

CHESS_SQUARE_E8 

Square E8

CHESS_SQUARE_F8 

Square F8

CHESS_SQUARE_G8 

Square G8

CHESS_SQUARE_H8 

Square H8

CHESS_SQUARE_NONE 

None square

Function Documentation

◆ chess_square_color()

ChessColor chess_square_color ( ChessSquare  square)

Gets the color of the given square.

Parameters
squareThe square.
Returns
The color of the square.

◆ chess_square_debug()

void chess_square_debug ( ChessSquare  square)

Prints a debug representation of the given square.

Parameters
[in]squareThe square to print.

◆ chess_square_file()

ChessFile chess_square_file ( ChessSquare  square)

Gets the file of the given square.

Parameters
squareThe square.
Returns
The file of the square.

◆ chess_square_from_algebraic()

size_t chess_square_from_algebraic ( ChessSquare square,
const char *  string 
)

Parses a square from algebraic notation (e.g., "a1", "b1", ..., "h8").

Parameters
[out]squarePointer to store the parsed square.
[in]stringThe string containing the algebraic notation.
Returns
The number of characters read if successful, 0 otherwise.

◆ chess_square_is_attacked()

bool chess_square_is_attacked ( const ChessPosition position,
ChessSquare  square,
ChessColor  color 
)

Checks if the given square is attacked by a given color in a position.

Parameters
[in]positionThe chess position.
[in]squareThe square to check.
[in]colorThe color to check for attacks.
Returns
true if the square is attacked, false otherwise.

◆ chess_square_is_valid()

bool chess_square_is_valid ( ChessSquare  square)

Checks if the given square is valid (A1-H8).

Parameters
[in]squareThe square to check.
Returns
true if the square is valid, false otherwise.

◆ chess_square_new()

ChessSquare chess_square_new ( ChessFile  file,
ChessRank  rank 
)

Creates a new square from a file and rank.

Parameters
[in]fileThe file of the square.
[in]rankThe rank of the square.
Returns
The created square.

◆ chess_square_rank()

ChessRank chess_square_rank ( ChessSquare  square)

Gets the rank of the given square.

Parameters
squareThe square.
Returns
The rank of the square.

◆ chess_square_to_algebraic()

size_t chess_square_to_algebraic ( ChessSquare  square,
char *  string,
size_t  string_size 
)

Converts a square to algebraic notation (e.g., "a1", "b1", ..., "h8").

Parameters
[in]squareThe square to convert.
[out]stringThe buffer to store the algebraic notation.
[in]string_sizeThe size of the output buffer.
Returns
The number of characters written.