Small fixes
[vchess.git] / client / src / variants / Pandemonium1.js
CommitLineData
278a28a1
BA
1import { Pandemonium2Rules } from "@/variants/Pandemonium2";
2
3export class Pandemonium1Rules extends Pandemonium2Rules {
4
5 static get PawnSpecs() {
6 return Object.assign(
7 { },
8 Pandemonium2Rules.PawnSpecs,
9 { threeSquares: true }
10 );
11 }
12
13 static get size() {
14 return { x: 10, y: 10};
15 }
16
17 static IsGoodEnpassant(enpassant) {
18 if (enpassant != "-") {
19 const squares = enpassant.split(",");
20 if (squares.length > 2) return false;
21 for (let sq of squares) {
22 if (!sq.match(/[a-j0-9]/)) return false;
23 }
24 }
25 return true;
26 }
27
4313762d
BA
28 static GenRandInitFen(options) {
29 const baseFen = Pandemonium2Rules.GenRandInitFen(options)
278a28a1
BA
30 return baseFen.substr(0, 22) + "91/91/" + baseFen.substr(22);
31 }
32
33 getEnpassantFen() {
34 const L = this.epSquares.length;
35 if (!this.epSquares[L - 1]) return "-"; //no en-passant
36 let res = "";
37 this.epSquares[L - 1].forEach(sq => {
38 res += V.CoordsToSquare(sq) + ",";
39 });
40 return res.slice(0, -1); //remove last comma
41 }
42
43 getEpSquare(moveOrSquare) {
44 if (!moveOrSquare) return undefined;
45 if (typeof moveOrSquare === "string") {
46 const square = moveOrSquare;
47 if (square == "-") return undefined;
48 let res = [];
49 square.split(",").forEach(sq => {
50 res.push(V.SquareToCoords(sq));
51 });
52 return res;
53 }
54 // Argument is a move:
55 const move = moveOrSquare;
56 const [sx, sy, ex] = [move.start.x, move.start.y, move.end.x];
57 if (this.getPiece(sx, sy) == V.PAWN && Math.abs(sx - ex) >= 2) {
58 const step = (ex - sx) / Math.abs(ex - sx);
59 let res = [{
60 x: sx + step,
61 y: sy
62 }];
63 if (sx + 2 * step != ex) {
64 // 3-squares jump
65 res.push({
66 x: sx + 2 * step,
67 y: sy
68 });
69 }
70 return res;
71 }
72 return undefined; //default
73 }
74
75 applyPromotions(moves, promoted) {
76 const lastRanks = (this.turn == 'w' ? [0, 1] : [V.size.x - 1, V.size.x]);
77 let promotions = [];
78 moves.forEach(m => {
79 if (lastRanks.includes(m.start.x) || lastRanks.includes(m.end.x)) {
80 let pMove = JSON.parse(JSON.stringify(m));
81 pMove.appear[0].p = promoted;
82 promotions.push(pMove);
83 }
84 });
85 Array.prototype.push.apply(moves, promotions);
86 }
87
88 addPawnMoves([x1, y1], [x2, y2], moves) {
89 const color = this.turn;
90 const lastRanks = (color == "w" ? [0, 1] : [V.size.x - 1, V.size.x - 2]);
91 if (!lastRanks.includes(x2)) {
92 moves.push(this.getBasicMove([x1, y1], [x2, y2]));
93 return;
94 }
95 let finalPieces = [V.GILDING];
96 if (x2 == lastRanks[1]) finalPieces.push(V.PAWN);
97 for (let piece of finalPieces) {
98 const tr = (piece != V.PAWN ? { c: color, p: piece } : null);
99 moves.push(this.getBasicMove([x1, y1], [x2, y2], tr));
100 }
101 }
102
103 getEnpassantCaptures([x, y], shiftX) {
104 const Lep = this.epSquares.length;
105 const epSquare = this.epSquares[Lep - 1];
106 let moves = [];
107 if (!!epSquare) {
108 for (let epsq of epSquare) {
109 // TODO: some redundant checks
110 if (epsq.x == x + shiftX && Math.abs(epsq.y - y) == 1) {
111 let enpassantMove = this.getBasicMove([x, y], [epsq.x, epsq.y]);
112 // WARNING: the captured pawn may be diagonally behind us,
113 // if it's a 3-squares jump and we take on 1st passing square
114 const px = this.board[x][epsq.y] != V.EMPTY ? x : x - shiftX;
115 enpassantMove.vanish.push({
116 x: px,
117 y: epsq.y,
118 p: "p",
119 c: this.getColor(px, epsq.y)
120 });
121 moves.push(enpassantMove);
122 }
123 }
124 }
125 return moves;
126 }
127
128};