1 import { ChessRules
, PiPo
, Move
} from "@/base_rules";
2 import { ArrayFun
} from "@/utils/array";
4 export class ShogunRules
extends ChessRules
{
12 static get ARCHBISHOP() {
18 static get DUCHESS() {
25 .concat([V
.CAPTAIN
, V
.GENERAL
, V
.ARCHBISHOP
, V
.MORTAR
, V
.DUCHESS
])
33 getReservePpath(index
, color
) {
34 return "Shogun/" + color
+ V
.RESERVE_PIECES
[index
];
37 static IsGoodFen(fen
) {
38 if (!ChessRules
.IsGoodFen(fen
)) return false;
39 const fenParsed
= V
.ParseFen(fen
);
41 if (!fenParsed
.reserve
|| !fenParsed
.reserve
.match(/^[0-9]{10,10}$/))
46 static ParseFen(fen
) {
47 const fenParts
= fen
.split(" ");
49 ChessRules
.ParseFen(fen
),
50 { reserve: fenParts
[5] }
54 static GenRandInitFen(randomness
) {
55 return ChessRules
.GenRandInitFen(randomness
) + " 0000000000";
59 return super.getFen() + " " + this.getReserveFen();
63 return super.getFenForRepeat() + "_" + this.getReserveFen();
67 let counts
= new Array(10);
68 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
69 counts
[i
] = this.reserve
["w"][V
.RESERVE_PIECES
[i
]];
70 counts
[5 + i
] = this.reserve
["b"][V
.RESERVE_PIECES
[i
]];
72 return counts
.join("");
75 setOtherVariables(fen
) {
76 super.setOtherVariables(fen
);
77 // Also init reserves (used by the interface to show landable pieces)
79 V
.ParseFen(fen
).reserve
.split("").map(x
=> parseInt(x
, 10));
84 [V
.KNIGHT
]: reserve
[2],
85 [V
.BISHOP
]: reserve
[3],
86 [V
.DUCHESS
]: reserve
[4]
91 [V
.KNIGHT
]: reserve
[7],
92 [V
.BISHOP
]: reserve
[8],
93 [V
.DUCHESS
]: reserve
[9]
99 if (i
>= V
.size
.x
) return i
== V
.size
.x
? "w" : "b";
100 return this.board
[i
][j
].charAt(0);
104 if (i
>= V
.size
.x
) return V
.RESERVE_PIECES
[j
];
105 return this.board
[i
][j
].charAt(1);
108 // Ordering on reserve pieces
109 static get RESERVE_PIECES() {
110 return [V
.PAWN
, V
.ROOK
, V
.KNIGHT
, V
.BISHOP
, V
.DUCHESS
];
113 getReserveMoves([x
, y
]) {
114 const color
= this.turn
;
115 const iZone
= (color
== 'w' ? [3, 4, 5, 6, 7] : [0, 1, 2, 3, 4]);
116 const p
= V
.RESERVE_PIECES
[y
];
117 if (this.reserve
[color
][p
] == 0) return [];
119 for (let i
of iZone
) {
120 for (let j
= 0; j
< V
.size
.y
; j
++) {
121 if (this.board
[i
][j
] == V
.EMPTY
) {
132 start: { x: x
, y: y
}, //a bit artificial...
142 static get MapUnpromoted() {
152 getPotentialMovesFrom([x
, y
]) {
154 // Reserves, outside of board: x == sizeX(+1)
155 return this.getReserveMoves([x
, y
]);
157 const piece
= this.getPiece(x
, y
);
159 if (piece
== V
.KING
) return super.getPotentialKingMoves(sq
);
164 return this.getPotentialPawnMoves(sq
);
166 moves
= super.getPotentialRookMoves(sq
);
169 moves
= super.getPotentialKnightMoves(sq
);
172 moves
= super.getPotentialBishopMoves(sq
);
175 moves
= this.getPotentialDuchessMoves(sq
);
178 if ([V
.ROOK
, V
.KNIGHT
, V
.BISHOP
, V
.DUCHESS
].includes(piece
)) {
180 // Check that no promoted form is already on board:
181 const promotedForm
= V
.MapUnpromoted
[piece
];
186 cell
[0] == c
&& cell
[1] == promotedForm
)
191 const promotionZone
= (this.turn
== 'w' ? [0, 1, 2] : [5, 6, 7]);
194 promotionZone
.includes(m
.end
.x
) ||
195 promotionZone
.includes(m
.start
.x
)
197 let newMove
= JSON
.parse(JSON
.stringify(m
));
198 newMove
.appear
[0].p
= promotedForm
;
199 extraMoves
.push(newMove
);
202 return moves
.concat(extraMoves
);
206 case V
.CAPTAIN: return this.getPotentialCaptainMoves(sq
);
207 case V
.MORTAR: return this.getPotentialMortarMoves(sq
);
208 case V
.GENERAL: return this.getPotentialGeneralMoves(sq
);
209 case V
.ARCHBISHOP: return this.getPotentialArchbishopMoves(sq
);
210 case V
.QUEEN: return super.getPotentialQueenMoves(sq
);
212 return []; //never reached
215 getPotentialPawnMoves([x
, y
]) {
216 // NOTE: apply promotion freely, but not on en-passant
218 const oppCol
= V
.GetOppCol(c
);
219 const forward
= (c
== 'w' ? -1 : 1);
220 const initialRank
= (c
== 'w' ? 6 : 1);
223 let [i
, j
] = [x
+ forward
, y
];
224 if (this.board
[i
][j
] == V
.EMPTY
) {
225 moves
.push(this.getBasicMove([x
, y
], [i
, j
]));
226 if (x
== initialRank
&& this.board
[i
+ forward
][j
] == V
.EMPTY
)
227 moves
.push(this.getBasicMove([x
, y
], [i
+ forward
, j
]));
230 for (let shiftY
of [-1, 1]) {
231 [i
, j
] = [x
+ forward
, y
+ shiftY
];
234 this.board
[i
][j
] != V
.EMPTY
&&
235 this.getColor(i
, j
) == oppCol
237 moves
.push(this.getBasicMove([x
, y
], [i
, j
]));
241 const promotionZone
= (this.turn
== 'w' ? [1, 2] : [5, 6]);
242 const lastRank
= (c
== 'w' ? 0 : 7);
244 if (m
.end
.x
== lastRank
)
246 m
.appear
[0].p
= V
.CAPTAIN
;
247 else if (promotionZone
.includes(m
.end
.x
)) {
248 let newMove
= JSON
.parse(JSON
.stringify(m
));
249 newMove
.appear
[0].p
= V
.CAPTAIN
;
250 extraMoves
.push(newMove
);
254 moves
.concat(extraMoves
)
255 .concat(super.getEnpassantCaptures([x
, y
], forward
))
259 getPotentialDuchessMoves(sq
) {
260 return super.getSlideNJumpMoves(sq
, V
.steps
[V
.BISHOP
], "oneStep");
263 getPotentialCaptainMoves(sq
) {
264 const steps
= V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]);
265 return super.getSlideNJumpMoves(sq
, steps
, "oneStep");
268 getPotentialMortarMoves(sq
) {
270 super.getSlideNJumpMoves(sq
, V
.steps
[V
.ROOK
])
271 .concat(super.getSlideNJumpMoves(sq
, V
.steps
[V
.KNIGHT
], "oneStep"))
275 getPotentialGeneralMoves(sq
) {
277 V
.steps
[V
.BISHOP
].concat(V
.steps
[V
.ROOK
]).concat(V
.steps
[V
.KNIGHT
]);
278 return super.getSlideNJumpMoves(sq
, steps
, "oneStep");
281 getPotentialArchbishopMoves(sq
) {
283 super.getSlideNJumpMoves(sq
, V
.steps
[V
.BISHOP
])
284 .concat(super.getSlideNJumpMoves(sq
, V
.steps
[V
.KNIGHT
], "oneStep"))
288 isAttacked(sq
, color
) {
290 super.isAttacked(sq
, color
) ||
291 this.isAttackedByDuchess(sq
, color
) ||
292 this.isAttackedByCaptain(sq
, color
) ||
293 this.isAttackedByMortar(sq
, color
) ||
294 this.isAttackedByGeneral(sq
, color
) ||
295 this.isAttackedByArchbishop(sq
, color
)
299 isAttackedByDuchess(sq
, color
) {
301 super.isAttackedBySlideNJump(
302 sq
, color
, V
.DUCHESS
, V
.steps
[V
.BISHOP
], "oneStep")
306 isAttackedByCaptain(sq
, color
) {
307 const steps
= V
.steps
[V
.BISHOP
].concat(V
.steps
[V
.ROOK
]);
309 super.isAttackedBySlideNJump(sq
, color
, V
.DUCHESS
, steps
, "oneStep")
313 isAttackedByMortar(sq
, color
) {
315 super.isAttackedBySlideNJump(sq
, color
, V
.MORTAR
, V
.steps
[V
.ROOK
]) ||
316 super.isAttackedBySlideNJump(
317 sq
, color
, V
.MORTAR
, V
.steps
[V
.KNIGHT
], "oneStep")
321 isAttackedByGeneral(sq
, color
) {
323 V
.steps
[V
.BISHOP
].concat(V
.steps
[V
.ROOK
]).concat(V
.steps
[V
.KNIGHT
]);
325 super.isAttackedBySlideNJump(sq
, color
, V
.GENERAL
, steps
, "oneStep")
329 isAttackedByArchbishop(sq
, color
) {
331 super.isAttackedBySlideNJump(sq
, color
, V
.ARCHBISHOP
, V
.steps
[V
.BISHOP
])
333 super.isAttackedBySlideNJump(
334 sq
, color
, V
.ARCHBISHOP
, V
.steps
[V
.KNIGHT
], "oneStep")
339 let moves
= super.getAllPotentialMoves();
340 const color
= this.turn
;
341 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
342 moves
= moves
.concat(
343 this.getReserveMoves([V
.size
.x
+ (color
== "w" ? 0 : 1), i
])
346 return this.filterValid(moves
);
350 if (!super.atLeastOneMove()) {
351 // Search one reserve move
352 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
353 let moves
= this.filterValid(
354 this.getReserveMoves([V
.size
.x
+ (this.turn
== "w" ? 0 : 1), i
])
356 if (moves
.length
> 0) return true;
363 static get MapPromoted() {
373 getUnpromotedForm(piece
) {
374 if (Object
.keys(V
.MapPromoted
).includes(piece
))
375 return V
.MapPromoted
[piece
];
380 super.postPlay(move);
382 if (move.vanish
.length
== 2 && move.appear
.length
== 2) return;
383 const color
= move.appear
[0].c
;
384 if (move.vanish
.length
== 0) this.reserve
[color
][move.appear
[0].p
]--;
385 else if (move.vanish
.length
== 2)
386 this.reserve
[color
][this.getUnpromotedForm(move.vanish
[1].p
)]++;
390 super.postUndo(move);
391 if (move.vanish
.length
== 2 && move.appear
.length
== 2) return;
392 const color
= this.turn
;
393 if (move.vanish
.length
== 0) this.reserve
[color
][move.appear
[0].p
]++;
394 else if (move.vanish
.length
== 2)
395 this.reserve
[color
][this.getUnpromotedForm(move.vanish
[1].p
)]--;
398 static get SEARCH_DEPTH() {
402 static get VALUES() {
418 let evaluation
= super.evalPosition();
420 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
421 const p
= V
.RESERVE_PIECES
[i
];
422 evaluation
+= this.reserve
["w"][p
] * V
.VALUES
[p
];
423 evaluation
-= this.reserve
["b"][p
] * V
.VALUES
[p
];
429 if (move.vanish
.length
> 0) return super.getNotation(move);
432 move.appear
[0].p
!= V
.PAWN
? move.appear
[0].p
.toUpperCase() : "";
433 return piece
+ "@" + V
.CoordsToSquare(move.end
);