Small fixes + early draft of Eightpieces variant
[vchess.git] / client / src / variants / Eightpieces.js
CommitLineData
2a8a94c9
BA
1import { ArrayFun } from "@/utils/array";
2import { randInt, shuffle } from "@/utils/alea";
3import { ChessRules, PiPo, Move } from "@/base_rules";
4
5export const VariantRules = class EightpiecesRules extends ChessRules {
6 static get JAILER() {
7 return "j";
8 }
9 static get SENTRY() {
10 return "s";
11 }
12 static get LANCER() {
13 return "l";
14 }
15
16 static get PIECES() {
17 return ChessRules.PIECES.concat([V.JAILER, V.SENTRY, V.LANCER]);
18 }
19
20 getPpath(b) {
21 // TODO: more subtle, path depends on the orientations
22 // lancerOrientations should probably be a 8x8 array, for speed.
23 return (
24 ([V.JAILER, V.SENTRY, V.LANCER].includes(b[1])
25 ? "Eightpieces/" : "") + b
26 );
27 }
28
29 static IsGoodFen(fen) {
30 if (!ChessRules.IsGoodFen(fen)) return false;
31 const fenParsed = V.ParseFen(fen);
32 // 5) Check lancers orientations (if there are any left)
33 if (
34 !fenParsed.lancers ||
35 (
36 fenParsed.lancers != "-" &&
37 !fenParsed.lancers.match(/^([a-h][1-8][0-7],?)+$/)
38 )
39 ) {
40 return false;
41 }
42 return true;
43 }
44
45 static ParseFen(fen) {
46 const fenParts = fen.split(" ");
47 return Object.assign(ChessRules.ParseFen(fen), {
48 lancers: fenParts[5],
49 });
50 }
51
52 static GenRandInitFen(randomness) {
53 // TODO: special conditions
54 }
55
56 getFen() {
57 return (
58 super.getFen() + " " + this.getLancersFen()
59 );
60 }
61
62 getFenForRepeat() {
63 return (
64 this.getBaseFen() + "_" +
65 this.getTurnFen() + "_" +
66 this.getFlagsFen() + "_" +
67 this.getEnpassantFen() + "_" +
68 this.getLancersFen()
69 );
70 }
71
72 getLancersFen() {
73 let res = "";
74 this.lancerOrientations.forEach(o => {
75 res += V.CoordsToSquare(o.sq) + o.dir + ",";
76 });
77 res = res.slice(0, -1);
78 return res || "-";
79 }
80
81 setOtherVariables(fen) {
82 super.setOtherVariables(fen);
83 const fenParsed = V.ParseFen(fen);
84 // Also init lancer orientations (from FEN):
85 this.lancerOrientations = 32; // TODO
86 }
87
88 // getPotentialMoves, isAttacked: TODO
89
90 // updatedVariables: update lancers' orientations
91
92 // subTurn : if sentry moved to some enemy piece.
93
94 static get VALUES() {
95 return Object.assign(
96 { l: 5, s: 4, j: 5 }, //experimental
97 ChessRules.VALUES
98 );
99 }
100};