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
], 10);
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 // Also init reserves (used by the interface to show landable pieces)
70 V
.ParseFen(fen
).reserve
.split("").map(x
=> parseInt(x
, 10));
75 [V
.KNIGHT
]: reserve
[2],
76 [V
.BISHOP
]: reserve
[3],
77 [V
.QUEEN
]: reserve
[4],
83 [V
.KNIGHT
]: reserve
[8],
84 [V
.BISHOP
]: reserve
[9],
85 [V
.QUEEN
]: reserve
[10],
92 if (i
>= V
.size
.x
) return i
== V
.size
.x
? "w" : "b";
93 return this.board
[i
][j
].charAt(0);
97 if (i
>= V
.size
.x
) return V
.RESERVE_PIECES
[j
];
98 return this.board
[i
][j
].charAt(1);
101 // Used by the interface:
102 getReservePpath(index
, color
) {
103 return color
+ V
.RESERVE_PIECES
[index
];
106 // Ordering on reserve pieces (matching V.PIECES order)
107 static get RESERVE_PIECES() {
108 return [V
.PAWN
, V
.ROOK
, V
.KNIGHT
, V
.BISHOP
, V
.QUEEN
, V
.KING
];
111 getReserveMoves([x
, y
]) {
112 const color
= this.turn
;
113 const oppCol
= V
.GetOppCol(color
);
114 const p
= V
.RESERVE_PIECES
[y
];
115 if (this.reserve
[color
][p
] == 0) return [];
119 // Pawns can land only on 4 first ranks:
120 ? (color
== 'w' ? [4, 8] : [0, 4])
122 for (let i
= boundary
[0]; i
< boundary
[1]; i
++) {
123 for (let j
= 0; j
< 8; j
++) {
124 if (this.board
[i
][j
] == V
.EMPTY
) {
135 start: { x: x
, y: y
}, //a bit artificial...
139 // Landing with check is forbidden:
140 if (!this.underCheck(oppCol
)) moves
.push(mv
);
148 getPotentialMovesFrom([x
, y
]) {
151 ? this.getReserveMoves([x
, y
])
152 : super.getPotentialMovesFrom([x
, y
]);
153 // Forbid captures if king not landed yet:
154 if (x
< 8 && moves
.length
> 0 && this.kingPos
[moves
[0].appear
[0].c
][0] < 0)
155 moves
= moves
.filter(m
=> m
.vanish
.length
== 1);
160 let moves
= super.getAllValidMoves();
161 const color
= this.turn
;
162 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++)
163 moves
= moves
.concat(
164 this.getReserveMoves([V
.size
.x
+ (color
== "w" ? 0 : 1), i
])
166 return this.filterValid(moves
);
169 isAttacked(sq
, color
) {
170 // While the king hasn't landed, nothing is attacked:
171 if (this.kingPos
[color
][0] < 0) return false;
172 return super.isAttacked(sq
, color
);
176 if (!super.atLeastOneMove()) {
177 // Search one reserve move
178 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
179 let moves
= this.filterValid(
180 this.getReserveMoves([V
.size
.x
+ (this.turn
== "w" ? 0 : 1), i
])
182 if (moves
.length
> 0) return true;
190 if (this.kingPos
[color
][0] < 0)
191 // A king outside the board isn't under check
193 return this.isAttacked(this.kingPos
[color
], V
.GetOppCol(color
));
198 if (move.vanish
.length
== 0) this.reserve
[this.turn
][move.appear
[0].p
]--;
202 if (move.vanish
.length
== 0) this.reserve
[this.turn
][move.appear
[0].p
]++;
203 // (Potentially) Reset king position
204 if (move.appear
[0].p
== V
.KING
) {
205 const c
= move.appear
[0].c
;
206 if (move.vanish
.length
== 0)
208 this.kingPos
[c
] = [-1, -1];
211 this.kingPos
[c
] = [move.start
.x
, move.start
.y
];
215 static get SEARCH_DEPTH() {
220 let evaluation
= super.evalPosition();
222 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
223 const p
= V
.RESERVE_PIECES
[i
];
224 evaluation
+= this.reserve
["w"][p
] * V
.VALUES
[p
];
225 evaluation
-= this.reserve
["b"][p
] * V
.VALUES
[p
];
231 if (move.vanish
.length
> 0) return super.getNotation(move);
234 move.appear
[0].p
!= V
.PAWN
? move.appear
[0].p
.toUpperCase() : "";
235 return piece
+ "@" + V
.CoordsToSquare(move.end
);