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
+31 -29
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;
int _y_from, _y_to;
printf("move >> ");
@@ -67,6 +67,8 @@ bool get_move(move_t *move) {
move->to.x = _x_to-96-1;
move->to.y = _y_to-1;
move->piece = board[move->from.y][move->from.x];
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};
}
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
bool is_pos_oob(pos_t pos) {
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
* of the current piece
*/
/** TODO: add roundcount check for PAWN */
bool is_move_legal(move_t move) {
pos_t new_pos = move.to;
@@ -105,20 +112,16 @@ bool is_move_legal(move_t move) {
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++
for (int j = 0; j < valid_moves[move.piece].piece_move_count; j++) {
pos_t candidate = pos_add(move.from, valid_moves[move.piece].options[j]);
printf("cadidate {%d,%d}\n", candidate.x, candidate.y);
if (
compare_pos(
new_pos,
candidate
)
) {
if (
compare_pos(
new_pos,
valid_moves[i].options[j]
)
) {
return true;
}
return true;
}
}
break;
@@ -128,22 +131,21 @@ bool is_move_legal(move_t move) {
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 &&
move.piece == valid_moves[i].piece;
j++
) {
/** NOTE: BOARD_HORIZONTAL assuming HORIZONTAL & VERTICAL are the same */
for (int k = 0; k < BOARD_HORIZONTAL; k++) {
if (
compare_pos(
new_pos,
pos_multiply_scalar(valid_moves[i].options[j], k)
for (int j = 0; j < valid_moves[move.piece].piece_move_count; j++) {
/** NOTE: BOARD_HORIZONTAL assuming HORIZONTAL & VERTICAL are the same */
for (int k = 0; k < BOARD_HORIZONTAL; k++) {
if (
compare_pos(
new_pos,
pos_multiply_scalar(
pos_add(
move.from,
valid_moves[move.piece].options[j]
), k
)
) {
return true;
}
)
) {
return true;
}
}
}
+1 -1
View File
@@ -176,7 +176,7 @@ void init_board(board_t board);
void select_player(player_e *player, unsigned int round_count);
// 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
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);
get_move(&move);
get_move(board, &move);
end_condition = perform_move(&board, player, &captured, move);
if (end_condition == CHECKMATE || end_condition == STALEMATE) {