Fix bug when editing a problem
[vchess.git] / client / src / variants / Knightmate.js
1 import { ChessRules } from "@/base_rules";
2 import { ArrayFun } from "@/utils/array";
3 import { randInt } from "@/utils/alea";
4
5 export class KnightmateRules extends ChessRules {
6 static get COMMONER() {
7 return "c";
8 }
9
10 static get PIECES() {
11 return ChessRules.PIECES.concat([V.COMMONER]);
12 }
13
14 getPpath(b) {
15 return ([V.KING, V.COMMONER].includes(b[1]) ? "Knightmate/" : "") + b;
16 }
17
18 static GenRandInitFen(randomness) {
19 return ChessRules.GenRandInitFen(randomness)
20 .replace(/n/g, 'c').replace(/N/g, 'C');
21 }
22
23 getPotentialMovesFrom([x, y]) {
24 switch (this.getPiece(x, y)) {
25 case V.COMMONER:
26 return this.getPotentialCommonerMoves([x, y]);
27 default:
28 return super.getPotentialMovesFrom([x, y]);
29 }
30 }
31
32 getPotentialCommonerMoves(sq) {
33 return this.getSlideNJumpMoves(
34 sq,
35 V.steps[V.ROOK].concat(V.steps[V.BISHOP]),
36 "oneStep"
37 );
38 }
39
40 getPotentialKingMoves(sq) {
41 return super.getPotentialKnightMoves(sq).concat(super.getCastleMoves(sq));
42 }
43
44 isAttacked(sq, color) {
45 return (
46 this.isAttackedByCommoner(sq, color) ||
47 this.isAttackedByPawn(sq, color) ||
48 this.isAttackedByRook(sq, color) ||
49 this.isAttackedByBishop(sq, color) ||
50 this.isAttackedByQueen(sq, color) ||
51 this.isAttackedByKing(sq, color)
52 );
53 }
54
55 isAttackedByKing(sq, color) {
56 return this.isAttackedBySlideNJump(
57 sq,
58 color,
59 V.KING,
60 V.steps[V.KNIGHT],
61 "oneStep"
62 );
63 }
64
65 isAttackedByCommoner(sq, color) {
66 return this.isAttackedBySlideNJump(
67 sq,
68 color,
69 V.COMMONER,
70 V.steps[V.ROOK].concat(V.steps[V.BISHOP]),
71 "oneStep"
72 );
73 }
74
75 static get VALUES() {
76 return {
77 p: 1,
78 r: 5,
79 c: 5, //the commoner is valuable
80 b: 3,
81 q: 9,
82 k: 1000
83 };
84 }
85 };