check captures + promotions done?

This commit is contained in:
Odweta
2026-02-18 16:51:14 +01:00
parent 44c38667a4
commit de55c98be3
3 changed files with 209 additions and 62 deletions
+77 -27
View File
@@ -19,18 +19,28 @@
#define BOARD_VERTICAL 8
// type abstractions
typedef int board_t[BOARD_HORIZONTAL][BOARD_VERTICAL];
typedef enum { WHITE, BLACK } player_e;
#define PIECE_COUNT 7 // how many unique pieces are there
typedef int board_t[BOARD_VERTICAL][BOARD_HORIZONTAL];
#define PIECE_COUNT 13 // how many unique pieces are there
#define MAX_PIECE_MOVE_COUNT 27 // the max possible num of options where to move in one moment
typedef enum {
NONE,
PAWN,
ROOK,
KNIGHT,
BISHOP,
QUEEN,
KING
WPAWN,
WROOK,
WKNIGHT,
WBISHOP,
WQUEEN,
WKING,
BPAWN,
BROOK,
BKNIGHT,
BBISHOP,
BQUEEN,
BKING
} piece_e;
typedef struct {
@@ -38,8 +48,6 @@ typedef struct {
int y;
} pos_t;
typedef enum { WHITE, BLACK } player_e;
typedef struct {
// what
// where from
@@ -50,15 +58,17 @@ typedef struct {
} move_t;
// the default arrangment of pieces on the board
// negative numbers are black, positive ones white
#define BOARD_DEFAULT_CONFIG \
(board_t){\
{2,3,4,5,6,4,3,2},\
{1,1,1,1,1,1,1,1},\
{0,0,0,0,0,0,0,0},\
{0,0,0,0,0,0,0,0},\
{0,0,0,0,0,0,0,0},\
{1,1,1,1,1,1,1,1},\
{2,3,4,5,6,4,3,2}\
{ BROOK, BKNIGHT, BBISHOP, BQUEEN, BKING, BBISHOP, BKNIGHT, BROOK },\
{ BPAWN, BPAWN, BPAWN, BPAWN, BPAWN, BPAWN, BPAWN, BPAWN },\
{ NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE },\
{ NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE },\
{ NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE },\
{ NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE },\
{ WPAWN, WPAWN, WPAWN, WPAWN, WPAWN, WPAWN, WPAWN, WPAWN },\
{ WROOK, WKNIGHT, WBISHOP, WQUEEN, WKING, WBISHOP, WKNIGHT, WROOK }\
}
typedef struct {
@@ -87,34 +97,77 @@ typedef struct {
*/
#define DEFAULT_VALID_MOVES (valid_move_t[PIECE_COUNT]){\
{NONE, {}, 0},\
{PAWN, {\
\
{WPAWN, {\
{ 0, -1}, {-1, -1}, {+1, -1}, { 0, -2}\
}, 4},\
{ROOK, {\
{WROOK, {\
{ 0, -1}, { 0, +1}, {-1, 0}, {+1, 0}\
}, 4},\
{KNIGHT, {\
{WKNIGHT, {\
{-1, -2}, {+1, -2}, {-2, -1}, {-2, +1},\
{+2, -1}, {+2, +1}, {-1, -2}, {-1, +2}\
}, 8},\
{BISHOP, {\
{WBISHOP, {\
{-1, -1}, {-1, +1}, {+1, -1}, {+1, +1}\
}, 4},\
{QUEEN, {\
{WQUEEN, {\
{0, -1}, {0, +1}, {-1, 0}, {+1, 0},\
{-1, -1}, {-1, +1}, {+1, -1}, {+1, +1}\
}, 8},\
{KING, {\
{WKING, {\
{ 0, -1}, { 0, +1}, {-1, 0}, {+1, 0},\
{-1, -1}, {-1, +1}, {+1, -1}, {+1, +1}\
}, 8},\
\
{BPAWN, {\
{ 0, -1}, {-1, -1}, {+1, -1}, { 0, -2}\
}, 4},\
{BROOK, {\
{ 0, -1}, { 0, +1}, {-1, 0}, {+1, 0}\
}, 4},\
{BKNIGHT, {\
{-1, -2}, {+1, -2}, {-2, -1}, {-2, +1},\
{+2, -1}, {+2, +1}, {-1, -2}, {-1, +2}\
}, 8},\
{BBISHOP, {\
{-1, -1}, {-1, +1}, {+1, -1}, {+1, +1}\
}, 4},\
{BQUEEN, {\
{0, -1}, {0, +1}, {-1, 0}, {+1, 0},\
{-1, -1}, {-1, +1}, {+1, -1}, {+1, +1}\
}, 8},\
{BKING, {\
{ 0, -1}, { 0, +1}, {-1, 0}, {+1, 0},\
{-1, -1}, {-1, +1}, {+1, -1}, {+1, +1}\
}, 8}\
}
/**
* states in which the game can end
* CONTINUE => the game does not end, it continues
* CHECKMATE => one side wins
* STALEMATE => it's a draw
*/
typedef enum {
CONTINUE,
CHECKMATE,
STALEMATE
} end_condition_e;
/**
* a tracker for which pieces have been captured by who
* white contains opponent pieces WHITE have been captured by them
* black contains opponent pieces BLACK have been captured by them
*/
#define PIECE_COUNT_CAPTURABLE_PER_PLAYER 15 // 2*8 - 1
typedef struct {
piece_e white[PIECE_COUNT_CAPTURABLE_PER_PLAYER];
unsigned int white_n;
piece_e black[PIECE_COUNT_CAPTURABLE_PER_PLAYER];
unsigned int black_n;
} captured_t;
// initializes the board with the default configuration of pieces
void init_board(board_t *board);
@@ -126,10 +179,7 @@ void select_player(player_e *player, unsigned int round_count);
bool get_move(move_t *move);
// performs the move if is it possible
bool perform_move(board_t *board, player_e player, move_t move);
// checks for captures, prommotions and mates
end_condition_e check_edge_cases(board_t board);
end_condition_e perform_move(board_t *board, player_e player, captured_t *captured, move_t move);
// if there is one of the mates, end the game and show who has won
void announce_results(end_condition_e end_condition, player_e player);