A few bug fixes
[vchess.git] / client / src / variants / Pacifist2.js
1 import { Pacifist1Rules } from "@/variants/Pacifist1";
2
3 export class Pacifist2Rules extends Pacifist1Rules {
4
5 // Sum values of white pieces attacking a square,
6 // and remove (sum of) black pieces values.
7 sumAttacks([x, y]) {
8 const getSign = (color) => {
9 return (color == 'w' ? 1 : -1);
10 };
11 let res = 0;
12 // Knights:
13 V.steps[V.KNIGHT].forEach(s => {
14 const [i, j] = [x + s[0], y + s[1]];
15 if (V.OnBoard(i, j) && this.getPiece(i, j) == V.KNIGHT)
16 res += getSign(this.getColor(i, j)) * V.VALUES[V.KNIGHT];
17 });
18 // Kings:
19 V.steps[V.ROOK].concat(V.steps[V.BISHOP]).forEach(s => {
20 const [i, j] = [x + s[0], y + s[1]];
21 if (V.OnBoard(i, j) && this.getPiece(i, j) == V.KING)
22 res += getSign(this.getColor(i, j)) * V.VALUES[V.KING];
23 });
24 // Pawns:
25 for (let c of ['w', 'b']) {
26 for (let shift of [-1, 1]) {
27 const sign = getSign(c);
28 const [i, j] = [x + sign, y + shift];
29 if (
30 V.OnBoard(i, j) &&
31 this.getPiece(i, j) == V.PAWN &&
32 this.getColor(i, j) == c
33 ) {
34 res += sign * V.VALUES[V.PAWN];
35 }
36 }
37 }
38 // Other pieces (sliders):
39 V.steps[V.ROOK].concat(V.steps[V.BISHOP]).forEach(s => {
40 let [i, j] = [x + s[0], y + s[1]];
41 let compatible = [V.QUEEN];
42 compatible.push(s[0] == 0 || s[1] == 0 ? V.ROOK : V.BISHOP);
43 let firstCol = undefined;
44 while (V.OnBoard(i, j)) {
45 if (this.board[i][j] != V.EMPTY) {
46 const pieceIJ = this.getPiece(i, j);
47 if (!(compatible.includes(pieceIJ))) break;
48 const colIJ = this.getColor(i, j);
49 if (!firstCol) firstCol = colIJ;
50 if (colIJ == firstCol) res += getSign(colIJ) * V.VALUES[pieceIJ];
51 else break;
52 }
53 i += s[0];
54 j += s[1];
55 }
56 });
57 return res;
58 }
59
60 };