1 import { ChessRules
} from "@/base_rules";
3 export class ZenRules
extends ChessRules
{
5 getEpSquare(moveOrSquare
) {
6 if (!moveOrSquare
) return undefined;
7 if (typeof moveOrSquare
=== "string") {
8 const square
= moveOrSquare
;
9 if (square
== "-") return undefined;
10 return V
.SquareToCoords(square
);
12 const move = moveOrSquare
;
16 // Exclude captures (of rooks for example)
17 move.vanish
.length
== 1 &&
19 Math
.abs(s
.x
- e
.x
) == 2 &&
20 move.appear
[0].p
== V
.PAWN
30 // TODO(?): some duplicated code in 2 next functions
31 getSlideNJumpMoves([x
, y
], steps
, oneStep
) {
33 outerLoop: for (let loop
= 0; loop
< steps
.length
; loop
++) {
34 const step
= steps
[loop
];
37 while (V
.OnBoard(i
, j
) && this.board
[i
][j
] == V
.EMPTY
) {
38 moves
.push(this.getBasicMove([x
, y
], [i
, j
]));
39 if (oneStep
) continue outerLoop
;
43 // No capture check: handled elsewhere (next method)
48 // follow steps from x,y until something is met.
49 // if met piece is opponent and same movement (asA): eat it!
50 findCaptures_aux([x
, y
], asA
) {
51 const color
= this.getColor(x
, y
);
56 ? V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
])
67 const oneStep
= [V
.KNIGHT
,V
.PAWN
].includes(asA
); //we don't capture king
68 const lastRank
= color
== "w" ? 0 : V
.size
.x
- 1;
69 const promotionPieces
= [V
.ROOK
, V
.KNIGHT
, V
.BISHOP
, V
.QUEEN
];
70 const oppCol
= V
.GetOppCol(color
);
71 outerLoop: for (let loop
= 0; loop
< steps
.length
; loop
++) {
72 const step
= steps
[loop
];
75 while (V
.OnBoard(i
, j
) && this.board
[i
][j
] == V
.EMPTY
) {
76 if (oneStep
) continue outerLoop
;
82 this.board
[i
][j
] != V
.EMPTY
&&
83 this.getColor(i
, j
) == oppCol
&&
84 this.getPiece(i
, j
) == asA
87 if (this.getPiece(x
, y
) == V
.PAWN
&& i
== lastRank
) {
88 // Special case of promotion:
89 promotionPieces
.forEach(p
=> {
90 moves
.push(this.getBasicMove([x
, y
], [i
, j
], { c: color
, p: p
}));
95 moves
.push(this.getBasicMove([x
, y
], [i
, j
]));
102 // Find possible captures from a square: look in every direction!
105 Array
.prototype.push
.apply(moves
, this.findCaptures_aux(sq
, V
.PAWN
));
106 Array
.prototype.push
.apply(moves
, this.findCaptures_aux(sq
, V
.ROOK
));
107 Array
.prototype.push
.apply(moves
, this.findCaptures_aux(sq
, V
.KNIGHT
));
108 Array
.prototype.push
.apply(moves
, this.findCaptures_aux(sq
, V
.BISHOP
));
109 Array
.prototype.push
.apply(moves
, this.findCaptures_aux(sq
, V
.QUEEN
));
114 return false; //captures handled separately
117 getPotentialMovesFrom(sq
) {
118 return super.getPotentialMovesFrom(sq
).concat(this.findCaptures(sq
));
122 // Recognize special moves first
123 if (move.appear
.length
== 2) {
125 if (move.end
.y
< move.start
.y
) return "0-0-0";
129 // Translate initial square (because pieces may fly unusually!)
130 const initialSquare
= V
.CoordsToSquare(move.start
);
132 // Translate final square
133 const finalSquare
= V
.CoordsToSquare(move.end
);
136 const piece
= this.getPiece(move.start
.x
, move.start
.y
);
137 if (piece
== V
.PAWN
) {
138 // pawn move (TODO: enPassant indication)
139 if (move.vanish
.length
== 2) {
141 notation
= initialSquare
+ "x" + finalSquare
;
143 else notation
= finalSquare
;
144 if (piece
!= move.appear
[0].p
)
146 notation
+= "=" + move.appear
[0].p
.toUpperCase();
150 notation
= piece
.toUpperCase();
151 if (move.vanish
.length
> 1) notation
+= initialSquare
+ "x";
152 notation
+= finalSquare
;
157 static get VALUES() {