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