Skip to content

Instantly share code, notes, and snippets.

@djtango
Last active December 5, 2018 17:39
Show Gist options
  • Save djtango/1625ffecc67675ce2de56c2ee6117740 to your computer and use it in GitHub Desktop.
Save djtango/1625ffecc67675ce2de56c2ee6117740 to your computer and use it in GitHub Desktop.
Specs that describe Chess Portable Chess Notation
(ns chess-gm.pgn
"See https://opensource.apple.com/source/Chess/Chess-110.0.6/Documentation/PGN-Standard.txt
and https://en.wikipedia.org/wiki/Portable_Game_Notation for more details on specification"
(:require [clojure.spec.alpha :as s]))
(s/def ::column-index (set "abcdefgh"))
(s/def ::row-index (set "12345678"))
(s/def ::piece-name (set "QKWBNRP"))
(s/def ::check #{\+})
(s/def ::checkmate #{\#})
(s/def ::position (s/cat :file ::column-index
:rank ::row-index))
(s/def ::basic-move (s/cat :piece (s/? ::piece-name)
:original-file (s/? ::column-index)
:original-rank (s/? ::row-index)
:position ::position
:pawn-promotion? (s/? ::pawn-promotion)
:win? (s/alt :check (s/? ::check)
:checkmate (s/? ::checkmate))))
(s/def ::pawn-promotion (s/cat :pawn-promotion #{\=}
:piece ::piece-name))
(s/def ::capture (s/cat :taker (s/or :pawn ::column-index
:piece ::piece-name)
:original-file (s/? ::column-index)
:original-rank (s/? ::row-index)
:x #{\x}
:takee ::position
:pawn-promotion? (s/? ::pawn-promotion)
:check (s/? ::check)
:checkmate (s/? ::checkmate)))
(s/def ::castling (s/or :kingside #{(seq "O-O")}
:queenside #{(seq "O-O-O")}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment