GET MOVE WORKS

This commit is contained in:
Odweta
2026-02-18 23:45:06 +01:00
parent 465398f210
commit 71e1f16fac
3 changed files with 33 additions and 31 deletions
+19 -17
View File
@@ -50,7 +50,7 @@ bool is_move_in_bounds(move_t move) {
} }
} }
bool get_move(move_t *move) { bool get_move(board_t board, move_t *move) {
char _x_from, _x_to; char _x_from, _x_to;
int _y_from, _y_to; int _y_from, _y_to;
printf("move >> "); printf("move >> ");
@@ -67,6 +67,8 @@ bool get_move(move_t *move) {
move->to.x = _x_to-96-1; move->to.x = _x_to-96-1;
move->to.y = _y_to-1; move->to.y = _y_to-1;
move->piece = board[move->from.y][move->from.x];
return is_move_in_bounds(*move); return is_move_in_bounds(*move);
} }
@@ -78,6 +80,10 @@ pos_t pos_multiply_scalar(pos_t a, int b) {
return (pos_t){a.x*b, a.y*b}; return (pos_t){a.x*b, a.y*b};
} }
pos_t pos_add(pos_t a, pos_t b) {
return (pos_t){a.x + b.x, a.y + b.y};
}
// check whether a position is out of bounds of the board // check whether a position is out of bounds of the board
bool is_pos_oob(pos_t pos) { bool is_pos_oob(pos_t pos) {
return pos.x < 0 || pos.x > BOARD_HORIZONTAL || return pos.x < 0 || pos.x > BOARD_HORIZONTAL ||
@@ -88,6 +94,7 @@ bool is_pos_oob(pos_t pos) {
* check the wanted pos_t against all valid move options * check the wanted pos_t against all valid move options
* of the current piece * of the current piece
*/ */
/** TODO: add roundcount check for PAWN */
bool is_move_legal(move_t move) { bool is_move_legal(move_t move) {
pos_t new_pos = move.to; pos_t new_pos = move.to;
@@ -105,22 +112,18 @@ bool is_move_legal(move_t move) {
case BPAWN: case BPAWN:
case BKNIGHT: case BKNIGHT:
case BKING: case BKING:
for (int i = 0; i < PIECE_COUNT; i++) { for (int j = 0; j < valid_moves[move.piece].piece_move_count; j++) {
for (int j = 0; pos_t candidate = pos_add(move.from, valid_moves[move.piece].options[j]);
j < valid_moves[i].piece_move_count && printf("cadidate {%d,%d}\n", candidate.x, candidate.y);
move.piece == valid_moves[i].piece;
j++
) {
if ( if (
compare_pos( compare_pos(
new_pos, new_pos,
valid_moves[i].options[j] candidate
) )
) { ) {
return true; return true;
} }
} }
}
break; break;
case WROOK: case WROOK:
case WBISHOP: case WBISHOP:
@@ -128,25 +131,24 @@ bool is_move_legal(move_t move) {
case BROOK: case BROOK:
case BBISHOP: case BBISHOP:
case BQUEEN: case BQUEEN:
for (int i = 0; i < PIECE_COUNT; i++) { for (int j = 0; j < valid_moves[move.piece].piece_move_count; j++) {
for (int j = 0;
j < valid_moves[i].piece_move_count &&
move.piece == valid_moves[i].piece;
j++
) {
/** NOTE: BOARD_HORIZONTAL assuming HORIZONTAL & VERTICAL are the same */ /** NOTE: BOARD_HORIZONTAL assuming HORIZONTAL & VERTICAL are the same */
for (int k = 0; k < BOARD_HORIZONTAL; k++) { for (int k = 0; k < BOARD_HORIZONTAL; k++) {
if ( if (
compare_pos( compare_pos(
new_pos, new_pos,
pos_multiply_scalar(valid_moves[i].options[j], k) pos_multiply_scalar(
pos_add(
move.from,
valid_moves[move.piece].options[j]
), k
)
) )
) { ) {
return true; return true;
} }
} }
} }
}
break; break;
} }
+1 -1
View File
@@ -176,7 +176,7 @@ void init_board(board_t board);
void select_player(player_e *player, unsigned int round_count); void select_player(player_e *player, unsigned int round_count);
// listens for a command to make a move by the currently playing player // listens for a command to make a move by the currently playing player
bool get_move(move_t *move); bool get_move(board_t board, move_t *move);
// performs the move if is it possible // performs the move if is it possible
end_condition_e perform_move(board_t *board, player_e player, captured_t *captured, move_t move); end_condition_e perform_move(board_t *board, player_e player, captured_t *captured, move_t move);
+1 -1
View File
@@ -40,7 +40,7 @@ int main() {
show_board(board, player); show_board(board, player);
get_move(&move); get_move(board, &move);
end_condition = perform_move(&board, player, &captured, move); end_condition = perform_move(&board, player, &captured, move);
if (end_condition == CHECKMATE || end_condition == STALEMATE) { if (end_condition == CHECKMATE || end_condition == STALEMATE) {