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