Some fixes and enhancements as suggested on Discord
[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
28b32b4f
BA
20 static get LANCER_DIRS() {
21 return {
22 'c': [-1, 0], //north
23 'd': [-1, 1], //N-E
24 'e': [0, 1], //east
25 'f': [1, 1], //S-E
26 'g': [1, 0], //south
27 'h': [1, -1], //S-W
28 'm': [0, -1], //west
29 'o': [-1, -1] //N-W
30 };
31 }
32
33 getPiece(i, j) {
34 const piece = this.board[i][j].charAt(1);
35 // Special lancer case: 8 possible orientations
36 if (Object.keys(V.LANCER_DIRS).includes(piece)) return V.LANCER;
37 return piece;
38 }
39
2a8a94c9 40 getPpath(b) {
2a8a94c9
BA
41 return (
42 ([V.JAILER, V.SENTRY, V.LANCER].includes(b[1])
43 ? "Eightpieces/" : "") + b
44 );
45 }
46
28b32b4f
BA
47 setOtherVariables(fen) {
48 super.setOtherVariables(fen);
49 // subTurn == 2 only when a sentry moved, and is about to push something
50 this.subTurn = 1;
51 // Stack pieces' forbidden squares after a sentry move at each turn
52 this.sentryPath = [];
2a8a94c9
BA
53 }
54
28b32b4f
BA
55 canTake([x1,y1], [x2, y2]) {
56 if (this.subTurn == 2)
57 // Sentry push: pieces can capture own color (only)
58 return this.getColor(x1, y1) == this.getColor(x2, y2);
59 return super.canTake([x1,y1], [x2, y2]);
2a8a94c9
BA
60 }
61
62 static GenRandInitFen(randomness) {
63 // TODO: special conditions
64 }
65
28b32b4f
BA
66 // TODO: rook + jailer
67 scanKingsRooks(fen) {
68 this.kingPos = { w: [-1, -1], b: [-1, -1] };
69 const fenRows = V.ParseFen(fen).position.split("/");
70 for (let i = 0; i < fenRows.length; i++) {
71 let k = 0; //column index on board
72 for (let j = 0; j < fenRows[i].length; j++) {
73 switch (fenRows[i].charAt(j)) {
74 case "k":
75 case "l":
76 this.kingPos["b"] = [i, k];
77 break;
78 case "K":
79 case "L":
80 this.kingPos["w"] = [i, k];
81 break;
82 default: {
83 const num = parseInt(fenRows[i].charAt(j));
84 if (!isNaN(num)) k += num - 1;
85 }
86 }
87 k++;
88 }
89 }
2a8a94c9
BA
90 }
91
28b32b4f
BA
92 getPotentialMovesFrom([x,y]) {
93 // if subTurn == 2, allow only
2a8a94c9
BA
94 }
95
28b32b4f
BA
96 // getPotentialMoves, isAttacked: TODO
97 getPotentialCastleMoves(sq) { //TODO: adapt, with jailer
2a8a94c9
BA
98 }
99
28b32b4f
BA
100 updateVariables(move) {
101 // TODO: stack sentryPath if subTurn == 2 --> all squares between move.start et move.end, sauf si c'est un pion
2a8a94c9
BA
102 }
103
28b32b4f 104 // TODO: special pass move: take jailer with king
2a8a94c9 105
28b32b4f
BA
106 // subTurn : if sentry moved to some enemy piece --> enregistrer déplacement sentry, subTurn == 2, puis déplacer pièce adverse --> 1st 1/2 of turn, vanish sentry tout simplement.
107 // --> le turn ne change pas !
108 // 2nd half: move only
109 // FEN flag: sentryPath from init pushing to final enemy square --> forbid some moves (getPotentialMoves)
2a8a94c9
BA
110
111 static get VALUES() {
112 return Object.assign(
28b32b4f 113 { l: 4.8, s: 2.8, j: 3.8 }, //Jeff K. estimations
2a8a94c9
BA
114 ChessRules.VALUES
115 );
116 }
117};