# Difference between revisions of "Knight Pattern"

GerdIsenberg (talk | contribs) |
GerdIsenberg (talk | contribs) |
||

Line 162: | Line 162: | ||

* [[Noam Elkies|Noam D. Elkies]], [[Mathematician#RPStanley|Richard P. Stanley]] ('''2003'''). ''The Mathematical Knight''. [https://en.wikipedia.org/wiki/The_Mathematical_Intelligencer The Mathematical Intelligencer], Vol. 25, No. 1, [http://www.math.harvard.edu/%7Eelkies/knight.pdf pdf] | * [[Noam Elkies|Noam D. Elkies]], [[Mathematician#RPStanley|Richard P. Stanley]] ('''2003'''). ''The Mathematical Knight''. [https://en.wikipedia.org/wiki/The_Mathematical_Intelligencer The Mathematical Intelligencer], Vol. 25, No. 1, [http://www.math.harvard.edu/%7Eelkies/knight.pdf pdf] | ||

* Ben Hill ('''2004'''). ''Knight’s Tours''. [http://faculty.olin.edu/~sadams/DM/ktpaper.pdf pdf] | * Ben Hill ('''2004'''). ''Knight’s Tours''. [http://faculty.olin.edu/~sadams/DM/ktpaper.pdf pdf] | ||

− | * [ | + | * [https://scholar.google.com/citations?user=QNcGZdQAAAAJ&hl=en Philip Hingston], [[Graham Kendall]] ('''2005'''). ''[https://link.springer.com/chapter/10.1007/978-3-540-30549-1_125 Ant Colonies Discover Knight's Tours]''. [https://link.springer.com/book/10.1007/b104336 AI 2004], [https://en.wikipedia.org/wiki/Lecture_Notes_in_Computer_Science Lecture Notes in Computer Science], Vol. 3339 |

− | * [ | + | * [https://scholar.google.com/citations?user=QNcGZdQAAAAJ&hl=en Philip Hingston], [[Graham Kendall]] ('''2005'''). ''Enumerating Knight’s Tours using an Ant Colony Algorithm''. [https://dblp.uni-trier.de/db/conf/cec/cec2005.html CEC 2005], [http://www.cs.nott.ac.uk/~pszgxk/papers/cec05knights.pdf pdf] |

=Forum Posts= | =Forum Posts= |

## Latest revision as of 16:04, 4 December 2019

**Home * Board Representation * Bitboards * Knight Pattern**

**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.

## Contents

# 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:

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

## 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:

U64 arrKnightAttacks[64]; U64 knightAttacks(enumSquare sq) {return arrKnightAttacks[sq];}

For instance a knight on d4

arrKnightAttacks[d4] . . . . . . . . . . . . . . . . . . 1 . 1 . . . . 1 . . . 1 . . . . . . . . . . . 1 . . . 1 . . . . 1 . 1 . . . . . . . . . . .

## 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.

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;}

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:

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; }

or to possibly gain some more parallelism:

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); }

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:

U64 knightFill(U64 knights) {return knightAttacks(knights) | knights;}

for instance applied six times on the otherwise empty board:

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

# 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:

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; }

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'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