1 import { ChessRules
, PiPo
, Move
} from "@/base_rules";
3 export class ParachuteRules
extends ChessRules
{
5 static get HasFlags() {
9 static IsGoodFen(fen
) {
10 if (!ChessRules
.IsGoodFen(fen
)) return false;
11 const fenParsed
= V
.ParseFen(fen
);
13 if (!fenParsed
.reserve
|| !fenParsed
.reserve
.match(/^[0-9]{12,12}$/))
18 static IsGoodPosition(position
) {
19 if (position
.length
== 0) return false;
20 const rows
= position
.split("/");
21 if (rows
.length
!= V
.size
.x
) return false;
22 for (let row
of rows
) {
24 for (let i
= 0; i
< row
.length
; i
++) {
25 if (V
.PIECES
.includes(row
[i
].toLowerCase())) sumElts
++;
27 const num
= parseInt(row
[i
], 10);
28 if (isNaN(num
)) return false;
32 if (sumElts
!= V
.size
.y
) return false;
37 static ParseFen(fen
) {
38 const fenParts
= fen
.split(" ");
40 ChessRules
.ParseFen(fen
),
41 { reserve: fenParts
[4] }
45 static GenRandInitFen() {
46 // ChessRules.PIECES order is P, R, N, B, Q, K:
47 return "8/8/8/8/8/8/8/8 w 0 - 822211822211";
51 return super.getFen() + " " + this.getReserveFen();
55 return super.getFenForRepeat() + "_" + this.getReserveFen();
59 let counts
= new Array(12);
60 for (let i
= 0; i
< V
.PIECES
.length
; i
++) {
61 counts
[i
] = this.reserve
["w"][V
.PIECES
[i
]];
62 counts
[6 + i
] = this.reserve
["b"][V
.PIECES
[i
]];
64 return counts
.join("");
67 setOtherVariables(fen
) {
68 super.setOtherVariables(fen
);
69 // Also init reserves (used by the interface to show landable pieces)
71 V
.ParseFen(fen
).reserve
.split("").map(x
=> parseInt(x
, 10));
76 [V
.KNIGHT
]: reserve
[2],
77 [V
.BISHOP
]: reserve
[3],
78 [V
.QUEEN
]: reserve
[4],
84 [V
.KNIGHT
]: reserve
[8],
85 [V
.BISHOP
]: reserve
[9],
86 [V
.QUEEN
]: reserve
[10],
93 if (i
>= V
.size
.x
) return i
== V
.size
.x
? "w" : "b";
94 return this.board
[i
][j
].charAt(0);
98 if (i
>= V
.size
.x
) return V
.RESERVE_PIECES
[j
];
99 return this.board
[i
][j
].charAt(1);
102 // Used by the interface:
103 getReservePpath(index
, color
) {
104 return color
+ V
.RESERVE_PIECES
[index
];
107 // Ordering on reserve pieces (matching V.PIECES order)
108 static get RESERVE_PIECES() {
109 return [V
.PAWN
, V
.ROOK
, V
.KNIGHT
, V
.BISHOP
, V
.QUEEN
, V
.KING
];
112 getReserveMoves([x
, y
]) {
113 const color
= this.turn
;
114 const oppCol
= V
.GetOppCol(color
);
115 const p
= V
.RESERVE_PIECES
[y
];
116 if (this.reserve
[color
][p
] == 0) return [];
120 // Pawns can land only on 4 first ranks:
121 ? (color
== 'w' ? [4, 8] : [0, 4])
123 for (let i
= boundary
[0]; i
< boundary
[1]; i
++) {
124 for (let j
= 0; j
< 8; j
++) {
125 if (this.board
[i
][j
] == V
.EMPTY
) {
136 start: { x: x
, y: y
}, //a bit artificial...
140 // Landing with check is forbidden:
141 if (!this.underCheck(oppCol
)) moves
.push(mv
);
149 getPotentialMovesFrom([x
, y
]) {
152 ? this.getReserveMoves([x
, y
])
153 : super.getPotentialMovesFrom([x
, y
]);
154 // Forbid captures if king not landed yet:
155 if (x
< 8 && moves
.length
> 0 && this.kingPos
[moves
[0].appear
[0].c
][0] < 0)
156 moves
= moves
.filter(m
=> m
.vanish
.length
== 1);
161 let moves
= super.getAllValidMoves();
162 const color
= this.turn
;
163 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++)
164 moves
= moves
.concat(
165 this.getReserveMoves([V
.size
.x
+ (color
== "w" ? 0 : 1), i
])
167 return this.filterValid(moves
);
170 isAttacked(sq
, color
) {
171 // While the king hasn't landed, nothing is attacked:
172 if (this.kingPos
[color
][0] < 0) return false;
173 return super.isAttacked(sq
, color
);
177 if (!super.atLeastOneMove()) {
178 // Search one reserve move
179 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
180 let moves
= this.filterValid(
181 this.getReserveMoves([V
.size
.x
+ (this.turn
== "w" ? 0 : 1), i
])
183 if (moves
.length
> 0) return true;
191 if (this.kingPos
[color
][0] < 0)
192 // A king outside the board isn't under check
194 return this.isAttacked(this.kingPos
[color
], V
.GetOppCol(color
));
199 if (move.vanish
.length
== 0) this.reserve
[this.turn
][move.appear
[0].p
]--;
203 if (move.vanish
.length
== 0) this.reserve
[this.turn
][move.appear
[0].p
]++;
204 // (Potentially) Reset king position
205 if (move.appear
[0].p
== V
.KING
) {
206 const c
= move.appear
[0].c
;
207 if (move.vanish
.length
== 0)
209 this.kingPos
[c
] = [-1, -1];
212 this.kingPos
[c
] = [move.start
.x
, move.start
.y
];
216 static get SEARCH_DEPTH() {
221 let evaluation
= super.evalPosition();
223 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
224 const p
= V
.RESERVE_PIECES
[i
];
225 evaluation
+= this.reserve
["w"][p
] * V
.VALUES
[p
];
226 evaluation
-= this.reserve
["b"][p
] * V
.VALUES
[p
];
232 if (move.vanish
.length
> 0) return super.getNotation(move);
235 move.appear
[0].p
!= V
.PAWN
? move.appear
[0].p
.toUpperCase() : "";
236 return piece
+ "@" + V
.CoordsToSquare(move.end
);