Skip to content

Knight move generation

Sebastian Veile edited this page Jun 17, 2022 · 2 revisions

This page contains descriptions of the general logic behind finding possible moves for knights. This page will go into much less detail than the Pawn move generation page so it is recommended to read that first.

Prerequisites to follow along:

A knight can move in 8 different directions

          2       3
      1               4
              N
      8               5
          7       6

A knight can move in any of these directions as long as there is non of its own pieces on that square already.

Finding possible moves

Calculating each move is fairly simple and requires only 6 bitboards

White knight bitboard (Current) Own pieces bitboard not-A-file not-AB-file not-H-file not-HG-file
8  0  0  0  0  0  0  0  0
7  0  0  0  0  0  0  0  0
6  0  0  0  0  0  0  0  0
5  0  0  1  0  0  0  0  0
4  0  0  0  0  0  0  1  0
3  0  0  0  0  0  0  0  0
2  0  0  0  0  0  0  0  0
1  0  0  0  0  0  0  0  0
   A  B  C  D  E  D  G  H
8  0  0  0  0  0  0  0  0
7  0  0  0  0  0  0  0  0
6  0  0  0  0  0  0  0  0
5  0  0  1  0  0  0  0  0
4  0  0  0  0  0  0  1  0
3  0  0  0  1  0  0  0  0
2  0  1  0  0  1  0  1  0
1  0  0  0  0  1  0  0  0
   A  B  C  D  E  D  G  H
8  0  1  1  1  1  1  1  1
7  0  1  1  1  1  1  1  1
6  0  1  1  1  1  1  1  1
5  0  1  1  1  1  1  1  1
4  0  1  1  1  1  1  1  1
3  0  1  1  1  1  1  1  1
2  0  1  1  1  1  1  1  1
1  0  1  1  1  1  1  1  1
   A  B  C  D  E  D  G  H
8  0  0  1  1  1  1  1  1
7  0  0  1  1  1  1  1  1
6  0  0  1  1  1  1  1  1
5  0  0  1  1  1  1  1  1
4  0  0  1  1  1  1  1  1
3  0  0  1  1  1  1  1  1
2  0  0  1  1  1  1  1  1
1  0  0  1  1  1  1  1  1
   A  B  C  D  E  D  G  H
8  1  1  1  1  1  1  1  0
7  1  1  1  1  1  1  1  0
6  1  1  1  1  1  1  1  0
5  1  1  1  1  1  1  1  0
4  1  1  1  1  1  1  1  0
3  1  1  1  1  1  1  1  0
2  1  1  1  1  1  1  1  0
1  1  1  1  1  1  1  1  0
   A  B  C  D  E  D  G  H
8  1  1  1  1  1  1  0  0
7  1  1  1  1  1  1  0  0
6  1  1  1  1  1  1  0  0
5  1  1  1  1  1  1  0  0
4  1  1  1  1  1  1  0  0
3  1  1  1  1  1  1  0  0
2  1  1  1  1  1  1  0  0
1  1  1  1  1  1  1  0  0
   A  B  C  D  E  D  G  H

Calculating the moves of knights share the same problems as with pawns and kings. Moving outside the board is a real possibility and since the knight can move two squares in either direction, it can now occur on the H G A, and B files. To avoid moving outside of the board we utilize some helper bitboards not-H-file, not-HG-file, not-AB-file, and not-AB-file. If the knight is on A, we don't want to calculate positions to the left and if the knight is on H we do not want to calculate positions to the right (Since they will be off the board).

See the directions page to get an overview of the number of bitshifts to calculate a move in a specific direction.

This is how each single move is calculated:

  1. ((knight bitboard & not-AB-file) << 10)
  2. ((knight bitboard & not-A-file) << 17)
  3. ((knight bitboard & not-H-file) << 15)
  4. ((knight bitboard & not-HG-file) << 6)
  5. knight-so-e-e ((knight bitboard & not-HG-file) >>> 10)
  6. knight-so-so-e ((knight bitboard & not-H-file) >>> 17)
  7. knight-so-so-w ((knight bitboard & not-A-file) >>> 15)
  8. knight-so-w-w ((knight bitboard & not-AB-file) >>> 6)

Last but not least we need to make sure that there is not one of our own pieces on those squares. The way we can do that is by taking the inverse of Own pieces bitboard using the bitwise NOT operator '~' (~Own pieces bitboard)

Then we can use the bitwise and operator '&' to remove all moves where our own pieces already are located

Knight moves all directions ~Own pieces bitboard Knight moves
8  0  0  0  0  0  0  0  0
7  0  1  0  1  0  0  0  0
6  1  0  0  0  1  1  0  1
5  0  0  0  0  1  0  0  0
4  1  0  0  0  1  0  0  0
3  0  1  0  1  1  0  0  0
2  0  0  0  0  0  1  0  1
1  0  0  0  0  0  0  0  0
   A  B  C  D  E  D  G  H
&
8  1  1  1  1  1  1  1  1
7  1  1  1  1  1  1  1  1
6  1  1  1  1  1  1  1  1
5  1  1  0  1  1  1  1  1
4  1  1  1  1  1  1  0  1
3  1  1  1  0  1  1  1  1
2  1  0  1  1  0  1  0  1
1  1  1  1  1  0  1  1  1
   A  B  C  D  E  D  G  H
=
8  0  0  0  0  0  0  0  0
7  0  1  0  1  0  0  0  0
6  1  0  0  0  1  1  0  1
5  0  0  0  0  1  0  0  0
4  1  0  0  0  1  0  0  0
3  0  1  0  0  1  0  0  0
2  0  0  0  0  0  1  0  1
1  0  0  0  0  0  0  0  0
   A  B  C  D  E  D  G  H

Clone this wiki locally