1 import { ChessRules
} from "@/base_rules";
3 export class KingsmakerRules
extends ChessRules
{
5 static IsGoodPosition(position
) {
6 if (position
.length
== 0) return false;
7 const rows
= position
.split("/");
8 if (rows
.length
!= V
.size
.x
) return false;
9 let kings
= { "k": 0, "K": 0 };
10 for (let row
of rows
) {
12 for (let i
= 0; i
< row
.length
; i
++) {
13 if (['K','k'].includes(row
[i
])) kings
[row
[i
]]++;
14 if (V
.PIECES
.includes(row
[i
].toLowerCase())) sumElts
++;
16 const num
= parseInt(row
[i
], 10);
17 if (isNaN(num
) || num
<= 0) return false;
21 if (sumElts
!= V
.size
.y
) return false;
23 // At least one king per color.
24 if (Object
.values(kings
).some(v
=> v
== 0)) return false;
30 getPotentialMovesFrom([x
, y
]) {
31 const moves
= super.getPotentialMovesFrom([x
, y
]);
32 if (this.getPiece(x
, y
) != V
.PAWN
) return moves
;
33 const c
= this.getColor(x
, y
);
34 const oppCol
= V
.GetOppCol(c
);
35 const forward
= (c
== 'w' ? -1 : 1);
36 const lastRanks
= (c
== 'w' ? [0, 1] : [7, 6]);
37 let newKingMoves
= [];
38 if (lastRanks
.includes(x
+ forward
)) {
39 // Manually add promotion into enemy king:
41 { step: [forward
, 0] },
42 { step: [forward
, 1], capture: true },
43 { step: [forward
, -1], capture: true }
45 for (let s
of trials
) {
46 const [i
, j
] = [x
+ s
.step
[0], y
+ s
.step
[1]];
50 (!s
.capture
&& this.board
[i
][j
] == V
.EMPTY
) ||
53 this.board
[i
][j
] != V
.EMPTY
&&
54 this.getColor(i
, j
) == oppCol
59 super.getBasicMove([x
, y
], [i
, j
], { c: oppCol
, p: V
.KING
})
64 return moves
.concat(newKingMoves
);
68 // First at first check found (if any)
69 const oppCol
= V
.GetOppCol(color
);
70 for (let i
=0; i
<8; i
++) {
71 for (let j
=0; j
<8; j
++) {
73 this.board
[i
][j
] != V
.EMPTY
&&
74 this.getPiece(i
, j
) == V
.KING
&&
75 this.getColor(i
, j
) == color
77 if (super.isAttacked([i
, j
], oppCol
)) return true;
85 const color
= this.turn
;
86 const oppCol
= V
.GetOppCol(color
);
89 for (let i
=0; i
<8; i
++) {
90 for (let j
=0; j
<8; j
++) {
92 this.board
[i
][j
] != V
.EMPTY
&&
93 this.getPiece(i
, j
) == V
.KING
&&
94 this.getColor(i
, j
) == color
96 if (super.isAttacked([i
, j
], oppCol
)) res
.push([i
, j
]);
104 this.updateCastleFlags(move, move.vanish
[0].p
);
109 static get VALUES() {
110 // Assign -5 to the king, so that the bot sometimes promote into king
111 return Object
.assign({}, ChessRules
.VALUES
, { k: -5 });