Skip to content

Commit

Permalink
Fix bug in king attacking detection
Browse files Browse the repository at this point in the history
A bug was introduced allowing kings to attack more than one square away
  • Loading branch information
camc committed Feb 1, 2023
1 parent 2e0b9de commit 0a18e1d
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# A chess game & engine

## Features
-
- Computer move generation
- minimax (negamax), alpha-beta pruning & iterative deepening
- Multithreaded search (Lazy SMP) on platforms supporting C11 threads
Expand Down
7 changes: 5 additions & 2 deletions src/engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -990,8 +990,8 @@ bool is_piece_attacked(struct GameState *state, struct BoardPos attackee_pos, en
const struct BoardPos translation = PIECE_MOVE_DIRECTIONS[Queen - 1][i];

const bool is_diagonal = abs(translation.file) == abs(translation.rank);
const bool is_king = abs(translation.file) <= 1 && abs(translation.rank) <= 1;

bool is_king = true;
struct BoardPos check = boardpos_add(attackee_pos, translation);
while (!boardpos_eq(check, NULL_BOARDPOS)) {
struct Piece check_piece = get_piece(state, check);
Expand All @@ -1011,6 +1011,9 @@ bool is_piece_attacked(struct GameState *state, struct BoardPos attackee_pos, en
}
}

// Kings can only move one square.
is_king = false;

// Queens, rooks and bishops can attack over multiple squares in the same direction, so the
// check continues for every square in that direction. Overflow is handled by boardpos_add,
// NULL_BOARDPOS will be returned if the add overflows.
Expand Down

0 comments on commit 0a18e1d

Please sign in to comment.