// Download idealmate.cql
// PGN output when run on sample.pgn

black ideal mate
Same as model mate (in puremate.cql), except each piece on the
board takes part.
The output is sorted by the total number of pieces in the mate.

This file begins by copying modelmate.cql, then making a few small

cql(input heijden.pgn)
flipcolor{mate btm

KingsField= . attackedby k

Checkers= A attacks k

function GuardedSquares(x){
  KingsField attackedby x
  | xray (x k KingsField)}

function GuardingPieces(s) { 
  piece fy in A
     s & GuardedSquares(fy)}

// Every flight square guarded exactly once
square all FlightSquare in KingsField & [_A]

//Every selfblocker is either unguarded, or, if it is not double check,
// (a) pinned and (b) could interfere with a checker

square all SelfBlocker in KingsField & a {
  if Checkers>1 then
  else{ //single-check case
      pin through SelfBlocker
      move pseudolegal
          from SelfBlocker
	  to   (Checkers | between (Checkers k))
   } // end the single-check case
  } //end considering the SelfBlocker

function Necessary(Checker){
  OtherChecker=Checkers & ~Checker
  RefutationSquares= OtherChecker | between (OtherChecker k)
  NonPinnedBlackPiece= {a & ~pin} & ~k
  NonPinnedBlackPiece attacks RefutationSquares

if Checkers>1 then
  square all Checker in Checkers
/* The code above this line is identical to puremate.cql */

  K |
  Checkers |
  between(Checkers k)

  move from a to RefuterSquares pseudolegal

  square  Attacker in A
     Attacker in Checkers
     or Attacker attacks KingsField
     or pin through Attacker
     or Checkers ==1
        and pin from Attacker through PossibleRefuter
     or Checkers==1
        and xray (PossibleRefuter Attacker RefuterSquares)

[RNBQ] in WhiteParticipants //every piece participates
/* The code above this line is identical to modelmate.cql*/

/* Now we start on the idealmate specific stuff*/

//each white piece is a participating attacker (this could
// have been accomplished with a square all as well)


//check that all black pieces participates
square all BlackPiece in a & ~(k|KingsField) {
  xray (a BlackPiece K)
  or {Checkers==1 //not a double check
      xray (a BlackPiece RefuterSquares)}

sort "Number Pieces in the ideal mate" [Aa]