6c522dc6f8cddfd7a8def4aa8a4e5d78789f5add
1 import { ChessRules
, PiPo
, Move
} from "@/base_rules";
2 import { ArrayFun
} from "@/utils/array";
3 import { sample
, shuffle
} from "@/utils/alea";
5 export class ShogiRules
extends ChessRules
{
6 static get HasFlags() {
10 static get HasEnpassant() {
14 static get Monochrome() {
18 static get Notoodark() {
22 static IsGoodFen(fen
) {
23 if (!ChessRules
.IsGoodFen(fen
)) return false;
24 const fenParsed
= V
.ParseFen(fen
);
26 if (!fenParsed
.reserve
|| !fenParsed
.reserve
.match(/^[0-9]{14,14}$/))
31 static ParseFen(fen
) {
32 const fenParts
= fen
.split(" ");
34 ChessRules
.ParseFen(fen
),
35 { reserve: fenParts
[3] }
39 // pawns, rooks, knights, bishops and king kept from ChessRules
43 static get SILVER_G() {
54 static get P_KNIGHT() {
57 static get P_SILVER() {
60 static get P_LANCE() {
66 static get P_BISHOP() {
89 getPpath(b
, color
, score
, orientation
) {
90 // 'i' for "inversed":
91 const suffix
= (b
[0] == orientation
? "" : "i");
92 return "Shogi/" + b
+ suffix
;
95 getPPpath(m
, orientation
) {
98 m
.appear
[0].c
+ m
.appear
[0].p
,
106 static GenRandInitFen(randomness
) {
107 if (randomness
== 0) {
109 "lnsgkgsnl/1r5b1/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL " +
113 // Randomization following these indications:
114 // http://www.shogi.net/shogi-l/Archive/2007/Nmar16-02.txt
115 let pieces1
= { w: new Array(4), b: new Array(4) };
116 let positions2
= { w: new Array(2), b: new Array(2) };
117 for (let c
of ["w", "b"]) {
118 if (c
== 'b' && randomness
== 1) {
119 pieces1
['b'] = JSON
.parse(JSON
.stringify(pieces1
['w'])).reverse();
121 JSON
.parse(JSON
.stringify(positions2
['w'])).reverse()
125 let positions
= shuffle(ArrayFun
.range(4));
126 const composition
= ['s', 's', 'g', 'g'];
127 for (let i
= 0; i
< 4; i
++) pieces1
[c
][positions
[i
]] = composition
[i
];
128 positions2
[c
] = sample(ArrayFun
.range(9), 2).sort();
133 pieces1
["b"].slice(0, 2).join("") +
135 pieces1
["b"].slice(2, 4).join("") +
139 (positions2
['b'][0] || "") + 'r' +
140 (positions2
['b'][1] - positions2
['b'][0] - 1 || "") + 'b' +
141 (8 - positions2
['b'][1] || "")
143 "/ppppppppp/9/9/9/PPPPPPPPP/" +
145 (positions2
['w'][0] || "") + 'B' +
146 (positions2
['w'][1] - positions2
['w'][0] - 1 || "") + 'R' +
147 (8 - positions2
['w'][1] || "")
151 pieces1
["w"].slice(0, 2).join("").toUpperCase() +
153 pieces1
["w"].slice(2, 4).join("").toUpperCase() +
156 " w 0 00000000000000"
161 return super.getFen() + " " + this.getReserveFen();
165 return super.getFenForRepeat() + "_" + this.getReserveFen();
169 let counts
= new Array(14);
170 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
171 counts
[i
] = this.reserve
["w"][V
.RESERVE_PIECES
[i
]];
172 counts
[7 + i
] = this.reserve
["b"][V
.RESERVE_PIECES
[i
]];
174 return counts
.join("");
177 setOtherVariables(fen
) {
178 super.setOtherVariables(fen
);
179 // Also init reserves (used by the interface to show landable pieces)
181 V
.ParseFen(fen
).reserve
.split("").map(x
=> parseInt(x
, 10));
184 [V
.PAWN
]: reserve
[0],
185 [V
.ROOK
]: reserve
[1],
186 [V
.BISHOP
]: reserve
[2],
187 [V
.GOLD_G
]: reserve
[3],
188 [V
.SILVER_G
]: reserve
[4],
189 [V
.KNIGHT
]: reserve
[5],
190 [V
.LANCE
]: reserve
[6]
193 [V
.PAWN
]: reserve
[7],
194 [V
.ROOK
]: reserve
[8],
195 [V
.BISHOP
]: reserve
[9],
196 [V
.GOLD_G
]: reserve
[10],
197 [V
.SILVER_G
]: reserve
[11],
198 [V
.KNIGHT
]: reserve
[12],
199 [V
.LANCE
]: reserve
[13]
205 if (i
>= V
.size
.x
) return i
== V
.size
.x
? "w" : "b";
206 return this.board
[i
][j
].charAt(0);
210 if (i
>= V
.size
.x
) return V
.RESERVE_PIECES
[j
];
211 return this.board
[i
][j
].charAt(1);
215 return { x: 9, y: 9};
218 getReservePpath(index
, color
, orientation
) {
220 "Shogi/" + color
+ V
.RESERVE_PIECES
[index
] +
221 (color
!= orientation
? 'i' : '')
225 // Ordering on reserve pieces
226 static get RESERVE_PIECES() {
228 [V
.PAWN
, V
.ROOK
, V
.BISHOP
, V
.GOLD_G
, V
.SILVER_G
, V
.KNIGHT
, V
.LANCE
]
232 getReserveMoves([x
, y
]) {
233 const color
= this.turn
;
234 const p
= V
.RESERVE_PIECES
[y
];
236 var oppCol
= V
.GetOppCol(color
);
238 [...Array(9).keys()].filter(j
=>
239 [...Array(9).keys()].every(i
=> {
241 this.board
[i
][j
] == V
.EMPTY
||
242 this.getColor(i
, j
) != color
||
243 this.getPiece(i
, j
) != V
.PAWN
248 if (this.reserve
[color
][p
] == 0) return [];
250 const forward
= color
== 'w' ? -1 : 1;
251 const lastRanks
= color
== 'w' ? [0, 1] : [8, 7];
252 for (let i
= 0; i
< V
.size
.x
; i
++) {
254 (i
== lastRanks
[0] && [V
.PAWN
, V
.KNIGHT
, V
.LANCE
].includes(p
)) ||
255 (i
== lastRanks
[1] && p
== V
.KNIGHT
)
259 for (let j
= 0; j
< V
.size
.y
; j
++) {
261 this.board
[i
][j
] == V
.EMPTY
&&
262 (p
!= V
.PAWN
|| allowedFiles
.includes(j
))
274 start: { x: x
, y: y
}, //a bit artificial...
278 // Do not drop on checkmate:
280 const res
= (this.underCheck(oppCol
) && !this.atLeastOneMove());
291 getPotentialMovesFrom([x
, y
]) {
293 // Reserves, outside of board: x == sizeX(+1)
294 return this.getReserveMoves([x
, y
]);
296 switch (this.getPiece(x
, y
)) {
298 return this.getPotentialPawnMoves([x
, y
]);
300 return this.getPotentialRookMoves([x
, y
]);
302 return this.getPotentialKnightMoves([x
, y
]);
304 return this.getPotentialBishopMoves([x
, y
]);
306 return this.getPotentialSilverMoves([x
, y
]);
308 return this.getPotentialLanceMoves([x
, y
]);
310 return this.getPotentialKingMoves([x
, y
]);
312 return this.getPotentialDragonMoves([x
, y
]);
314 return this.getPotentialHorseMoves([x
, y
]);
320 return this.getPotentialGoldMoves([x
, y
]);
322 return []; //never reached
325 // Modified to take promotions into account
326 getSlideNJumpMoves([x
, y
], steps
, options
) {
327 options
= options
|| {};
328 const color
= this.turn
;
329 const oneStep
= options
.oneStep
;
330 const forcePromoteOnLastRank
= options
.force
;
331 const promoteInto
= options
.promote
;
332 const lastRanks
= (color
== 'w' ? [0, 1, 2] : [9, 8, 7]);
334 outerLoop: for (let step
of steps
) {
337 while (V
.OnBoard(i
, j
) && this.board
[i
][j
] == V
.EMPTY
) {
338 if (i
!= lastRanks
[0] || !forcePromoteOnLastRank
)
339 moves
.push(this.getBasicMove([x
, y
], [i
, j
]));
340 if (!!promoteInto
&& lastRanks
.includes(i
)) {
343 [x
, y
], [i
, j
], { c: color
, p: promoteInto
})
346 if (oneStep
) continue outerLoop
;
350 if (V
.OnBoard(i
, j
) && this.canTake([x
, y
], [i
, j
])) {
351 if (i
!= lastRanks
[0] || !forcePromoteOnLastRank
)
352 moves
.push(this.getBasicMove([x
, y
], [i
, j
]));
353 if (!!promoteInto
&& lastRanks
.includes(i
)) {
356 [x
, y
], [i
, j
], { c: color
, p: promoteInto
})
364 getPotentialGoldMoves(sq
) {
365 const forward
= (this.turn
== 'w' ? -1 : 1);
366 return this.getSlideNJumpMoves(
368 V
.steps
[V
.ROOK
].concat([ [forward
, 1], [forward
, -1] ]),
373 getPotentialPawnMoves(sq
) {
374 const forward
= (this.turn
== 'w' ? -1 : 1);
376 this.getSlideNJumpMoves(
388 getPotentialSilverMoves(sq
) {
389 const forward
= (this.turn
== 'w' ? -1 : 1);
390 return this.getSlideNJumpMoves(
392 V
.steps
[V
.BISHOP
].concat([ [forward
, 0] ]),
400 getPotentialKnightMoves(sq
) {
401 const forward
= (this.turn
== 'w' ? -2 : 2);
402 return this.getSlideNJumpMoves(
404 [ [forward
, 1], [forward
, -1] ],
413 getPotentialLanceMoves(sq
) {
414 const forward
= (this.turn
== 'w' ? -1 : 1);
415 return this.getSlideNJumpMoves(
425 getPotentialRookMoves(sq
) {
426 return this.getSlideNJumpMoves(
427 sq
, V
.steps
[V
.ROOK
], { promote: V
.P_ROOK
});
430 getPotentialBishopMoves(sq
) {
431 return this.getSlideNJumpMoves(
432 sq
, V
.steps
[V
.BISHOP
], { promote: V
.P_BISHOP
});
435 getPotentialDragonMoves(sq
) {
437 this.getSlideNJumpMoves(sq
, V
.steps
[V
.ROOK
]).concat(
438 this.getSlideNJumpMoves(sq
, V
.steps
[V
.BISHOP
], { oneStep: true }))
442 getPotentialHorseMoves(sq
) {
444 this.getSlideNJumpMoves(sq
, V
.steps
[V
.BISHOP
]).concat(
445 this.getSlideNJumpMoves(sq
, V
.steps
[V
.ROOK
], { oneStep: true }))
449 getPotentialKingMoves(sq
) {
450 return this.getSlideNJumpMoves(
452 V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]),
457 isAttacked(sq
, color
) {
459 this.isAttackedByPawn(sq
, color
) ||
460 this.isAttackedByRook(sq
, color
) ||
461 this.isAttackedByDragon(sq
, color
) ||
462 this.isAttackedByKnight(sq
, color
) ||
463 this.isAttackedByBishop(sq
, color
) ||
464 this.isAttackedByHorse(sq
, color
) ||
465 this.isAttackedByLance(sq
, color
) ||
466 this.isAttackedBySilver(sq
, color
) ||
467 this.isAttackedByGold(sq
, color
) ||
468 this.isAttackedByKing(sq
, color
)
472 isAttackedByGold([x
, y
], color
) {
473 const shift
= (color
== 'w' ? 1 : -1);
474 for (let step
of V
.steps
[V
.ROOK
].concat([[shift
, 1], [shift
, -1]])) {
475 const [i
, j
] = [x
+ step
[0], y
+ step
[1]];
478 this.board
[i
][j
] != V
.EMPTY
&&
479 this.getColor(i
, j
) == color
&&
480 [V
.GOLD_G
, V
.P_PAWN
, V
.P_SILVER
, V
.P_KNIGHT
, V
.P_LANCE
]
481 .includes(this.getPiece(i
, j
))
489 isAttackedBySilver([x
, y
], color
) {
490 const shift
= (color
== 'w' ? 1 : -1);
491 for (let step
of V
.steps
[V
.BISHOP
].concat([[shift
, 0]])) {
492 const [i
, j
] = [x
+ step
[0], y
+ step
[1]];
495 this.board
[i
][j
] != V
.EMPTY
&&
496 this.getColor(i
, j
) == color
&&
497 this.getPiece(i
, j
) == V
.SILVER_G
505 isAttackedByPawn([x
, y
], color
) {
506 const shift
= (color
== 'w' ? 1 : -1);
507 const [i
, j
] = [x
+ shift
, y
];
510 this.board
[i
][j
] != V
.EMPTY
&&
511 this.getColor(i
, j
) == color
&&
512 this.getPiece(i
, j
) == V
.PAWN
516 isAttackedByKnight(sq
, color
) {
517 const forward
= (color
== 'w' ? 2 : -2);
518 return this.isAttackedBySlideNJump(
519 sq
, color
, V
.KNIGHT
, [[forward
, 1], [forward
, -1]], "oneStep");
522 isAttackedByLance(sq
, color
) {
523 const forward
= (color
== 'w' ? 1 : -1);
524 return this.isAttackedBySlideNJump(sq
, color
, V
.LANCE
, [[forward
, 0]]);
527 isAttackedByDragon(sq
, color
) {
529 this.isAttackedBySlideNJump(sq
, color
, V
.P_ROOK
, V
.steps
[V
.ROOK
]) ||
530 this.isAttackedBySlideNJump(
531 sq
, color
, V
.P_ROOK
, V
.steps
[V
.BISHOP
], "oneStep")
535 isAttackedByHorse(sq
, color
) {
537 this.isAttackedBySlideNJump(sq
, color
, V
.P_BISHOP
, V
.steps
[V
.BISHOP
]) ||
538 this.isAttackedBySlideNJump(
539 sq
, color
, V
.P_BISHOP
, V
.steps
[V
.ROOK
], "oneStep")
544 let moves
= super.getAllPotentialMoves();
545 const color
= this.turn
;
546 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
547 moves
= moves
.concat(
548 this.getReserveMoves([V
.size
.x
+ (color
== "w" ? 0 : 1), i
])
551 return this.filterValid(moves
);
555 if (!super.atLeastOneMove()) {
556 // Search one reserve move
557 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
558 let moves
= this.filterValid(
559 this.getReserveMoves([V
.size
.x
+ (this.turn
== "w" ? 0 : 1), i
])
561 if (moves
.length
> 0) return true;
568 static get P_CORRESPONDANCES() {
579 static MayDecode(piece
) {
580 if (Object
.keys(V
.P_CORRESPONDANCES
).includes(piece
))
581 return V
.P_CORRESPONDANCES
[piece
];
586 super.postPlay(move);
587 const color
= move.appear
[0].c
;
588 if (move.vanish
.length
== 0)
589 // Drop unpromoted piece:
590 this.reserve
[color
][move.appear
[0].p
]--;
591 else if (move.vanish
.length
== 2)
592 // May capture a promoted piece:
593 this.reserve
[color
][V
.MayDecode(move.vanish
[1].p
)]++;
597 super.postUndo(move);
598 const color
= this.turn
;
599 if (move.vanish
.length
== 0)
600 this.reserve
[color
][move.appear
[0].p
]++;
601 else if (move.vanish
.length
== 2)
602 this.reserve
[color
][V
.MayDecode(move.vanish
[1].p
)]--;
605 static get SEARCH_DEPTH() {
609 static get VALUES() {
610 // TODO: very arbitrary and wrong
630 let evaluation
= super.evalPosition();
632 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
633 const p
= V
.RESERVE_PIECES
[i
];
634 evaluation
+= this.reserve
["w"][p
] * V
.VALUES
[p
];
635 evaluation
-= this.reserve
["b"][p
] * V
.VALUES
[p
];
641 const finalSquare
= V
.CoordsToSquare(move.end
);
642 if (move.vanish
.length
== 0) {
644 const piece
= move.appear
[0].p
.toUpperCase();
645 return (piece
!= 'P' ? piece : "") + "@" + finalSquare
;
647 const piece
= move.vanish
[0].p
.toUpperCase();
649 (piece
!= 'P' || move.vanish
.length
== 2 ? piece : "") +
650 (move.vanish
.length
== 2 ? "x" : "") +
653 move.appear
[0].p
!= move.vanish
[0].p
654 ? "=" + move.appear
[0].p
.toUpperCase()