Better 8pieces rules. Almost there (still buggish for sentries pushes)
[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
5export const VariantRules = 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
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
44 isAttacked(sq, colors) {
45 return (
46 this.isAttackedByCommoner(sq, colors) ||
47 this.isAttackedByPawn(sq, colors) ||
48 this.isAttackedByRook(sq, colors) ||
49 this.isAttackedByBishop(sq, colors) ||
50 this.isAttackedByQueen(sq, colors) ||
51 this.isAttackedByKing(sq, colors)
52 );
53 }
54
55 isAttackedByKing(sq, colors) {
56 return this.isAttackedBySlideNJump(
57 sq,
58 colors,
59 V.KING,
60 V.steps[V.KNIGHT],
61 "oneStep"
62 );
63 }
64
65 isAttackedByCommoner(sq, colors) {
66 return this.isAttackedBySlideNJump(
67 sq,
68 colors,
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};