1 import { ChessRules
} from "@/base_rules";
3 export class RampageRules
extends ChessRules
{
4 // Sum white pieces attacking a square, and remove black pieces count.
6 const getSign
= (color
) => {
7 return (color
== 'w' ? 1 : -1);
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
));
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
));
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
];
29 this.getPiece(i
, j
) == V
.PAWN
&&
30 this.getColor(i
, j
) == c
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
);
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
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
]);
73 ((color
== 'w' && sa
> 0) || (color
== 'b' && sa
< 0)) &&
74 (piece
!= V
.PAWN
|| i
!= lastRank
)
76 moves
.push(this.getBasicMove([x
, y
], [i
, j
]));
84 static get SEARCH_DEPTH() {