1 import { ChessRules
, PiPo
, Move
} from "@/base_rules";
3 export class ParachuteRules
extends ChessRules
{
6 // TODO: later, allow variant, placing piece giving check maybe...
10 static get HasFlags() {
14 static IsGoodFen(fen
) {
15 if (!ChessRules
.IsGoodFen(fen
)) return false;
16 const fenParsed
= V
.ParseFen(fen
);
18 if (!fenParsed
.reserve
|| !fenParsed
.reserve
.match(/^[0-9]{12,12}$/))
23 static IsGoodPosition(position
) {
24 if (position
.length
== 0) return false;
25 const rows
= position
.split("/");
26 if (rows
.length
!= V
.size
.x
) return false;
27 for (let row
of rows
) {
29 for (let i
= 0; i
< row
.length
; i
++) {
30 if (V
.PIECES
.includes(row
[i
].toLowerCase())) sumElts
++;
32 const num
= parseInt(row
[i
], 10);
33 if (isNaN(num
)) return false;
37 if (sumElts
!= V
.size
.y
) return false;
42 static ParseFen(fen
) {
43 const fenParts
= fen
.split(" ");
45 ChessRules
.ParseFen(fen
),
46 { reserve: fenParts
[4] }
50 static GenRandInitFen() {
51 // ChessRules.PIECES order is P, R, N, B, Q, K:
52 return "8/8/8/8/8/8/8/8 w 0 - 822211822211";
56 return super.getFen() + " " + this.getReserveFen();
60 return super.getFenForRepeat() + "_" + this.getReserveFen();
64 let counts
= new Array(12);
65 for (let i
= 0; i
< V
.PIECES
.length
; i
++) {
66 counts
[i
] = this.reserve
["w"][V
.PIECES
[i
]];
67 counts
[6 + i
] = this.reserve
["b"][V
.PIECES
[i
]];
69 return counts
.join("");
72 setOtherVariables(fen
) {
73 super.setOtherVariables(fen
);
74 // Also init reserves (used by the interface to show landable pieces)
76 V
.ParseFen(fen
).reserve
.split("").map(x
=> parseInt(x
, 10));
81 [V
.KNIGHT
]: reserve
[2],
82 [V
.BISHOP
]: reserve
[3],
83 [V
.QUEEN
]: reserve
[4],
89 [V
.KNIGHT
]: reserve
[8],
90 [V
.BISHOP
]: reserve
[9],
91 [V
.QUEEN
]: reserve
[10],
98 if (i
>= V
.size
.x
) return i
== V
.size
.x
? "w" : "b";
99 return this.board
[i
][j
].charAt(0);
103 if (i
>= V
.size
.x
) return V
.RESERVE_PIECES
[j
];
104 return this.board
[i
][j
].charAt(1);
107 // Used by the interface:
108 getReservePpath(index
, color
) {
109 return color
+ V
.RESERVE_PIECES
[index
];
112 // Ordering on reserve pieces (matching V.PIECES order)
113 static get RESERVE_PIECES() {
114 return [V
.PAWN
, V
.ROOK
, V
.KNIGHT
, V
.BISHOP
, V
.QUEEN
, V
.KING
];
117 getReserveMoves([x
, y
]) {
118 const color
= this.turn
;
119 const oppCol
= V
.GetOppCol(color
);
120 const p
= V
.RESERVE_PIECES
[y
];
121 if (this.reserve
[color
][p
] == 0) return [];
125 // Pawns can land only on 4 first ranks:
126 ? (color
== 'w' ? [4, 8] : [0, 4])
128 for (let i
= boundary
[0]; i
< boundary
[1]; i
++) {
129 for (let j
= 0; j
< 8; j
++) {
130 if (this.board
[i
][j
] == V
.EMPTY
) {
141 start: { x: x
, y: y
}, //a bit artificial...
145 // Landing with check is forbidden:
146 if (!this.underCheck(oppCol
)) moves
.push(mv
);
154 getPotentialMovesFrom([x
, y
]) {
157 ? this.getReserveMoves([x
, y
])
158 : super.getPotentialMovesFrom([x
, y
]);
159 // Forbid captures if king not landed yet:
160 if (x
< 8 && moves
.length
> 0 && this.kingPos
[moves
[0].appear
[0].c
][0] < 0)
161 moves
= moves
.filter(m
=> m
.vanish
.length
== 1);
166 let moves
= super.getAllValidMoves();
167 const color
= this.turn
;
168 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++)
169 moves
= moves
.concat(
170 this.getReserveMoves([V
.size
.x
+ (color
== "w" ? 0 : 1), i
])
172 return this.filterValid(moves
);
175 isAttacked(sq
, color
) {
176 // While the king hasn't landed, nothing is attacked:
177 if (this.kingPos
[color
][0] < 0) return false;
178 return super.isAttacked(sq
, color
);
182 if (!super.atLeastOneMove()) {
183 // Search one reserve move
184 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
185 let moves
= this.filterValid(
186 this.getReserveMoves([V
.size
.x
+ (this.turn
== "w" ? 0 : 1), i
])
188 if (moves
.length
> 0) return true;
196 if (this.kingPos
[color
][0] < 0)
197 // A king outside the board isn't under check
199 return this.isAttacked(this.kingPos
[color
], V
.GetOppCol(color
));
204 if (move.vanish
.length
== 0) this.reserve
[this.turn
][move.appear
[0].p
]--;
208 if (move.vanish
.length
== 0) this.reserve
[this.turn
][move.appear
[0].p
]++;
209 // (Potentially) Reset king position
210 if (move.appear
[0].p
== V
.KING
) {
211 const c
= move.appear
[0].c
;
212 if (move.vanish
.length
== 0)
214 this.kingPos
[c
] = [-1, -1];
217 this.kingPos
[c
] = [move.start
.x
, move.start
.y
];
221 static get SEARCH_DEPTH() {
226 let evaluation
= super.evalPosition();
228 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
229 const p
= V
.RESERVE_PIECES
[i
];
230 evaluation
+= this.reserve
["w"][p
] * V
.VALUES
[p
];
231 evaluation
-= this.reserve
["b"][p
] * V
.VALUES
[p
];
237 if (move.vanish
.length
> 0) return super.getNotation(move);
240 move.appear
[0].p
!= V
.PAWN
? move.appear
[0].p
.toUpperCase() : "";
241 return piece
+ "@" + V
.CoordsToSquare(move.end
);