Fix Fullcavalry + complete rules. Also complete Atomic2 rules
[vchess.git] / client / src / variants / Zen.js
1 import { ChessRules } from "@/base_rules";
2
3 export class ZenRules extends ChessRules {
4
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);
11 }
12 const move = moveOrSquare;
13 const s = move.start,
14 e = move.end;
15 if (
16 // Exclude captures (of rooks for example)
17 move.vanish.length == 1 &&
18 s.y == e.y &&
19 Math.abs(s.x - e.x) == 2 &&
20 move.appear[0].p == V.PAWN
21 ) {
22 return {
23 x: (s.x + e.x) / 2,
24 y: s.y
25 };
26 }
27 return undefined;
28 }
29
30 // TODO(?): some duplicated code in 2 next functions
31 getSlideNJumpMoves([x, y], steps, oneStep) {
32 let moves = [];
33 outerLoop: for (let loop = 0; loop < steps.length; loop++) {
34 const step = steps[loop];
35 let i = x + step[0];
36 let j = y + step[1];
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;
40 i += step[0];
41 j += step[1];
42 }
43 // No capture check: handled elsewhere (next method)
44 }
45 return moves;
46 }
47
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);
52 let moves = [];
53 const steps =
54 asA != V.PAWN
55 ? asA == V.QUEEN
56 ? V.steps[V.ROOK].concat(V.steps[V.BISHOP])
57 : V.steps[asA]
58 : color == "w"
59 ? [
60 [-1, -1],
61 [-1, 1]
62 ]
63 : [
64 [1, -1],
65 [1, 1]
66 ];
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];
73 let i = x + step[0];
74 let j = y + step[1];
75 while (V.OnBoard(i, j) && this.board[i][j] == V.EMPTY) {
76 if (oneStep) continue outerLoop;
77 i += step[0];
78 j += step[1];
79 }
80 if (
81 V.OnBoard(i, j) &&
82 this.getColor(i, j) == oppCol &&
83 this.getPiece(i, j) == asA
84 ) {
85 // eat!
86 if (this.getPiece(x, y) == V.PAWN && i == lastRank) {
87 // Special case of promotion:
88 promotionPieces.forEach(p => {
89 moves.push(this.getBasicMove([x, y], [i, j], { c: color, p: p }));
90 });
91 } else {
92 // All other cases
93 moves.push(this.getBasicMove([x, y], [i, j]));
94 }
95 }
96 }
97 return moves;
98 }
99
100 // Find possible captures from a square: look in every direction!
101 findCaptures(sq) {
102 let moves = [];
103 Array.prototype.push.apply(moves, this.findCaptures_aux(sq, V.PAWN));
104 Array.prototype.push.apply(moves, this.findCaptures_aux(sq, V.ROOK));
105 Array.prototype.push.apply(moves, this.findCaptures_aux(sq, V.KNIGHT));
106 Array.prototype.push.apply(moves, this.findCaptures_aux(sq, V.BISHOP));
107 Array.prototype.push.apply(moves, this.findCaptures_aux(sq, V.QUEEN));
108 return moves;
109 }
110
111 canTake(sq1, sq2) {
112 return false; //captures handled separately
113 }
114
115 getPotentialMovesFrom(sq) {
116 return super.getPotentialMovesFrom(sq).concat(this.findCaptures(sq));
117 }
118
119 getNotation(move) {
120 // Recognize special moves first
121 if (move.appear.length == 2) {
122 // castle
123 if (move.end.y < move.start.y) return "0-0-0";
124 return "0-0";
125 }
126
127 // Translate initial square (because pieces may fly unusually!)
128 const initialSquare = V.CoordsToSquare(move.start);
129
130 // Translate final square
131 const finalSquare = V.CoordsToSquare(move.end);
132
133 let notation = "";
134 const piece = this.getPiece(move.start.x, move.start.y);
135 if (piece == V.PAWN) {
136 // pawn move (TODO: enPassant indication)
137 if (move.vanish.length == 2) {
138 // capture
139 notation = initialSquare + "x" + finalSquare;
140 }
141 else notation = finalSquare;
142 if (piece != move.appear[0].p)
143 //promotion
144 notation += "=" + move.appear[0].p.toUpperCase();
145 }
146 else {
147 // Piece movement
148 notation = piece.toUpperCase();
149 if (move.vanish.length > 1) notation += initialSquare + "x";
150 notation += finalSquare;
151 }
152 return notation;
153 }
154
155 static get VALUES() {
156 return {
157 p: 1,
158 r: 3,
159 n: 2,
160 b: 2,
161 q: 5,
162 k: 1000
163 };
164 }
165
166 };