Fix Xiangqi + a few cosmetics
[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 {
7e8a7ea1 6
a97bdbda
BA
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
7ba4a5bc
BA
19 static GenRandInitFen(randomness) {
20 return ChessRules.GenRandInitFen(randomness)
21 .replace(/n/g, 'c').replace(/N/g, 'C');
a97bdbda
BA
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
68e19a44 45 isAttacked(sq, color) {
a97bdbda 46 return (
68e19a44
BA
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)
a97bdbda
BA
53 );
54 }
55
68e19a44 56 isAttackedByKing(sq, color) {
a97bdbda
BA
57 return this.isAttackedBySlideNJump(
58 sq,
68e19a44 59 color,
a97bdbda
BA
60 V.KING,
61 V.steps[V.KNIGHT],
62 "oneStep"
63 );
64 }
65
68e19a44 66 isAttackedByCommoner(sq, color) {
a97bdbda
BA
67 return this.isAttackedBySlideNJump(
68 sq,
68e19a44 69 color,
a97bdbda
BA
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 }
7e8a7ea1 86
a97bdbda 87};