Difference between revisions of "Knight Pattern"

From Chessprogramming wiki
Jump to: navigation, search
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
'''[[Main Page|Home]] * [[Board Representation]] * [[Bitboards]] * Knight Pattern'''
 
'''[[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> ]]
+
[[FILE:EschersHorseman.jpg|border|right|thumb|232px|link=http://www.mcescher.com/Shopmain/ShopEU/facsilimeprints/prints.html|[[:Category:M. C. 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'''
 
'''Knight Pattern'''
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]
* [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]
+
* [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
* [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]
+
* [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=
 
* [https://www.stmintz.com/ccc/index.php?id=354355 Symbolic: From bitboards to ideas] by [[Steven Edwards]], [[CCC]], March 13, 2004 » [[Symbolic]]
 
* [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/forum3/viewtopic.php?f=7&t=41542 Problem with bitboard knight attack generator] by [[Matthew R. Brades|ZirconiumX]], [[CCC]], December 21, 2011
 +
* [https://groups.google.com/d/msg/fishcooking/_qtvakyb_yM/FYOlNteY0N0J Knight fork threats] by [[Stefan Geschwentner]], [[Computer Chess Forums|FishCooking]], November 17, 2014 » [[Knight Pattern#KnightForks|Knight Forks]], [[Stockfish]]
 
* [http://www.talkchess.com/forum/viewtopic.php?t=55118 knight's multiple atacks] by [[Daniel Anulliero]], [[CCC]], January 27, 2015
 
* [http://www.talkchess.com/forum/viewtopic.php?t=55118 knight's multiple atacks] by [[Daniel Anulliero]], [[CCC]], January 27, 2015
  
Line 184: Line 186:
 
'''[[Bitboards|Up one Level]]'''
 
'''[[Bitboards|Up one Level]]'''
 
[[Category:M. C. Escher]]
 
[[Category:M. C. Escher]]
 +
[[Category:Videos]]

Latest revision as of 17:16, 27 June 2020

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.

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

Forum Posts

External Links

References

Up one Level