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
+130 -32
View File
@@ -1,4 +1,5 @@
#include <stdio.h>
#include <stdlib.h>
#include "chess.h"
#include "util.h"
@@ -79,33 +80,59 @@ pos_t pos_multiply_scalar(pos_t a, int b) {
return (pos_t){a.x*b, a.y*b};
}
// check whether a position is out of bounds of the board
bool is_pos_oob(pos_t pos) {
return pos.x < 0 || pos.x > BOARD_HORIZONTAL ||
pos.y < 0 || pos.y > BOARD_VERTICAL;
}
/**
* check the wanted pos_t against all valid move options
* of the current piece
*/
bool is_move_legal(move_t move) {
pos_t new_pos = {
move.from.x+move.to.x,
move.from.y+move.to.y
};
// OOB check
if (is_pos_oob(new_pos)) {
return false;
}
valid_move_t valid_moves[PIECE_COUNT] = DEFAULT_VALID_MOVES;
switch (move.piece) {
case NONE:
case PAWN:
case KNIGHT:
case KING:
case WPAWN:
case WKNIGHT:
case WKING:
case BPAWN:
case BKNIGHT:
case BKING:
for (int i = 0; i < PIECE_COUNT; i++) {
for (int j = 0;
j < valid_moves[i].piece_move_count &&
move.piece == valid_moves[i].piece;
j++
) {
pos_t new_pos = {move.from.x+move.to.x, move.from.y+move.to.y};
if (compare_pos(new_pos, valid_moves[i].options[j]) == true) {
if (
compare_pos(
new_pos,
valid_moves[i].options[j]
)
) {
return true;
}
}
}
break;
case ROOK:
case BISHOP:
case QUEEN:
case WROOK:
case WBISHOP:
case WQUEEN:
case BROOK:
case BBISHOP:
case BQUEEN:
for (int i = 0; i < PIECE_COUNT; i++) {
for (int j = 0;
j < valid_moves[i].piece_move_count &&
@@ -114,15 +141,13 @@ bool is_move_legal(move_t move) {
) {
/** NOTE: BOARD_HORIZONTAL assuming HORIZONTAL & VERTICAL are the same */
for (int k = 0; k < BOARD_HORIZONTAL; k++) {
pos_t new_pos = {
move.from.x+move.to.x,
move.from.y+move.to.y
};
if (compare_pos(
new_pos,
pos_multiply_scalar(valid_moves[i].options[j], k)
) == true) {
return true;
if (
compare_pos(
new_pos,
pos_multiply_scalar(valid_moves[i].options[j], k)
)
) {
return true;
}
}
}
@@ -133,22 +158,36 @@ bool is_move_legal(move_t move) {
return false;
}
bool is_none(board_t board, pos_t pos) {
bool is_dest_none(board_t board, pos_t pos) {
return board[pos.y][pos.x] == NONE;
}
bool is_dest_opponent(board_t board, player_e player, move_t move) {
switch (player) {
case WHITE:
return board[move.to.y][move.to.x] < 0;
break;
case BLACK:
return board[move.to.y][move.to.x] > 0;
break;
}
}
/** the move can be:
* 1. legal x illegal
* 2. the 'to' pos could be occupied x non-occupied
* 2a. it could be capturable x non-capturable
* 2a. the 'to' pos could be occupied x non-occupied
* 2b. it could be capturable x non-capturable
* 2c. it could be out of bounds
*/
bool validate_move(board_t board, move_t move) {
bool none = is_none(board, move.to);
bool validate_move(board_t board, player_e player, move_t move) {
bool none = is_dest_none(board, move.to);
bool dest_is_opponent = is_dest_opponent(board, player, move);
bool move_legal = is_move_legal(move);
if (none == true && move_legal == true) {
if (none && move_legal) {
// free ability to move
return true;
} else if (none == false && move_legal == true) {
} else if (!none && dest_is_opponent && move_legal) {
// capturing
return true;
} else {
@@ -157,18 +196,76 @@ bool validate_move(board_t board, move_t move) {
}
}
/** TODO: capturing and promotions */
bool perform_move(board_t *board, player_e player, move_t move) {
bool move_valid = validate_move(*board, move);
if (move_valid == true) {
/**
* checks and sorts out a capture edge case of a move
* sorts out = moves the piece into the 'captured' area and puts a NONE in its' place
* (if the 'captured' piece is not a king, because you cannot capture a king)
*/
bool check_captures(board_t *board, player_e player, captured_t *captured, move_t move) {
if (is_dest_opponent(*board, player, move)) {
switch (player) {
case WHITE:
captured->white[captured->white_n++] = abs(*board[move.to.y][move.to.x]);
*board[move.to.y][move.to.x] = NONE;
break;
case BLACK:
captured->white[captured->white_n++] = abs(*board[move.to.y][move.to.x]);
*board[move.to.y][move.to.x] = NONE;
break;
}
return true;
} else {
return false;
}
}
// checks and sorts out a promotion edge case of a move
bool check_promotions(board_t *board, player_e player, move_t *move) {
pos_t new_pos = {
move->from.x+move->to.x,
move->from.y+move->to.y
};
// white player achieved a promotion
if (new_pos.y < 0 && player == WHITE && move->piece == WPAWN) {
*board[move->from.y][move->from.x] = WQUEEN;
move->piece = WQUEEN;
return true;
}
// black player achieved a promotion
if (new_pos.y > BOARD_VERTICAL && player == BLACK && move->piece == BPAWN) {
*board[move->from.y][move->from.x] = BQUEEN;
move->piece = BQUEEN;
return true;
}
return false;
}
// checks for mates
/** FIXME: unimplemented */
end_condition_e check_end() {
return CONTINUE;
}
end_condition_e perform_move(board_t *board, player_e player, captured_t *captured, move_t move) {
if (validate_move(*board, player, move)) {
if (
*board[move.to.y][move.to.x] != WKING || // accounting for both players
*board[move.to.y][move.to.x] != BKING // you cannot capture a king
) {
check_captures(board, player, captured, move);
}
check_promotions(board, player, &move);
*board[move.to.y][move.to.x] = move.piece; // move piece to wanted place
*board[move.from.y][move.from.x] = NONE; // erase the remnants
}
return move_valid;
}
end_condition_e check_edge_cases(board_t board) {
return CHECKMATE;
return check_end();
}
void announce_results(end_condition_e end_condition, player_e player) {
@@ -181,6 +278,7 @@ void announce_results(end_condition_e end_condition, player_e player) {
case STALEMATE:
printf("STALEMATE");
break;
case CONTINUE: break; // to make the linter happy
}
printf("\nand the player who has won is");