'update'
[vchess.git] / client / src / variants / Joker.js
CommitLineData
459edd58
BA
1import { ChessRules, Move, PiPo } from "@/base_rules";
2import { Antiking2Rules } from "@/variants/Antiking2";
3
4export class JokerRules extends ChessRules {
5
6 static get PawnSpecs() {
7 return Object.assign(
8 {},
9 ChessRules.PawnSpecs,
10 { promotions: ChessRules.PawnSpecs.promotions.concat([V.JOKER]) }
11 );
12 }
13
4313762d
BA
14 static GenRandInitFen(options) {
15 const antikingFen = Antiking2Rules.GenRandInitFen(options);
459edd58
BA
16 return antikingFen.replace('a', 'J').replace('A', 'j');
17 }
18
19 static get JOKER() {
20 return 'j';
21 }
22
23 static get PIECES() {
24 return ChessRules.PIECES.concat([V.JOKER]);
25 }
26
27 getPpath(b) {
28 return (b.charAt(1) == 'j' ? "Joker/" : "") + b;
29 }
30
31 getPotentialMovesFrom([x, y]) {
32 const piece = this.getPiece(x, y);
33 if (piece == V.JOKER) return this.getPotentialJokerMoves([x, y]);
34 let moves = super.getPotentialMovesFrom([x, y]);
35 if (piece == V.PAWN) {
36 const c = this.turn;
37 const alreadyOneJoker = this.board.some(row =>
38 row.some(cell => cell == c + 'j'));
39 if (alreadyOneJoker) moves = moves.filter(m => m.appear[0].p != V.JOKER)
40 }
41 return moves;
42 }
43
44 canTake([x1, y1], [x2, y2]) {
45 if (this.getPiece(x1, y1) == V.JOKER) return false;
46 return super.canTake([x1, y1], [x2, y2]);
47 }
48
49 getPotentialJokerMoves([x, y]) {
50 const moving =
4313762d 51 super.getSlideNJumpMoves([x, y], V.steps[V.KNIGHT], 1)
459edd58
BA
52 .concat(super.getSlideNJumpMoves([x, y],
53 V.steps[V.ROOK].concat(V.steps[V.BISHOP])));
54 let swapping = [];
55 const c = this.turn;
56 for (let i=0; i<8; i++) {
57 for (let j=0; j<8; j++) {
58 // Following test is OK because only one Joker on board at a time
59 if (this.board[i][j] != V.EMPTY && this.getColor(i, j) == c) {
60 const p = this.getPiece(i, j);
4c3031a5
BA
61 const lastRank = (c == 'w' ? 0 : 7);
62 if (p != V.KING && (p != V.PAWN || x != lastRank)) {
63 swapping.push(
64 new Move({
65 vanish: [
66 new PiPo({ x: x, y: y, c: c, p: V.JOKER }),
67 new PiPo({ x: i, y: j, c: c, p: p })
68 ],
69 appear: [
70 new PiPo({ x: i, y: j, c: c, p: V.JOKER }),
71 new PiPo({ x: x, y: y, c: c, p: p })
72 ]
73 })
74 );
75 }
459edd58
BA
76 }
77 }
78 }
79 return moving.concat(swapping);
80 }
81
cee75a57
BA
82 postPlay(move) {
83 super.postPlay(move);
84 // Was my king swapped?
85 if (move.vanish.length == 2 && move.vanish[1].p == V.KING)
86 this.kingPos[move.appear[1].c] = [move.appear[1].x, move.appear[1].y];
87 }
88
89 postUndo(move) {
90 super.postUndo(move);
91 if (move.vanish.length == 2 && move.vanish[1].p == V.KING)
92 this.kingPos[move.vanish[1].c] = [move.vanish[1].x, move.vanish[1].y];
93 }
94
459edd58
BA
95 static get VALUES() {
96 return Object.assign({ j: 2 }, ChessRules.VALUES);
97 }
98
99 static get SEARCH_DEPTH() {
100 return 2;
101 }
102
103};