Fix Progressive2. Fixing attempt on Doublemove1
[vchess.git] / client / src / variants / Rampage.js
1 import { ChessRules } from "@/base_rules";
2
3 export class RampageRules extends ChessRules {
4 // Sum white pieces attacking a square, and remove black pieces count.
5 sumAttacks([x, y]) {
6 const getSign = (color) => {
7 return (color == 'w' ? 1 : -1);
8 };
9 let res = 0;
10 // Knights:
11 V.steps[V.KNIGHT].forEach(s => {
12 const [i, j] = [x + s[0], y + s[1]];
13 if (V.OnBoard(i, j) && this.getPiece(i, j) == V.KNIGHT)
14 res += getSign(this.getColor(i, j));
15 });
16 // Kings:
17 V.steps[V.ROOK].concat(V.steps[V.BISHOP]).forEach(s => {
18 const [i, j] = [x + s[0], y + s[1]];
19 if (V.OnBoard(i, j) && this.getPiece(i, j) == V.KING)
20 res += getSign(this.getColor(i, j));
21 });
22 // Pawns:
23 for (let c of ['w', 'b']) {
24 for (let shift of [-1, 1]) {
25 const sign = getSign(c);
26 const [i, j] = [x + sign, y + shift];
27 if (
28 V.OnBoard(i, j) &&
29 this.getPiece(i, j) == V.PAWN &&
30 this.getColor(i, j) == c
31 ) {
32 res += sign;
33 }
34 }
35 }
36 // Other pieces (sliders):
37 V.steps[V.ROOK].concat(V.steps[V.BISHOP]).forEach(s => {
38 let [i, j] = [x + s[0], y + s[1]];
39 let compatible = [V.QUEEN];
40 compatible.push(s[0] == 0 || s[1] == 0 ? V.ROOK : V.BISHOP);
41 let firstCol = undefined;
42 while (V.OnBoard(i, j)) {
43 if (this.board[i][j] != V.EMPTY) {
44 if (!(compatible.includes(this.getPiece(i, j)))) break;
45 const colIJ = this.getColor(i, j);
46 if (!firstCol) firstCol = colIJ;
47 if (colIJ == firstCol) res += getSign(colIJ);
48 else break;
49 }
50 i += s[0];
51 j += s[1];
52 }
53 });
54 return res;
55 }
56
57 getPotentialMovesFrom([x, y]) {
58 let moves = super.getPotentialMovesFrom([x, y]);
59 const color = this.turn;
60 if (this.getPiece(x, y) == V.KING && this.underCheck(color))
61 // The king under check can only move as usual
62 return moves;
63 // Remember current final squares to not add moves twice:
64 const destinations = {};
65 const lastRank = (color == 'w' ? 0 : 7);
66 const piece = this.getPiece(x, y);
67 moves.forEach(m => destinations[m.end.x + "_" + m.end.y] = true);
68 for (let i=0; i<8; i++) {
69 for (let j=0; j<8; j++) {
70 if (this.board[i][j] == V.EMPTY && !destinations[i + "_" + j]) {
71 const sa = this.sumAttacks([i, j]);
72 if (
73 ((color == 'w' && sa > 0) || (color == 'b' && sa < 0)) &&
74 (piece != V.PAWN || i != lastRank)
75 ) {
76 moves.push(this.getBasicMove([x, y], [i, j]));
77 }
78 }
79 }
80 }
81 return moves;
82 }
83
84 static get SEARCH_DEPTH() {
85 return 1;
86 }
87 };