flipcolor
flipcolor
is the most important and most commonly used transform filter.
flipcolor F
matches a position if either F
matches the position or if reversecolor F
matches the position.
The reversecolor transform
The idea ofreversecolor F
is that the roles of the white and black players are switched in F
.
reversecolor F
is formed from F by performing the following operations on each filter contained in F:
- keywords
white
andblack
are interchanged wtm
andbtm
are interchangedresult 0-1
andresult 1-0
are interchanged- The color of any piecetype is inverted:
K
becomesk
;k
becomesK
,a
becomesA
, and so on -
Any square designator is reflected about the horizontal bisector. Thus,
a2
becomesa7
;[b-d4,g8]
becomes[b-d5,g1]
. light
anddark
are interchanged
Here are some examples of the effect of the reversecolor operation on different filters:
reversecolor Ke1 ≡ ke8 reversecolor 2==A attacks k ≡ 2==a attacks K reversecolor {player white "Kasparov" result 1-0} ≡ {player black "Kasparov" result 0-1} reversecolor power A>power a + 10 ≡ power a>power A + 10 reversecolor [Kk]h1-8 ≡ [Kk]h1-8 //no change
Thus, Ke1
matches a position in which the white king is on
e1
, so flipcolor Ke1
matches a position in which either the white king is on
e1
or the black king is on e8
.
Likewise,
2==A attacks k
matches a position in which the black king is in double check, so
flipcolor 2 == A attacks kmatches a position in which either the white or the black king is in double check.
flipcolor
as a set
If F is a set filter, then so is flipcolor F
and its value is the union of the sets of squares represented by F
and reversecolor F
.
For example, A attacks q
represents the set of white pieces that are attacking a black queen (in the current position). Therefore,
flipcolor A attacks q
represents the set of pieces attacking an enemy queen, that is, the union of the set of white pieces attacking a black queen (A attacks q
) and the set of black pieces attacking a white queen (a attacks Q
).
flipcolor
as a number
If F is a numeric filter, then so is flipcolor F
and its value is the maximum of the values of F
and reversecolor F
. (If neither of these two filters match the position, neither does flipcolor F
).
For example, #P
represents the set of white pawns in the current position. Thus, reversecolor #P
is #p
and represents the number of black pawns in the current position. Hence
flipcolor #P
≡
max(#P #p)
the maximum of the number of white pawns and the number of black pawns in the position.
flipcolor count
The value of
flipcolor count F
is the number of distinct filters in the set of filters consisting of F
and reversecolor F
that match the current position:
- If neither
F
norreversecolor F
match the current position, then the value offlipcolor count F
is0
. - If exactly one of
F
andreversecolor F
match the current position, then the value offlipcolor count F
is1
. - If both
F
andreversecolor F
match the current position (and if the two filters are distinct) then the value offlipcolor count F
is2
For example, A attacks q
matches a position in which the black queen is under attack. Therefore,
2==flipcolor count A attacks qmatches a position in which both the white and black queen are being attacked.
The -flipcolor
command line option
flipcolor
can be invoked from the command line as well. If the -flipcolor
option is provided to the cql
command line, then the entire body of the input CQL file is implicitly wrapped in a flipcolor
. You can thus write a CQL file for one side, and experiment using the command line to see what happens from either side.
Examples
Theflipcolor
filter is used in many of the examples.
In excelsior-multiple.cql, the filter
flipcolor Pa-h2is used to get the set of pawns in their starting positions (second rank for white pawns; 7th rank for black pawns). It is equivalent to:
Pa-h2 | pa-h7
A more common example is illustrated by immured.cql, which finds pieces that are not pinned and have no legal moves. The results are sorted by the number of such pieces by one side, if this number is at least 3.
First, the CQL file is written from the perspective of one side, say finding games with 3 white immured pieces. Then the entire body is wrapped in braces and flipcolor
is applied, to search for games with 3 black immured pieces too. Note that the sort works perfectly well inside of flipcolor
: sort
will remember and sort by the maximum value it sees, whether the original or under reversecolor.
Other examples of flipcolor
are in gameinfo.cql, idealstalemate.cql, long-sacrifice.cql, massivestalemate.cql, mirrormate.cql, multiple-discovered-checks.cql, passers.cql, pins.cql, pinstalemate.cql, powerdifference-stalemate.cql, quadrupled-pawns.cql, queentriangulation.cql, staircase-sort.cql.