1 class CheckeredRules
extends ChessRules
5 return b
[0]=='c' ? "Checkered/"+b : b
;
9 const checkered_codes
= {
17 return checkered_codes
[b
[1]];
18 return ChessRules
.board2fen(b
);
22 const checkered_pieces
= {
29 if (Object
.keys(checkered_pieces
).includes(f
))
30 return 'c'+checkered_pieces
[f
];
31 return ChessRules
.fen2board(f
);
36 super.setFlags(fen
); //castleFlags
39 "w": new Array(8), //pawns can move 2 squares?
42 const flags
= fen
.split(" ")[1].substr(4); //skip first 4 digits, for castle
43 for (let c
of ['w','b'])
45 for (let i
=0; i
<8; i
++)
46 this.pawnFlags
[c
][i
] = (flags
.charAt((c
=='w'?0:8)+i
) == '1');
50 // Aggregates flags into one object
52 return [this.castleFlags
, this.pawnFlags
];
58 this.castleFlags
= flags
[0];
59 this.pawnFlags
= flags
[1];
62 canTake([x1
,y1
], [x2
,y2
])
64 const color1
= this.getColor(x1
,y1
);
65 const color2
= this.getColor(x2
,y2
);
66 // Checkered aren't captured
67 return color1
!= color2
&& color2
!= 'c' && (color1
!= 'c' || color2
!= this.turn
);
70 // Post-processing: apply "checkerization" of standard moves
71 getPotentialMovesFrom([x
,y
])
73 let standardMoves
= super.getPotentialMovesFrom([x
,y
]);
74 const lastRank
= this.turn
== "w" ? 0 : 7;
75 if (this.getPiece(x
,y
) == V
.KING
)
76 return standardMoves
; //king has to be treated differently (for castles)
78 standardMoves
.forEach(m
=> {
79 if (m
.vanish
[0].p
== V
.PAWN
)
81 if (Math
.abs(m
.end
.x
-m
.start
.x
)==2 && !this.pawnFlags
[this.turn
][m
.start
.y
])
82 return; //skip forbidden 2-squares jumps
83 if (this.board
[m
.end
.x
][m
.end
.y
] == V
.EMPTY
&& m
.vanish
.length
==2
84 && this.getColor(m
.start
.x
,m
.start
.y
) == 'c')
86 return; //checkered pawns cannot take en-passant
89 if (m
.vanish
.length
== 1)
90 moves
.push(m
); //no capture
93 // A capture occured (m.vanish.length == 2)
96 if (m
.appear
[0].p
!= m
.vanish
[1].p
//avoid promotions (already treated):
97 && (m
.vanish
[0].p
!= V
.PAWN
|| m
.end
.x
!= lastRank
))
99 // Add transformation into captured piece
100 let m2
= JSON
.parse(JSON
.stringify(m
));
101 m2
.appear
[0].p
= m
.vanish
[1].p
;
109 canIplay(side
, [x
,y
])
111 return ((side
=='w' && this.moves
.length
%2==0)
112 || (side
=='b' && this.moves
.length
%2==1))
113 && [side
,'c'].includes(this.getColor(x
,y
));
116 // Does m2 un-do m1 ? (to disallow undoing checkered moves)
117 oppositeMoves(m1
, m2
)
119 return m1
.appear
.length
== 1 && m2
.appear
.length
== 1
120 && m1
.vanish
.length
== 1 && m2
.vanish
.length
== 1
121 && m1
.start
.x
== m2
.end
.x
&& m1
.end
.x
== m2
.start
.x
122 && m1
.start
.y
== m2
.end
.y
&& m1
.end
.y
== m2
.start
.y
123 && m1
.appear
[0].c
== m2
.vanish
[0].c
&& m1
.appear
[0].p
== m2
.vanish
[0].p
124 && m1
.vanish
[0].c
== m2
.appear
[0].c
&& m1
.vanish
[0].p
== m2
.appear
[0].p
;
129 if (moves
.length
== 0)
131 const color
= this.turn
;
132 return moves
.filter(m
=> {
133 const L
= this.moves
.length
;
134 if (L
> 0 && this.oppositeMoves(this.moves
[L
-1], m
))
136 return !this.underCheck(m
);
140 isAttackedByPawn([x
,y
], colors
)
142 for (let c
of colors
)
144 const color
= (c
=="c" ? this.turn : c
);
145 let pawnShift
= (color
=="w" ? 1 : -1);
146 if (x
+pawnShift
>=0 && x
+pawnShift
<8)
148 for (let i
of [-1,1])
150 if (y
+i
>=0 && y
+i
<8 && this.getPiece(x
+pawnShift
,y
+i
)==V
.PAWN
151 && this.getColor(x
+pawnShift
,y
+i
)==c
)
163 const color
= this.turn
;
165 let res
= this.isAttacked(this.kingPos
[color
], [this.getOppCol(color
),'c']);
170 getCheckSquares(move)
173 const color
= this.turn
;
174 this.moves
.push(move); //artifically change turn, for checkered pawns (TODO)
175 const kingAttacked
= this.isAttacked(
176 this.kingPos
[color
], [this.getOppCol(color
),'c']);
177 let res
= kingAttacked
178 ? [ JSON
.parse(JSON
.stringify(this.kingPos
[color
])) ] //need to duplicate!
185 updateVariables(move)
187 super.updateVariables(move);
188 // Does this move turn off a 2-squares pawn flag?
189 const secondRank
= [1,6];
190 if (secondRank
.includes(move.start
.x
) && move.vanish
[0].p
== V
.PAWN
)
191 this.pawnFlags
[move.start
.x
==6 ? "w" : "b"][move.start
.y
] = false;
196 const color
= this.turn
;
197 this.moves
.length
++; //artifically change turn, for checkered pawns (TODO)
198 const res
= this.isAttacked(this.kingPos
[color
], [this.getOppCol(color
),'c'])
199 ? (color
== "w" ? "0-1" : "1-0")
208 //Just count material for now, considering checkered neutral (...)
209 for (let i
=0; i
<V
.size
.x
; i
++)
211 for (let j
=0; j
<V
.size
.y
; j
++)
213 if (this.board
[i
][j
] != V
.EMPTY
)
215 const sqColor
= this.getColor(i
,j
);
216 const sign
= sqColor
== "w" ? 1 : (sqColor
=="b" ? -1 : 0);
217 evaluation
+= sign
* V
.VALUES
[this.getPiece(i
,j
)];
224 static GenRandInitFen()
226 return ChessRules
.GenRandInitFen() + "1111111111111111"; //add 16 pawns flags
231 let fen
= super.getFlagsFen();
233 for (let c
of ['w','b'])
235 for (let i
=0; i
<8; i
++)
236 fen
+= this.pawnFlags
[c
][i
] ? '1' : '0';
243 if (move.appear
.length
== 2)
246 if (move.end
.y
< move.start
.y
)
252 // Translate final square
254 String
.fromCharCode(97 + move.end
.y
) + (V
.size
.x
-move.end
.x
);
256 let piece
= this.getPiece(move.start
.x
, move.start
.y
);
261 if (move.vanish
.length
> 1)
264 let startColumn
= String
.fromCharCode(97 + move.start
.y
);
265 notation
= startColumn
+ "x" + finalSquare
+
266 "=" + move.appear
[0].p
.toUpperCase();
270 notation
= finalSquare
;
271 if (move.appear
.length
> 0 && piece
!= move.appear
[0].p
) //promotion
272 notation
+= "=" + move.appear
[0].p
.toUpperCase();
280 return piece
.toUpperCase() + (move.vanish
.length
> 1 ? "x" : "") + finalSquare
281 + (move.vanish
.length
> 1 ? "=" + move.appear
[0].p
.toUpperCase() : "");