e91cf811097a980a5bab950820b7769b63db08b8
1 import ChessRules
from "/base_rules.js";
3 export class ApocalypseRules
extends ChessRules
{
17 genRandInitBaseFen() {
19 fen: "npppn/p3p/5/P3P/NPPPN w 0",
25 let parts
= super.getPartFen(o
);
26 parts
["whiteMove"] = this.whiteMove
|| "-";
32 this.penaltyFlags
['w'].toString() + this.penaltyFlags
['b'].toString()
36 setOtherVariables(fen
) {
37 const parsedFen
= V
.ParseFen(fen
);
38 this.setFlags(parsedFen
.flags
);
39 // Also init whiteMove
41 parsedFen
.whiteMove
!= "-"
42 ? JSON
.parse(parsedFen
.whiteMove
)
48 'w': parseInt(fenflags
[0], 10),
49 'b': parseInt(fenflags
[1], 10)
53 // TODO: could be a stack of 2 (pawn promote + relocation)
55 if (!this.whiteMove
) return "-";
56 return JSON
.stringify({
57 start: this.whiteMove
.start
,
58 end: this.whiteMove
.end
,
59 appear: this.whiteMove
.appear
,
60 vanish: this.whiteMove
.vanish
64 // allow pawns to move diagonally and capture vertically --> only purpose illegal
65 // allow capture self --> same purpose
66 // ---> MARK such moves : move.illegal = true
68 // simpler: allow all moves, including "capturing nothing"
69 // flag every pawn capture as "illegal" (potentially)
72 const pawnShift
= (color
== "w" ? -1 : 1);
78 steps: [[pawnShift
, 0], [pawnShift
, -1], [pawnShift
, 1]],
83 'n': super.pieces(color
, x
, y
)['n']
91 getPotentialMovesFrom([x
, y
]) {
93 if (this.subTurn
== 2) {
94 const start
= this.moveStack
[0].end
;
95 if (x
== start
.x
&& y
== start
.y
) {
96 // Move the pawn to any empty square not on last rank (== x)
97 for (let i
=0; i
<this.size
.x
; i
++) {
100 for (let j
=0; j
<this.size
.y
; j
++) {
101 if (this.board
[i
][j
] == "")
102 moves
.push(this.getBasicMove([x
, y
], [i
, j
]));
108 moves
= super.getPotentialMovesFrom([x
, y
])
109 // Flag a priori illegal moves
112 // Self-capture test:
113 (m
.vanish
.length
== 2 && m
.vanish
[1].c
== m
.vanish
[0].c
) ||
114 // Pawn going diagonaly to empty square, or vertically to occupied
116 m
.vanish
[0].p
== 'p' &&
118 (m
.end
.y
== m
.start
.y
&& m
.vanish
.length
== 2) ||
119 (m
.end
.y
!= m
.start
.y
&& m
.vanish
.length
== 1)
135 // White and black (partial) moves were played: merge
136 // + animate both at the same time !
137 resolveSynchroneMove(move) {
142 // Do not play on board (would reveal the move...)
143 this.turn
= V
.GetOppCol(this.turn
);
149 if (pawn promotion into pawn
) {
150 this.curMove
move; //TODO: animate both move at same time + effects AFTER !
153 else if (this.turn
== 'b')
154 // NOTE: whiteMove is used read-only, so no need to copy
155 this.whiteMove
= move;
158 // A full turn just ended:
159 const smove
= this.resolveSynchroneMove(move);
160 V
.PlayOnBoard(this.board
, smove
); //----> ici : animate both !
161 this.whiteMove
= null;
165 atLeastOneLegalMove(...) {
170 if (this.turn
== 'b')
171 // Turn (white + black) not over yet. Could be stalemate if black cannot move (legally)
172 // TODO: check. If so, return "1/2".
174 // Count footmen: if a side has none, it loses
175 let fmCount
= { 'w': 0, 'b': 0 };
176 for (let i
=0; i
<5; i
++) {
177 for (let j
=0; j
<5; j
++) {
178 if (this.board
[i
][j
] != V
.EMPTY
&& this.getPiece(i
, j
) == V
.PAWN
)
179 fmCount
[this.getColor(i
, j
)]++;
182 if (Object
.values(fmCount
).some(v
=> v
== 0)) {
183 if (fmCount
['w'] == 0 && fmCount
['b'] == 0)
186 if (fmCount
['w'] == 0) return "0-1";
187 return "1-0"; //fmCount['b'] == 0
189 // Check penaltyFlags: if a side has 2 or more, it loses
190 if (Object
.values(this.penaltyFlags
).every(v
=> v
== 2)) return "1/2";
191 if (this.penaltyFlags
['w'] == 2) return "0-1";
192 if (this.penaltyFlags
['b'] == 2) return "1-0";
193 if (!this.atLeastOneLegalMove('w') || !this.atLeastOneLegalMove('b'))
194 // Stalemate (should be very rare)