9de46ff1bca14558312086bf1e22433a0fa23128
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
))
155 const res
= !this.underCheck(color
);
161 isAttackedByPawn([x
,y
], colors
)
163 for (let c
of colors
)
165 const color
= (c
=="c" ? this.turn : c
);
166 let pawnShift
= (color
=="w" ? 1 : -1);
167 if (x
+pawnShift
>=0 && x
+pawnShift
<8)
169 for (let i
of [-1,1])
171 if (y
+i
>=0 && y
+i
<8 && this.getPiece(x
+pawnShift
,y
+i
)==V
.PAWN
172 && this.getColor(x
+pawnShift
,y
+i
)==c
)
184 return this.isAttacked(this.kingPos
[color
], [V
.GetOppCol(color
),'c']);
187 getCheckSquares(color
)
189 // Artifically change turn, for checkered pawns
190 this.turn
= V
.GetOppCol(color
);
191 const kingAttacked
= this.isAttacked(
192 this.kingPos
[color
], [V
.GetOppCol(color
),'c']);
193 let res
= kingAttacked
194 ? [JSON
.parse(JSON
.stringify(this.kingPos
[color
]))] //need to duplicate!
200 updateVariables(move)
202 super.updateVariables(move);
203 // Does this move turn off a 2-squares pawn flag?
204 const secondRank
= [1,6];
205 if (secondRank
.includes(move.start
.x
) && move.vanish
[0].p
== V
.PAWN
)
206 this.pawnFlags
[move.start
.x
==6 ? "w" : "b"][move.start
.y
] = false;
211 const color
= this.turn
;
212 // Artifically change turn, for checkered pawns
213 this.turn
= V
.GetOppCol(this.turn
);
214 const res
= this.isAttacked(this.kingPos
[color
], [V
.GetOppCol(color
),'c'])
215 ? (color
== "w" ? "0-1" : "1-0")
217 this.turn
= V
.GetOppCol(this.turn
);
224 //Just count material for now, considering checkered neutral (...)
225 for (let i
=0; i
<V
.size
.x
; i
++)
227 for (let j
=0; j
<V
.size
.y
; j
++)
229 if (this.board
[i
][j
] != V
.EMPTY
)
231 const sqColor
= this.getColor(i
,j
);
232 const sign
= sqColor
== "w" ? 1 : (sqColor
=="b" ? -1 : 0);
233 evaluation
+= sign
* V
.VALUES
[this.getPiece(i
,j
)];
240 static GenRandInitFen()
242 const randFen
= ChessRules
.GenRandInitFen();
243 // Add 16 pawns flags:
244 return randFen
.replace(" w 1111", " w 11111111111111111111");
249 let fen
= super.getFlagsFen();
251 for (let c
of ['w','b'])
253 for (let i
=0; i
<8; i
++)
254 fen
+= this.pawnFlags
[c
][i
] ? '1' : '0';
261 if (move.appear
.length
== 2)
264 if (move.end
.y
< move.start
.y
)
270 // Translate final square
271 const finalSquare
= V
.CoordsToSquare(move.end
);
273 const piece
= this.getPiece(move.start
.x
, move.start
.y
);
278 if (move.vanish
.length
> 1)
281 const startColumn
= V
.CoordToColumn(move.start
.y
);
282 notation
= startColumn
+ "x" + finalSquare
+
283 "=" + move.appear
[0].p
.toUpperCase();
287 notation
= finalSquare
;
288 if (move.appear
.length
> 0 && piece
!= move.appear
[0].p
) //promotion
289 notation
+= "=" + move.appear
[0].p
.toUpperCase();
297 return piece
.toUpperCase() + (move.vanish
.length
> 1 ? "x" : "") + finalSquare
298 + (move.vanish
.length
> 1 ? "=" + move.appear
[0].p
.toUpperCase() : "");