Fix Progressive2. Fixing attempt on Doublemove1
[vchess.git] / client / src / variants / Knightmate.js
CommitLineData
a97bdbda
BA
1import { ChessRules } from "@/base_rules";
2import { ArrayFun } from "@/utils/array";
3import { randInt } from "@/utils/alea";
4
32f6285e 5export class KnightmateRules extends ChessRules {
a97bdbda
BA
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
7ba4a5bc
BA
18 static GenRandInitFen(randomness) {
19 return ChessRules.GenRandInitFen(randomness)
20 .replace(/n/g, 'c').replace(/N/g, 'C');
a97bdbda
BA
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
68e19a44 44 isAttacked(sq, color) {
a97bdbda 45 return (
68e19a44
BA
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)
a97bdbda
BA
52 );
53 }
54
68e19a44 55 isAttackedByKing(sq, color) {
a97bdbda
BA
56 return this.isAttackedBySlideNJump(
57 sq,
68e19a44 58 color,
a97bdbda
BA
59 V.KING,
60 V.steps[V.KNIGHT],
61 "oneStep"
62 );
63 }
64
68e19a44 65 isAttackedByCommoner(sq, color) {
a97bdbda
BA
66 return this.isAttackedBySlideNJump(
67 sq,
68e19a44 68 color,
a97bdbda
BA
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};