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 (amove
== "-") 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);
44 // Stack "first moves" (on subTurn 1) to merge and check opposite moves
48 static ParseFen(fen
) {
50 ChessRules
.ParseFen(fen
),
51 { cmove: fen
.split(" ")[4] }
55 static IsGoodFen(fen
) {
56 if (!ChessRules
.IsGoodFen(fen
)) return false;
57 const fenParts
= fen
.split(" ");
58 if (fenParts
.length
!= 6) return false;
59 if (fenParts
[5] != "-" && !fenParts
[5].match(/^([a-h][1-8]){2}$/))
64 getAmove(move1
, move2
) {
65 // Just merge (one is action one is move, one may be empty)
67 appear: move1
.appear
.concat(move2
.appear
),
68 vanish: move1
.vanish
.concat(move2
.vanish
)
73 // If subTurn == 2 && square is the final square of last move,
74 // then return an empty move
75 const L
= this.firstMove
.length
;
78 square
.x
== this.firstMove
[L
-1].end
.x
&&
79 square
.y
== this.firstMove
[L
-1].end
.y
90 // Captures don't occur (only pulls & pushes)
94 // TODO: re-think these next 3 methods:
95 // Idea = have the info about lastMove in lastMoves[L-1],
96 // In particular if moving a piece or doing an action.
98 // "pa" : piece (as a square) doing this push/pull action
99 getActionMoves([sx
, sy
], [ex
, ey
], pa
) {
100 const color
= this.getColor(sx
, sy
);
101 const lastRank
= (color
== 'w' ? 0 : 7);
102 const piece
= this.getPiece(sx
, sy
);
104 if (ex
== lastRank
&& piece
== V
.PAWN
) {
105 // Promotion by push or pull
106 V
.PawnSpecs
.promotions
.forEach(p
=> {
107 let move = super.getBasicMove([sx
, sy
], [ex
, ey
], { c: color
, p: p
});
110 } else moves
.push(super.getBasicMove([sx
, sy
], [ex
, ey
]));
113 Math
.abs(pa
[0] - sx
) < Math
.abs(pa
[0] - ex
) ||
114 Math
.abs(pa
[1] - sy
) < Math
.abs(pa
[1] - ey
)
118 moves
.forEach(m
=> m
.action
= [{ by: pa
, type: actionType
}]);
122 // TODO: if type is given, consider only actions of this type
123 getPactions(sq
, color
, type
) {
128 const oppCol
= V
.GetOppCol(color
);
129 // Look in all directions for a "color" piece
130 for (let step
of V
.steps
[V
.KNIGHT
]) {
131 const xx
= x
+ step
[0],
135 this.getPiece(xx
, yy
) == V
.KNIGHT
&&
136 this.getColor(xx
, yy
) == color
138 const px
= x
- step
[0],
140 if (V
.OnBoard(px
, py
)) {
141 if (this.board
[px
][py
] == V
.EMPTY
) {
142 const hash
= "s" + px
+ py
;
143 if (!squares
[hash
]) {
144 squares
[hash
] = true;
145 Array
.prototype.push
.apply(
147 this.getActionMoves([x
, y
], [px
, py
], [xx
, yy
])
150 else { //add piece doing action
154 const hash
= "s" + xx
+ yy
;
155 if (!squares
[hash
]) {
156 squares
[hash
] = true;
159 start: { x: x
, y: y
},
160 end: { x: xx
, y: yy
},
166 p: this.getPiece(x
, y
),
176 for (let step
in V
.steps
[V
.ROOK
]) {
177 // (+ if color is ours, pawn pushes) king, rook and queen
178 // --> pawns special case can push from a little distance if on 2nd rank (or 1st rank)
180 for (let step
in V
.steps
[V
.BISHOP
]) {
181 // King, bishop, queen, and possibly pawns attacks (if color is enemy)
187 // NOTE: to push a piece out of the board, make it slide until our piece
188 // (doing the action, moving or not)
189 // TODO: for pushes, play the pushed piece first.
190 // for pulls: play the piece doing the action first
191 // If castle, then no options available next (just re-click)
193 getPotentialMovesFrom([x
, y
]) {
194 const color
= this.turn
;
195 if (this.getColor(x
, y
) != color
)
196 // The only moves possible with enemy pieces are pulls and pushes:
197 return this.getPactions([x
, y
], color
);
198 // Playing my pieces: either on their own, or pushed by another
199 // If subTurn == 2 then we should have a first move,
200 // TODO = use it to allow some type of action
201 if (this.subTurn
== 2) {
203 this.moveOnSubturn1
.isAnAction
204 ? super.getPotentialMovesFrom([x
, y
])
205 : this.getPactions([x
, y
], color
, TODO_arg
)
208 // Both options are possible at subTurn1: normal move, or push
210 super.getPotentialMovesFrom([x
, y
])
211 .concat(this.getPactions([x
, y
], color
, "push"))
212 // TODO: discard moves that let the king underCheck, and no second
213 // move can counter check. Example: pinned queen pushes pinned pawn.
216 const res
= this.filterMoves(this.getPotentialMoves(/* TODO: args? */)).length
> 0;
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;
241 m1
.appear
.length
== 2 &&
242 m2
.appear
.length
== 2 &&
243 m1
.vanish
.length
== 2 &&
244 m2
.vanish
.length
== 2 &&
245 isEqual(m1
.appear
, m2
.vanish
) &&
246 isEqual(m1
.vanish
, m2
.appear
)
251 if (this.subTurn
== 1)
252 // Validity of subTurn 1 should be checked in getPotentialMoves
254 const L
= this.firstMove
.length
;
258 // Move shouldn't undo another:
259 return !this.oppositeMoves(this.firstMove
[L
-1], m
)
265 isAttackedBySlideNJump([x
, y
], color
, piece
, steps
, oneStep
) {
266 for (let step
of steps
) {
267 let rx
= x
+ step
[0],
269 while (V
.OnBoard(rx
, ry
) && this.board
[rx
][ry
] == V
.EMPTY
&& !oneStep
) {
275 this.getPiece(rx
, ry
) == piece
&&
276 this.getColor(rx
, ry
) == color
278 // Now step in the other direction: if end of the world, then attacked
283 this.board
[rx
][ry
] == V
.EMPTY
&&
289 if (!V
.OnBoard(rx
, ry
)) return true;
295 isAttackedByPawn([x
, y
], color
) {
296 const lastRank
= (color
== 'w' ? 0 : 7);
298 // The king can be pushed out by a pawn only on last rank
300 const pawnShift
= (color
== "w" ? 1 : -1);
301 for (let i
of [-1, 1]) {
305 this.getPiece(x
+ pawnShift
, y
+ i
) == V
.PAWN
&&
306 this.getColor(x
+ pawnShift
, y
+ i
) == color
315 if (this.subTurn
== 2)
318 return super.getCurrentScore();
322 move.flags
= JSON
.stringify(this.aggregateFlags());
323 V
.PlayOnBoard(this.board
, move);
324 if (this.subTurn
== 2) {
325 this.turn
= V
.GetOppCol(this.turn
);
328 else this.firstMove
.push(move);
329 this.subTurn
= 3 - this.subTurn
;
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
337 if (piece
== V
.KING
) this.castleFlags
[c
] = [V
.size
.y
, V
.size
.y
];
338 for (let v
of move.vanish
) {
339 if (v
.x
== firstRank
&& this.castleFlags
[c
].includes(v
.y
)) {
340 const flagIdx
= (v
.y
== this.castleFlags
[c
][0] ? 0 : 1);
341 this.castleFlags
[c
][flagIdx
] = V
.size
.y
;
347 this.disaggregateFlags(JSON
.parse(move.flags
));
348 V
.UndoOnBoard(this.board
, move);
349 if (this.subTurn
== 1) {
350 this.turn
= V
.GetOppCol(this.turn
);
353 else this.firstMove
.pop();
354 this.subTurn
= 3 - this.subTurn
;