1 class CheckeredRules
extends ChessRules
5 return b
[0]=='c' ? "Checkered/"+b : b
;
10 const checkered_codes
= {
18 return checkered_codes
[b
[1]];
19 return ChessRules
.board2fen(b
);
24 // Tolerate upper-case versions of checkered pieces (why not?)
25 const checkered_pieces
= {
37 if (Object
.keys(checkered_pieces
).includes(f
))
38 return 'c'+checkered_pieces
[f
];
39 return ChessRules
.fen2board(f
);
44 return ChessRules
.PIECES
.concat(['s','t','u','c','o']);
47 static IsGoodFlags(flags
)
49 // 4 for castle + 16 for pawns
50 return !!flags
.match(/^[01]{20,20}$/);
55 super.setFlags(fenflags
); //castleFlags
58 "w": _
.map(_
.range(8), i
=> true), //pawns can move 2 squares?
59 "b": _
.map(_
.range(8), i
=> true)
63 const flags
= fenflags
.substr(4); //skip first 4 digits, for castle
64 for (let c
of ['w','b'])
66 for (let i
=0; i
<8; i
++)
67 this.pawnFlags
[c
][i
] = (flags
.charAt((c
=='w'?0:8)+i
) == '1');
73 return [this.castleFlags
, this.pawnFlags
];
76 disaggregateFlags(flags
)
78 this.castleFlags
= flags
[0];
79 this.pawnFlags
= flags
[1];
82 canTake([x1
,y1
], [x2
,y2
])
84 const color1
= this.getColor(x1
,y1
);
85 const color2
= this.getColor(x2
,y2
);
86 // Checkered aren't captured
87 return color1
!= color2
&& color2
!= 'c' && (color1
!= 'c' || color2
!= this.turn
);
90 // Post-processing: apply "checkerization" of standard moves
91 getPotentialMovesFrom([x
,y
])
93 let standardMoves
= super.getPotentialMovesFrom([x
,y
]);
94 const lastRank
= this.turn
== "w" ? 0 : 7;
95 if (this.getPiece(x
,y
) == V
.KING
)
96 return standardMoves
; //king has to be treated differently (for castles)
98 standardMoves
.forEach(m
=> {
99 if (m
.vanish
[0].p
== V
.PAWN
)
101 if (Math
.abs(m
.end
.x
-m
.start
.x
)==2 && !this.pawnFlags
[this.turn
][m
.start
.y
])
102 return; //skip forbidden 2-squares jumps
103 if (this.board
[m
.end
.x
][m
.end
.y
] == V
.EMPTY
&& m
.vanish
.length
==2
104 && this.getColor(m
.start
.x
,m
.start
.y
) == 'c')
106 return; //checkered pawns cannot take en-passant
109 if (m
.vanish
.length
== 1)
110 moves
.push(m
); //no capture
113 // A capture occured (m.vanish.length == 2)
116 if (m
.appear
[0].p
!= m
.vanish
[1].p
//avoid promotions (already treated):
117 && (m
.vanish
[0].p
!= V
.PAWN
|| m
.end
.x
!= lastRank
))
119 // Add transformation into captured piece
120 let m2
= JSON
.parse(JSON
.stringify(m
));
121 m2
.appear
[0].p
= m
.vanish
[1].p
;
129 canIplay(side
, [x
,y
])
131 return (side
== this.turn
&& [side
,'c'].includes(this.getColor(x
,y
)));
134 // Does m2 un-do m1 ? (to disallow undoing checkered moves)
135 oppositeMoves(m1
, m2
)
137 return m1
.appear
.length
== 1 && m2
.appear
.length
== 1
138 && m1
.vanish
.length
== 1 && m2
.vanish
.length
== 1
139 && m1
.start
.x
== m2
.end
.x
&& m1
.end
.x
== m2
.start
.x
140 && m1
.start
.y
== m2
.end
.y
&& m1
.end
.y
== m2
.start
.y
141 && m1
.appear
[0].c
== m2
.vanish
[0].c
&& m1
.appear
[0].p
== m2
.vanish
[0].p
142 && m1
.vanish
[0].c
== m2
.appear
[0].c
&& m1
.vanish
[0].p
== m2
.appear
[0].p
;
147 if (moves
.length
== 0)
149 const color
= this.turn
;
150 return moves
.filter(m
=> {
151 const L
= this.moves
.length
;
152 if (L
> 0 && this.oppositeMoves(this.moves
[L
-1], m
))
154 return !this.underCheck(m
);
158 isAttackedByPawn([x
,y
], colors
)
160 for (let c
of colors
)
162 const color
= (c
=="c" ? this.turn : c
);
163 let pawnShift
= (color
=="w" ? 1 : -1);
164 if (x
+pawnShift
>=0 && x
+pawnShift
<8)
166 for (let i
of [-1,1])
168 if (y
+i
>=0 && y
+i
<8 && this.getPiece(x
+pawnShift
,y
+i
)==V
.PAWN
169 && this.getColor(x
+pawnShift
,y
+i
)==c
)
181 const color
= this.turn
;
183 let res
= this.isAttacked(this.kingPos
[color
], [this.getOppCol(color
),'c']);
188 getCheckSquares(move)
191 const color
= this.turn
;
192 // Artifically change turn, for checkered pawns
193 this.turn
= this.getOppCol(color
);
194 const kingAttacked
= this.isAttacked(
195 this.kingPos
[color
], [this.getOppCol(color
),'c']);
196 let res
= kingAttacked
197 ? [JSON
.parse(JSON
.stringify(this.kingPos
[color
]))] //need to duplicate!
204 updateVariables(move)
206 super.updateVariables(move);
207 // Does this move turn off a 2-squares pawn flag?
208 const secondRank
= [1,6];
209 if (secondRank
.includes(move.start
.x
) && move.vanish
[0].p
== V
.PAWN
)
210 this.pawnFlags
[move.start
.x
==6 ? "w" : "b"][move.start
.y
] = false;
215 const color
= this.turn
;
216 // Artifically change turn, for checkered pawns
217 this.turn
= this.getOppCol(this.turn
);
218 const res
= this.isAttacked(this.kingPos
[color
], [this.getOppCol(color
),'c'])
219 ? (color
== "w" ? "0-1" : "1-0")
221 this.turn
= this.getOppCol(this.turn
);
228 //Just count material for now, considering checkered neutral (...)
229 for (let i
=0; i
<V
.size
.x
; i
++)
231 for (let j
=0; j
<V
.size
.y
; j
++)
233 if (this.board
[i
][j
] != V
.EMPTY
)
235 const sqColor
= this.getColor(i
,j
);
236 const sign
= sqColor
== "w" ? 1 : (sqColor
=="b" ? -1 : 0);
237 evaluation
+= sign
* V
.VALUES
[this.getPiece(i
,j
)];
244 static GenRandInitFen()
246 const randFen
= ChessRules
.GenRandInitFen();
247 // Add 16 pawns flags:
248 return randFen
.replace(" w 1111", " w 11111111111111111111");
253 let fen
= super.getFlagsFen();
255 for (let c
of ['w','b'])
257 for (let i
=0; i
<8; i
++)
258 fen
+= this.pawnFlags
[c
][i
] ? '1' : '0';
265 if (move.appear
.length
== 2)
268 if (move.end
.y
< move.start
.y
)
274 // Translate final square
275 const finalSquare
= V
.CoordsToSquare(move.end
);
277 const piece
= this.getPiece(move.start
.x
, move.start
.y
);
282 if (move.vanish
.length
> 1)
285 const startColumn
= V
.GetColumn(move.start
.y
);
286 notation
= startColumn
+ "x" + finalSquare
+
287 "=" + move.appear
[0].p
.toUpperCase();
291 notation
= finalSquare
;
292 if (move.appear
.length
> 0 && piece
!= move.appear
[0].p
) //promotion
293 notation
+= "=" + move.appear
[0].p
.toUpperCase();
301 return piece
.toUpperCase() + (move.vanish
.length
> 1 ? "x" : "") + finalSquare
302 + (move.vanish
.length
> 1 ? "=" + move.appear
[0].p
.toUpperCase() : "");
307 const VariantRules
= CheckeredRules
;