1 import { ChessRules
, PiPo
, Move
} from "@/base_rules";
3 export class ParachuteRules
extends ChessRules
{
4 static get HasFlags() {
8 static IsGoodFen(fen
) {
9 if (!ChessRules
.IsGoodFen(fen
)) return false;
10 const fenParsed
= V
.ParseFen(fen
);
12 if (!fenParsed
.reserve
|| !fenParsed
.reserve
.match(/^[0-9]{12,12}$/))
17 static IsGoodPosition(position
) {
18 if (position
.length
== 0) return false;
19 const rows
= position
.split("/");
20 if (rows
.length
!= V
.size
.x
) return false;
21 for (let row
of rows
) {
23 for (let i
= 0; i
< row
.length
; i
++) {
24 if (V
.PIECES
.includes(row
[i
].toLowerCase())) sumElts
++;
26 const num
= parseInt(row
[i
]);
27 if (isNaN(num
)) return false;
31 if (sumElts
!= V
.size
.y
) return false;
36 static ParseFen(fen
) {
37 const fenParts
= fen
.split(" ");
39 ChessRules
.ParseFen(fen
),
40 { reserve: fenParts
[4] }
44 static GenRandInitFen() {
45 // ChessRules.PIECES order is P, R, N, B, Q, K:
46 return "8/8/8/8/8/8/8/8 w 0 - 822211822211";
50 return super.getFen() + " " + this.getReserveFen();
54 return super.getFenForRepeat() + "_" + this.getReserveFen();
58 let counts
= new Array(12);
59 for (let i
= 0; i
< V
.PIECES
.length
; i
++) {
60 counts
[i
] = this.reserve
["w"][V
.PIECES
[i
]];
61 counts
[6 + i
] = this.reserve
["b"][V
.PIECES
[i
]];
63 return counts
.join("");
66 setOtherVariables(fen
) {
67 super.setOtherVariables(fen
);
68 const fenParsed
= V
.ParseFen(fen
);
69 // Also init reserves (used by the interface to show landable pieces)
72 [V
.PAWN
]: parseInt(fenParsed
.reserve
[0]),
73 [V
.ROOK
]: parseInt(fenParsed
.reserve
[1]),
74 [V
.KNIGHT
]: parseInt(fenParsed
.reserve
[2]),
75 [V
.BISHOP
]: parseInt(fenParsed
.reserve
[3]),
76 [V
.QUEEN
]: parseInt(fenParsed
.reserve
[4]),
77 [V
.KING
]: parseInt(fenParsed
.reserve
[5])
80 [V
.PAWN
]: parseInt(fenParsed
.reserve
[6]),
81 [V
.ROOK
]: parseInt(fenParsed
.reserve
[7]),
82 [V
.KNIGHT
]: parseInt(fenParsed
.reserve
[8]),
83 [V
.BISHOP
]: parseInt(fenParsed
.reserve
[9]),
84 [V
.QUEEN
]: parseInt(fenParsed
.reserve
[10]),
85 [V
.KING
]: parseInt(fenParsed
.reserve
[11])
91 if (i
>= V
.size
.x
) return i
== V
.size
.x
? "w" : "b";
92 return this.board
[i
][j
].charAt(0);
96 if (i
>= V
.size
.x
) return V
.RESERVE_PIECES
[j
];
97 return this.board
[i
][j
].charAt(1);
100 // Used by the interface:
101 getReservePpath(index
, color
) {
102 return color
+ V
.RESERVE_PIECES
[index
];
105 // Ordering on reserve pieces (matching V.PIECES order)
106 static get RESERVE_PIECES() {
107 return [V
.PAWN
, V
.ROOK
, V
.KNIGHT
, V
.BISHOP
, V
.QUEEN
, V
.KING
];
110 getReserveMoves([x
, y
]) {
111 const color
= this.turn
;
112 const oppCol
= V
.GetOppCol(color
);
113 const p
= V
.RESERVE_PIECES
[y
];
114 if (this.reserve
[color
][p
] == 0) return [];
118 // Pawns can land only on 4 first ranks:
119 ? (color
== 'w' ? [4, 8] : [0, 4])
121 for (let i
= boundary
[0]; i
< boundary
[1]; i
++) {
122 for (let j
= 0; j
< 8; j
++) {
123 if (this.board
[i
][j
] == V
.EMPTY
) {
134 start: { x: x
, y: y
}, //a bit artificial...
138 // Landing with check is forbidden:
139 if (!this.underCheck(oppCol
)) moves
.push(mv
);
147 getPotentialMovesFrom([x
, y
]) {
150 ? this.getReserveMoves([x
, y
])
151 : super.getPotentialMovesFrom([x
, y
]);
152 // Forbid captures if king not landed yet:
153 if (x
< 8 && moves
.length
> 0 && this.kingPos
[moves
[0].appear
[0].c
][0] < 0)
154 moves
= moves
.filter(m
=> m
.vanish
.length
== 1);
159 let moves
= super.getAllValidMoves();
160 const color
= this.turn
;
161 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++)
162 moves
= moves
.concat(
163 this.getReserveMoves([V
.size
.x
+ (color
== "w" ? 0 : 1), i
])
165 return this.filterValid(moves
);
168 isAttacked(sq
, color
) {
169 // While the king hasn't landed, nothing is attacked:
170 if (this.kingPos
[color
][0] < 0) return false;
171 return super.isAttacked(sq
, color
);
175 if (!super.atLeastOneMove()) {
176 // Search one reserve move
177 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
178 let moves
= this.filterValid(
179 this.getReserveMoves([V
.size
.x
+ (this.turn
== "w" ? 0 : 1), i
])
181 if (moves
.length
> 0) return true;
189 if (this.kingPos
[color
][0] < 0)
190 // A king outside the board isn't under check
192 return this.isAttacked(this.kingPos
[color
], V
.GetOppCol(color
));
197 if (move.vanish
.length
== 0) this.reserve
[this.turn
][move.appear
[0].p
]--;
201 if (move.vanish
.length
== 0) this.reserve
[this.turn
][move.appear
[0].p
]++;
202 // (Potentially) Reset king position
203 if (move.appear
[0].p
== V
.KING
) {
204 const c
= move.appear
[0].c
;
205 if (move.vanish
.length
== 0)
207 this.kingPos
[c
] = [-1, -1];
210 this.kingPos
[c
] = [move.start
.x
, move.start
.y
];
214 static get SEARCH_DEPTH() {
219 let evaluation
= super.evalPosition();
221 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
222 const p
= V
.RESERVE_PIECES
[i
];
223 evaluation
+= this.reserve
["w"][p
] * V
.VALUES
[p
];
224 evaluation
-= this.reserve
["b"][p
] * V
.VALUES
[p
];
230 if (move.vanish
.length
> 0) return super.getNotation(move);
233 move.appear
[0].p
!= V
.PAWN
? move.appear
[0].p
.toUpperCase() : "";
234 return piece
+ "@" + V
.CoordsToSquare(move.end
);