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. || toc  =Knight Attacks= The Knight attacks the target squares independently from other pieces around. The compass rose of all eight attacking directions associated with the to - from square differences from an 8x8 board:
 * Home * Board Representation * Bitboards * Knight Pattern**
 * [[image:Horseman.jpg width="232" height="238" link="http://www.mcescher.com/Shopmain/ShopEU/facsilimeprints/prints.html"]] ||~  || **Knight Pattern**
 * M. C. Escher, Horseman ||~  ||^   ||

code noNoWe   noNoEa +15 +17             |     | noWeWe  +6 __|     |__+10  noEaEa \  /               >0<           __ /   \ __ soWeWe -10   |     |   -6  soEaEa |    |            -17  -15        soSoWe    soSoEa code 

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: code format="cpp" U64 arrKnightAttacks[64];

U64 knightAttacks(enumSquare sq) {return arrKnightAttacks[sq];} code For instance a knight on d4 code arrKnightAttacks[d4] . . . . . . . . . . . . . . . . . . 1 . 1 . . . . 1 . . . 1 . . . . . . . . . . . 1 . . . 1 . . . . 1 . 1 . . . . . . . . . . . code 

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. code format="cpp" U64 noNoEa(U64 b) {return (b << 17) & notAFile ;} U64 noEaEa(U64 b) {return (b << 10) & notABFile;} U64 soEaEa(U64 b) {return (b >> 6) & notABFile;} U64 soSoEa(U64 b) {return (b >> 15) & notAFile ;} U64 noNoWe(U64 b) {return (b << 15) & notHFile ;} U64 noWeWe(U64 b) {return (b << 6) & notGHFile;} U64 soWeWe(U64 b) {return (b >> 10) & notGHFile;} U64 soSoWe(U64 b) {return (b >> 17) & notHFile ;}

U64 noNoEa(U64 b) {return (b & notHFile ) << 17;} U64 noEaEa(U64 b) {return (b & notGHFile) << 10;} U64 soEaEa(U64 b) {return (b & notGHFile) >> 6;} U64 soSoEa(U64 b) {return (b & notHFile ) >> 15;} U64 noNoWe(U64 b) {return (b & notAFile ) << 15;} U64 noWeWe(U64 b) {return (b & notABFile) << 6;} U64 soWeWe(U64 b) {return (b & notABFile) >> 10;} U64 soSoWe(U64 b) {return (b & notAFile ) >> 17;} code 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: code format="cpp" U64 knightAttacks(U64 knights) { U64 west, east, attacks; east    = eastOne (knights); west    = westOne (knights); attacks = (east|west) << 16; attacks |= (east|west) >> 16; east    = eastOne (east); west    = westOne (west); attacks |= (east|west) << 8; attacks |= (east|west) >> 8; return attacks; } code or to possibly gain some more parallelism: code format="cpp" U64 knightAttacks(U64 knights) { U64 l1 = (knights >> 1) & C64(0x7f7f7f7f7f7f7f7f); U64 l2 = (knights >> 2) & C64(0x3f3f3f3f3f3f3f3f); U64 r1 = (knights << 1) & C64(0xfefefefefefefefe); U64 r2 = (knights << 2) & C64(0xfcfcfcfcfcfcfcfc); U64 h1 = l1 | r1; U64 h2 = l2 | r2; return (h1<<16) | (h1>>16) | (h2<<8) | (h2>>8); } code 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: code format="cpp" U64 knightFill(U64 knights) {return knightAttacks(knights) | knights;} code for instance applied six times on the otherwise empty board: code 1. Fill           2. Fill           3. Fill . . . . . . . .  . . . . . . . .   . . . . . . . .   . . . . . . . . . . . . . . . .   . . . . . . . .   . . . . . . . .   . 1 . 1 . . . . . . . . . . . .   . . . . . . . .   . . . . . . . .   1 . 1 . 1 . . . . . . . . . . .   . . . . . . . .   1 . 1 . . . . .   1 1 1 1 . 1 . . . . . . . . . .   . . . . . . . .   . 1 . 1 . . . .   1 1 1 1 1 . 1 . . . . . . . . .   . 1 . . . . . .   1 1 . . 1 . . .   1 1 . 1 1 1 . . . . . . . . . .   . . 1 . . . . .   . . 1 1 . . . .   1 . 1 1 1 . 1 . 1 . . . . . . .   1 . . . . . . .   1 . 1 . 1 . . .   1 1 1 1 1 1 ..

4. Fill           5. Fill           6. Fill . 1 . 1 . 1 . .  1 1 1 1 1 1 1 .   1 1 1 1 1 1 1 1                  1 1 1 1 1 . 1 .   1 1 1 1 1 1 1 1   1 1 1 1 1 1 1 1                  1 1 1 1 1 1 . 1   1 1 1 1 1 1 1 1   1 1 1 1 1 1 1 1                  1 1 1 1 1 1 1 .   1 1 1 1 1 1 1 1   1 1 1 1 1 1 1 1                  1 1 1 1 1 1 1 1   1 1 1 1 1 1 1 1   1 1 1 1 1 1 1 1                  1 1 1 1 1 1 1 .   1 1 1 1 1 1 1 1   1 1 1 1 1 1 1 1                  1 1 1 1 1 1 1 1   1 1 1 1 1 1 1 1   1 1 1 1 1 1 1 1                  1 1 1 1 1 1 1 .   1 1 1 1 1 1 1 1   1 1 1 1 1 1 1 1 code  =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: code format="cpp" U64 forkTargetSquare(U64 targets) { U64 west, east, attak, forks; east  = eastOne (targets); west  = westOne (targets); attak =  east << 16; forks = (west << 16) & attak; attak |= west << 16; forks |= (east >> 16) & attak; attak |= east >> 16; forks |= (west >> 16) & attak; attak |= west >> 16; east  = eastOne (east); west  = westOne (west); forks |= (east << 8) & attak; attak |= east <<  8; forks |= (west << 8) & attak; attak |= west <<  8; forks |= (east >> 8) & attak; attak |= east >>  8; forks |= (west >> 8) & attak; return forks; } code 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=
 * AVX2 Knight Attacks
 * Fill Algorithms
 * Knight-Distance

=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 3339]
 * [|Philip Hingston], Graham Kendall (**2005**). //[|Enumerating knight's tours using an ant colony algorithm]//. [|CEC 2005]

=Forum Posts=
 * [|Symbolic: From bitboards to ideas] by Steven Edwards, CCC, March 13, 2004 » Symbolic
 * [|knight's multiple atacks] by Daniel Anulliero, CCC, January 27, 2015

=External Links= > media type="custom" key="25086882"
 * [|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

=References= =What links here?= include page="Knight Pattern" component="backlinks" limit="60"
 * Up one Level**