Code: Select all
;;; Position repetition counting
(defun count-repetitions (my-pos my-mh-stack my-limit)
  "Return the count of position repetitions in the given position and main hash stack."
  (let
    (
      (result    0)
      (hmvc      (pos-hmvc my-pos))
      (main-hash (pos-main-hash my-pos))
    )
    (do ((index 0 (1+ index))) ((or (= index hmvc) (null my-mh-stack) (= result my-limit)))
      (let ((prior-main-hash (pop my-mh-stack)))
        (when (and (odd? index) (hash-equal? main-hash prior-main-hash))
          (incf result))))
    result))
;;; Single repetition detection
(defun is-repeated? (my-pos my-mh-stack)
  "Return t if there is a position repetition in the given position and main hash stack."
  (one? (count-repetitions my-pos my-mh-stack 1)))
;;; Drawing status determination
(defun is-fifty-moves-draw? (my-pos)
  "Return t if there a fifty move rule draw in the given position."
  (>= (pos-hmvc my-pos) 100))
(defun is-insufficient-material-draw? (my-pos)
  "Return t if there is insufficent mating material draw in the given position."
  (insufficient-material? (pos-census my-pos)))
(defun is-repetition-draw? (my-pos my-mh-stack)
  "Return t if there is position repetition draw in the given position and main hash stack."
  (two? (count-repetitions my-pos my-mh-stack 2)))
;;; Checkmate/stalemate status determination
(defun is-checkmate? (my-pos)
  "Return t if the active king is checkmated in the given position."
  (and (is-act-king-in-check? my-pos) (no-moves? my-pos)))
(defun is-stalemate? (my-pos)
  "Return t if the active king is checkmated in the given position."
  (and (not (is-act-king-in-check? my-pos)) (no-moves? my-pos)))
