Skip to content

Instantly share code, notes, and snippets.

@ericnormand
Created January 3, 2022 22:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericnormand/655bca753bced9299363ee8d41df8b5e to your computer and use it in GitHub Desktop.
Save ericnormand/655bca753bced9299363ee8d41df8b5e to your computer and use it in GitHub Desktop.
457 PurelyFunctional.tv Newsletter

Chess moves

Write a function that determines if a chess piece, on an empty board, can move from one space to another in one move.

Examples

(can-move? :pawn "A2" "A3") ;=> true
(can-move? :queen "H1" "A8") ;=> true
(can-move? :knight "A4" "A5") ;=> false ;; (that's not how knights move)
(can-move? :king "A8" "A9") ;=> false ;; (that's off the board)

Notes

  • This page has a nice graphic of all of the chess pieces and their moves.
  • Assume that pawns are moving from the low to the high numbers.
  • You can ignore en passant, pawn's capture, castling, and pawn's two-square move on the second rank.

Thanks to this site for the problem idea, where it is rated Very Hard in Java. The problem has been modified.

Please submit your solutions as comments on this gist.

To subscribe: https://purelyfunctional.tv/newsletter/

@KingCode
Copy link

KingCode commented May 28, 2022

@jonasseglare

Your solution is just brilliant (even though obfuscated :)! I am enjoying picking it apart.

So far I can gather that you encoded all valid moves as indexes into a table (string) of characters, each of which's
underlying int has its compatible pieces' bits set - very clever.

I still have to figure out precisely how you managed to organize and encode all 225 (from the table size)
valid moves across all pieces.

Nice work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment