1 import { ChessRules
, Move
, PiPo
} from "@/base_rules";
3 export class DynamoRules
extends ChessRules
{
4 // TODO: later, allow to push out pawns on a and h files?
5 static get HasEnpassant() {
9 canIplay(side
, [x
, y
]) {
10 // Sometimes opponent's pieces can be moved directly
14 setOtherVariables(fen
) {
15 super.setOtherVariables(fen
);
17 // Local stack of "action moves"
19 const amove
= V
.ParseFen(fen
).amove
;
20 if (cmove
== "-") this.amoves
.push(null);
22 const amoveParts
= amove
.split("/");
24 // No need for start & end
29 amoveParts
[0].split(".").forEach(av
=> {
31 const xy
= V
.SquareToCoords(av
.substr(2));
32 move[i
== 0 ? "appear" : "vanish"].push(
42 this.amoves
.push(move);
46 static ParseFen(fen
) {
48 ChessRules
.ParseFen(fen
),
49 { cmove: fen
.split(" ")[4] }
53 static IsGoodFen(fen
) {
54 if (!ChessRules
.IsGoodFen(fen
)) return false;
55 const fenParts
= fen
.split(" ");
56 if (fenParts
.length
!= 6) return false;
57 if (fenParts
[5] != "-" && !fenParts
[5].match(/^([a-h][1-8]){2}$/))
63 if (move.appear
.length
== 2 && move.vanish
.length
== 2)
64 return { appear: move.appear
, vanish: move.vanish
};
68 // TODO: this.firstMove + rooks location in setOtherVariables
69 // only rooks location in FEN (firstMove is forgotten if quit game and come back)
71 // If subTurn == 2 && square is the final square of last move,
72 // then return an empty move
75 square
.x
== this.firstMove
.end
.x
&&
76 square
.y
== this.firstMove
.end
.y
87 // Captures don't occur (only pulls & pushes)
91 // "pa" : piece (as a square) doing this push/pull action
92 getActionMoves([sx
, sy
], [ex
, ey
], pa
) {
93 const color
= this.getColor(sx
, sy
);
94 const lastRank
= (color
== 'w' ? 0 : 7);
95 const piece
= this.getPiece(sx
, sy
);
97 if (ex
== lastRank
&& piece
== V
.PAWN
) {
98 // Promotion by push or pull
99 V
.PawnSpecs
.promotions
.forEach(p
=> {
100 let move = super.getBasicMove([sx
, sy
], [ex
, ey
], { c: color
, p: p
});
103 } else moves
.push(super.getBasicMove([sx
, sy
], [ex
, ey
]));
106 Math
.abs(pa
[0] - sx
) < Math
.abs(pa
[0] - ex
) ||
107 Math
.abs(pa
[1] - sy
) < Math
.abs(pa
[1] - ey
)
111 moves
.forEach(m
=> m
.action
= [{ by: pa
, type: actionType
}]);
115 // TODO: if type is given, consider only actions of this type
116 getPactions(sq
, color
, type
) {
121 const oppCol
= V
.GetOppCol(color
);
122 // Look in all directions for a "color" piece
123 for (let step
of V
.steps
[V
.KNIGHT
]) {
124 const xx
= x
+ step
[0],
128 this.getPiece(xx
, yy
) == V
.KNIGHT
&&
129 this.getColor(xx
, yy
) == color
131 const px
= x
- step
[0],
133 if (V
.OnBoard(px
, py
)) {
134 if (this.board
[px
][py
] == V
.EMPTY
) {
135 const hash
= "s" + px
+ py
;
136 if (!squares
[hash
]) {
137 squares
[hash
] = true;
138 Array
.prototype.push
.apply(
140 this.getActionMoves([x
, y
], [px
, py
], [xx
, yy
])
143 else { //add piece doing action
147 const hash
= "s" + xx
+ yy
;
148 if (!squares
[hash
]) {
149 squares
[hash
] = true;
152 start: { x: x
, y: y
},
153 end: { x: xx
, y: yy
},
159 p: this.getPiece(x
, y
),
169 for (let step
in V
.steps
[V
.ROOK
]) {
170 // (+ if color is ours, pawn pushes) king, rook and queen
171 // --> pawns special case can push from a little distance if on 2nd rank (or 1st rank)
173 for (let step
in V
.steps
[V
.BISHOP
]) {
174 // King, bishop, queen, and possibly pawns attacks (if color is enemy)
180 // NOTE: to push a piece out of the board, make it slide until our piece
181 // (doing the action, moving or not)
182 // TODO: for pushes, play the pushed piece first.
183 // for pulls: play the piece doing the action first
184 getPotentialMovesFrom([x
, y
]) {
185 const color
= this.turn
;
186 if (this.getColor(x
, y
) != color
)
187 // The only moves possible with enemy pieces are pulls and pushes:
188 return this.getPactions([x
, y
], color
);
189 // Playing my pieces: either on their own, or pushed by another
190 // If subTurn == 2 then we should have a first move,
191 // TODO = use it to allow some type of action
192 if (this.subTurn
== 2) {
194 this.moveOnSubturn1
.isAnAction
195 ? super.getPotentialMovesFrom([x
, y
])
196 : this.getPactions([x
, y
], color
, TODO_arg
)
199 // Both options are possible at subTurn1: normal move, or push
201 super.getPotentialMovesFrom([x
, y
])
202 .concat(this.getPactions([x
, y
], color
, "push"))
203 // TODO: discard moves that let the king underCheck, and no second
204 // move can counter check. Example: pinned queen pushes pinned pawn.
207 const res
= this.filterMoves(this.getPotentialMoves(/* TODO: args? */)).length
> 0;
214 // TODO: track rooks locations, should be a field in FEN, in castleflags?
215 // --> only useful if castleFlags is still ON
217 // TODO: if rook1 isn't at its place (with castleFlags ON), set it off
219 let moves
= super.getCastleMoves(sq
);
220 // TODO: restore castleFlags
223 // Does m2 un-do m1 ? (to disallow undoing actions)
224 oppositeMoves(m1
, m2
) {
225 const isEqual
= (av1
, av2
) => {
226 // Precondition: av1 and av2 length = 2
227 for (let av
of av1
) {
228 const avInAv2
= av2
.find(elt
=> {
236 if (!avInAv2
) return false;
242 m1
.appear
.length
== 2 &&
243 m2
.appear
.length
== 2 &&
244 m1
.vanish
.length
== 2 &&
245 m2
.vanish
.length
== 2 &&
246 isEqual(m1
.appear
, m2
.vanish
) &&
247 isEqual(m1
.vanish
, m2
.appear
)
252 if (moves
.length
== 0) return [];
253 const color
= this.turn
;
254 return moves
.filter(m
=> {
255 const L
= this.amoves
.length
; //at least 1: init from FEN
256 return !this.oppositeMoves(this.amoves
[L
- 1], m
);
260 isAttackedBySlideNJump([x
, y
], color
, piece
, steps
, oneStep
) {
261 for (let step
of steps
) {
262 let rx
= x
+ step
[0],
264 while (V
.OnBoard(rx
, ry
) && this.board
[rx
][ry
] == V
.EMPTY
&& !oneStep
) {
270 this.getPiece(rx
, ry
) == piece
&&
271 this.getColor(rx
, ry
) == color
273 // Now step in the other direction: if end of the world, then attacked
278 this.board
[rx
][ry
] == V
.EMPTY
&&
284 if (!V
.OnBoard(rx
, ry
)) return true;
290 isAttackedByPawn([x
, y
], color
) {
291 const lastRank
= (color
== 'w' ? 0 : 7);
293 // The king can be pushed out by a pawn only on last rank
295 const pawnShift
= (color
== "w" ? 1 : -1);
296 for (let i
of [-1, 1]) {
300 this.getPiece(x
+ pawnShift
, y
+ i
) == V
.PAWN
&&
301 this.getColor(x
+ pawnShift
, y
+ i
) == color
310 if (this.subTurn
== 2)
313 return super.getCurrentScore();
317 move.flags
= JSON
.stringify(this.aggregateFlags());
318 V
.PlayOnBoard(this.board
, move);
319 if (this.subTurn
== 1) {
320 // TODO: is there a second move possible?
321 // (if the first move is a normal one, there may be no actions available)
322 // --> If not, just change turn as ion the else {} section
327 this.turn
= V
.GetOppCol(this.turn
);
333 updateCastleFlags(move, piece
) {
334 const c
= V
.GetOppCol(this.turn
);
335 const firstRank
= (c
== "w" ? V
.size
.x
- 1 : 0);
336 // Update castling flags if rooks are moved (only)
337 if (piece
== V
.KING
&& move.appear
.length
> 0)
338 this.castleFlags
[c
] = [V
.size
.y
, V
.size
.y
];
340 move.start
.x
== firstRank
&&
341 this.castleFlags
[c
].includes(move.start
.y
)
343 const flagIdx
= (move.start
.y
== this.castleFlags
[c
][0] ? 0 : 1);
344 this.castleFlags
[c
][flagIdx
] = V
.size
.y
;
349 this.disaggregateFlags(JSON
.parse(move.flags
));
350 V
.UndoOnBoard(this.board
, move);
351 if (this.subTurn
== 2) {
356 // subTurn == 1 (after a move played)
357 this.turn
= V
.GetOppCol(this.turn
);