Knight Pattern
Home * Board Representation * Bitboards * Knight Pattern
M. C. Escher, Horseman [1] Knight Pattern with Bitboards covers knight attacks of single or multiple knights, either by indexed pre-calculated tables or direct bitboard calculation, and the set wise determination of Knight fork target squares.
by Lookup
The knight is specified by square index, likely from a bitscan of a piece-wise bitboard serialization of a knight bitboard from a standard board-definition, to index a table of pre-calculated knight-attacks:
For instance a knight on d4
by Calculation
Similar to one step only of the four orthogonal and four diagonal directions,each of the eight knight directions is calculated by left or right shift with appropriate pre- or post shift mask, to avoid A- H-file wraps or vice versa. See also AVX2 Knight Attacks.
In almost the same manner as the three pawn directions, there is a unique source-target relationship. The difference is - we have up to eight pawns, but likely not more than two knights per side. Keeping eight disjoint knight directions is consistent to direction-wise fill approaches of other pieces with unique target- source relationship - but disjoint direction-wise knight targets are sparse populated and usually contain only up to two bits.
Multiple Knight Attacks
To initialize the KnightAttacks array one may use a routine with some kind of parallel prefix calculations, rather than the union of all eight directions:
or to possibly gain some more parallelism:
If we pass multiple knights set-wise, attacks of some squares may be caused by different knights. Feeding back (safe) target sets, the routine may used to get sets of squares, knights may reach in two or more moves. For instance in late pawn-knight endings whether a knight may catch a passer.
Knight Fill
A fill cycle for a fill algorithm is the union of the attack set with the knights itself:
for instance applied six times on the otherwise empty board:
Knight Forks
A common knight pattern is the knight fork. Targets are heavy pieces - king, queen and rooks, hanging pieces, or even undefended pawns. A royal knight fork or “family” check, winning the queen is most important. Otherwise one may loop over all possible pieces, to get the knight-attacks by lookup and to intersect all combinations of attack-squares. A loop- and branch-less solution to get potential fork-attack squares is to union all intersections of all direction attacks, as explained in greater one sets:
The intersection of those targets with squares not occupied by own pieces or attacked by opponent pawns and knights, but attacked by own knight(s) leaves a move target set with some forced properties.
See also
Selected Publications
- Martin Gardner (1967). Problems that are Built on the Knight’s Tour in Chess. Scientific American, Vol. 130
- Noam D. Elkies, Richard P. Stanley (2003). The Mathematical Knight. The Mathematical Intelligencer, Vol. 25, No. 1, pdf
- Ben Hill (2004). Knight’s Tours. pdf
- Philip Hingston, Graham Kendall (2005). Ant Colonies Discover Knight’s Tours. AI 2004, Lecture Notes in Computer Science, Vol. 3339
- Philip Hingston, Graham Kendall (2005). Enumerating Knight’s Tours using an Ant Colony Algorithm. CEC 2005, pdf
Forum Posts
- Symbolic: From bitboards to ideas by Steven Edwards, CCC, March 13, 2004 » Symbolic
- Problem with bitboard knight attack generator by ZirconiumX, CCC, December 21, 2011
- Knight fork threats by Stefan Geschwentner, FishCooking, November 17, 2014 » Knight Forks, Stockfish
- knight’s multiple atacks by Daniel Anulliero, CCC, January 27, 2015
External Links
- Knight’s tour from Wikipedia
- Knight’s Tour Notes compiled by George Jelliss
- Knight’s Tour - from Wolfram MathWorld
- Longest uncrossed knight’s path from Wikipedia
- László Lindner’s knight wheel by Frederic Friedel from ChessBase Puzzle
- Knight’s Tour from Research Reflections by Graham Kendall, January 18, 2014
- Knight’s Tour - Numberphile, YouTube Video