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