Changes

Jump to: navigation, search

Knight Pattern

10,607 bytes added, 14:53, 8 May 2018
Created page with "'''Home * Board Representation * Bitboards * Knight Pattern''' FILE:EschersHorseman.jpg|border|right|thumb|232px|link=http://www.mcescher.com/Shopmain..."
'''[[Main Page|Home]] * [[Board Representation]] * [[Bitboards]] * Knight Pattern'''

[[FILE:EschersHorseman.jpg|border|right|thumb|232px|link=http://www.mcescher.com/Shopmain/ShopEU/facsilimeprints/prints.html|[[Arts#Escher|M. C. Escher]], Horseman <ref>[http://www.mcescher.com/Shopmain/ShopEU/facsilimeprints/prints.html View facsimile print] from [http://www.mcescher.com/Shopmain/ShopEU/facsilimeprints/ M.C. Escher - 16 Facsimile Prints]</ref> ]]

'''Knight Pattern'''
with Bitboards covers [[Knight|knight]] [[Attacks|attacks]] of single or multiple knights, either by indexed pre-calculated tables or direct bitboard calculation, and the set wise determination of [https://en.wikipedia.org/wiki/Fork_%28chess%29 Knight fork] target squares.

<span id="KnightAttacks"></span>
=Knight Attacks=
The [[Knight]] attacks the [[Target square|target squares]] independently from other pieces around. The compass rose of all eight attacking [[Direction|directions]] associated with the to - from square differences from an [[8x8 Board|8x8 board]]:

<pre>
noNoWe noNoEa
+15 +17
| |
noWeWe +6 __| |__+10 noEaEa
\ /
>0<
__ / \ __
soWeWe -10 | | -6 soEaEa
| |
-17 -15
soSoWe soSoEa
</pre>
<span id="ByLookup"></span>
==by Lookup==
The knight is specified by square index, likely from a [[Bitscan|bitscan]] of a piece-wise [[Bitboard Serialization|bitboard serialization]] of a knight bitboard from a [[Bitboard Board-Definition|standard board-definition]], to index a table of pre-calculated knight-attacks:
<pre>
U64 arrKnightAttacks[64];

U64 knightAttacks(enumSquare sq) {return arrKnightAttacks[sq];}
</pre>
For instance a knight on d4
<pre>
arrKnightAttacks[d4]
. . . . . . . .
. . . . . . . .
. . 1 . 1 . . .
. 1 . . . 1 . .
. . . . . . . .
. 1 . . . 1 . .
. . 1 . 1 . . .
. . . . . . . .
</pre>
<span id="Calculation"></span>
==by Calculation==
Similar to [[General Setwise Operations#OneStepOnly|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#KnightAttacks|AVX2 Knight Attacks]].
<pre>
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;}
</pre>
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 Algorithms|fill approaches]] of other pieces with unique [[Target Square|target]]-[[Origin Square|source]] relationship - but disjoint direction-wise knight targets are sparse populated and usually contain only up to two bits.
<span id="MultipleKnightAttacks"></span>
=Multiple Knight Attacks=
To initialize the KnightAttacks [[Array|array]] one may use a routine with some kind of [[Parallel Prefix Algorithms|parallel prefix]] calculations, rather than the union of all eight directions:
<pre>
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;
}
</pre>
or to possibly gain some more parallelism:
<pre>
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);
}
</pre>
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.
<span id="KnightFill"></span>
=Knight Fill=
A fill cycle for a [[Fill Algorithms|fill algorithm]] is the [[General Setwise Operations#Union|union]] of the attack set with the knights itself:
<pre>
U64 knightFill(U64 knights) {return knightAttacks(knights) | knights;}
</pre>
for instance applied six times on the otherwise empty board:
<pre>
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
</pre>
<span id="KnightForks"></span>
=Knight Forks=
A common knight pattern is the knight fork. Targets are heavy pieces - king, queen and rooks, [[Hanging Piece|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 [[General Setwise Operations#GreaterOne|greater one sets]]:
<pre>
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;
}
</pre>
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#KnightAttacks|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 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]
* [http://scholar.google.com/citations?user=QNcGZdQAAAAJ&hl=de Philip Hingston], [[Graham Kendall]] ('''2005'''). ''[http://www.graham-kendall.com/publications/displaypub.php?key=hk2005a&filename=gxk.bib Ant Colonies Discover Knight's Tours]''. AI 2004, [http://www.springer.com/computer/ai/book/978-3-540-24059-4 Lecture Notes in Computer Science 3339]
* [http://scholar.google.com/citations?user=QNcGZdQAAAAJ&hl=de Philip Hingston], [[Graham Kendall]] ('''2005'''). ''[http://www.graham-kendall.com/publications/displaypub.php?key=hk2005&filename=gxk.bib Enumerating knight's tours using an ant colony algorithm]''. [http://www.informatik.uni-trier.de/~ley/db/conf/cec/cec2005.html CEC 2005]

=Forum Posts=
* [https://www.stmintz.com/ccc/index.php?id=354355 Symbolic: From bitboards to ideas] by [[Steven Edwards]], [[CCC]], March 13, 2004 » [[Symbolic]]
* [http://www.talkchess.com/forum/viewtopic.php?t=55118 knight's multiple atacks] by [[Daniel Anulliero]], [[CCC]], January 27, 2015

=External Links=
* [https://en.wikipedia.org/wiki/Knight%27s_tour Knight's tour from Wikipedia]
* [http://www.mayhematics.com/t/t.htm Knight's Tour Notes compiled by George Jelliss]
* [http://mathworld.wolfram.com/KnightsTour.html Knight's Tour - from Wolfram MathWorld]
* [https://en.wikipedia.org/wiki/Longest_uncrossed_knight%27s_path Longest uncrossed knight's path from Wikipedia]
* [[László Lindner|László Lindner's]] [http://archive.is/bHju8 knight wheel] by [[Frederic Friedel]] from [[ChessBase|ChessBase Puzzle]]
* [http://graham-kendall.com/blog/2014/01/knights-tours/ Knight's Tour] from [http://graham-kendall.com/blog/ Research Reflections] by [[Graham Kendall]], January 18, 2014
* Knight's Tour - [http://www.numberphile.com/ Numberphile], [https://en.wikipedia.org/wiki/YouTube YouTube] Video
: {{#evu:https://www.youtube.com/watch?v=ab_dY3dZFHM|alignment=left|valignment=top}}

=References=
<references />

'''[[Bitboards|Up one Level]]'''

Navigation menu