Attempt to clarify installation instructions a little
[vchess.git] / client / src / variants / Zen.js
CommitLineData
0c3fe8a6
BA
1import { ChessRules } from "@/base_rules";
2
32f6285e 3export class ZenRules extends ChessRules {
7e8a7ea1 4
ffeaef85
BA
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;
6808d7a1 28 }
dac39588 29
dac39588
BA
30 // follow steps from x,y until something is met.
31 // if met piece is opponent and same movement (asA): eat it!
6808d7a1
BA
32 findCaptures_aux([x, y], asA) {
33 const color = this.getColor(x, y);
9bd6786b 34 let moves = [];
6808d7a1
BA
35 const steps =
36 asA != V.PAWN
37 ? asA == V.QUEEN
38 ? V.steps[V.ROOK].concat(V.steps[V.BISHOP])
39 : V.steps[asA]
40 : color == "w"
41 ? [
42 [-1, -1],
43 [-1, 1]
44 ]
45 : [
46 [1, -1],
47 [1, 1]
48 ];
9bd6786b 49 const oneStep = [V.KNIGHT,V.PAWN].includes(asA); //we don't capture king
6808d7a1
BA
50 const lastRank = color == "w" ? 0 : V.size.x - 1;
51 const promotionPieces = [V.ROOK, V.KNIGHT, V.BISHOP, V.QUEEN];
8ca6042e 52 const oppCol = V.GetOppCol(color);
6808d7a1 53 outerLoop: for (let loop = 0; loop < steps.length; loop++) {
dac39588
BA
54 const step = steps[loop];
55 let i = x + step[0];
56 let j = y + step[1];
6808d7a1
BA
57 while (V.OnBoard(i, j) && this.board[i][j] == V.EMPTY) {
58 if (oneStep) continue outerLoop;
dac39588
BA
59 i += step[0];
60 j += step[1];
61 }
6808d7a1
BA
62 if (
63 V.OnBoard(i, j) &&
0f7762c1 64 this.board[i][j] != V.EMPTY &&
8ca6042e 65 this.getColor(i, j) == oppCol &&
6808d7a1
BA
66 this.getPiece(i, j) == asA
67 ) {
dac39588 68 // eat!
6808d7a1 69 if (this.getPiece(x, y) == V.PAWN && i == lastRank) {
dac39588
BA
70 // Special case of promotion:
71 promotionPieces.forEach(p => {
6808d7a1 72 moves.push(this.getBasicMove([x, y], [i, j], { c: color, p: p }));
dac39588 73 });
0f7762c1
BA
74 }
75 else {
dac39588 76 // All other cases
6808d7a1 77 moves.push(this.getBasicMove([x, y], [i, j]));
dac39588
BA
78 }
79 }
80 }
81 return moves;
82 }
83
84 // Find possible captures from a square: look in every direction!
6808d7a1 85 findCaptures(sq) {
dac39588 86 let moves = [];
dac39588
BA
87 Array.prototype.push.apply(moves, this.findCaptures_aux(sq, V.PAWN));
88 Array.prototype.push.apply(moves, this.findCaptures_aux(sq, V.ROOK));
89 Array.prototype.push.apply(moves, this.findCaptures_aux(sq, V.KNIGHT));
90 Array.prototype.push.apply(moves, this.findCaptures_aux(sq, V.BISHOP));
91 Array.prototype.push.apply(moves, this.findCaptures_aux(sq, V.QUEEN));
dac39588
BA
92 return moves;
93 }
94
4313762d 95 canTake() {
e9b736ee
BA
96 return false; //captures handled separately
97 }
dac39588 98
8ca6042e
BA
99 getPotentialMovesFrom(sq) {
100 return super.getPotentialMovesFrom(sq).concat(this.findCaptures(sq));
dac39588
BA
101 }
102
6808d7a1 103 getNotation(move) {
dac39588 104 // Recognize special moves first
6808d7a1 105 if (move.appear.length == 2) {
dac39588 106 // castle
6808d7a1
BA
107 if (move.end.y < move.start.y) return "0-0-0";
108 return "0-0";
dac39588
BA
109 }
110
2c5d7b20 111 // Translate initial square (because pieces may fly unusually!)
dac39588
BA
112 const initialSquare = V.CoordsToSquare(move.start);
113
114 // Translate final square
115 const finalSquare = V.CoordsToSquare(move.end);
116
117 let notation = "";
118 const piece = this.getPiece(move.start.x, move.start.y);
6808d7a1 119 if (piece == V.PAWN) {
dac39588 120 // pawn move (TODO: enPassant indication)
8ca6042e 121 if (move.vanish.length == 2) {
dac39588
BA
122 // capture
123 notation = initialSquare + "x" + finalSquare;
8ca6042e 124 }
6808d7a1
BA
125 else notation = finalSquare;
126 if (piece != move.appear[0].p)
127 //promotion
dac39588 128 notation += "=" + move.appear[0].p.toUpperCase();
8ca6042e
BA
129 }
130 else {
dac39588
BA
131 // Piece movement
132 notation = piece.toUpperCase();
6808d7a1 133 if (move.vanish.length > 1) notation += initialSquare + "x";
dac39588
BA
134 notation += finalSquare;
135 }
136 return notation;
137 }
138
6808d7a1 139 static get VALUES() {
dac39588 140 return {
6808d7a1
BA
141 p: 1,
142 r: 3,
143 n: 2,
144 b: 2,
145 q: 5,
146 k: 1000
147 };
dac39588 148 }
7e8a7ea1 149
6808d7a1 150};