1 import { ChessRules
} from "@/base_rules";
3 export class MaharajahRules
extends ChessRules
{
5 static get HasEnpassant() {
9 static get MAHARAJAH() {
14 return b
.charAt(0) == 'w' ? b : "Maharajah/bm";
18 return ChessRules
.PIECES
.concat([V
.MAHARAJAH
]);
21 static get M_EXTRA_STEPS() {
35 static IsGoodPosition(position
) {
36 if (position
.length
== 0) return false;
37 const rows
= position
.split("/");
38 if (rows
.length
!= V
.size
.x
) return false;
40 for (let row
of rows
) {
42 for (let i
= 0; i
< row
.length
; i
++) {
43 const lowR
= row
[i
].toLowerCase();
44 if (!!lowR
.match(/[a-z]/)) {
45 if (row
[i
] == lowR
&& row
[i
] != 'm') return false;
46 if (row
[i
] == 'K') wKingCount
++;
47 if (V
.PIECES
.includes(lowR
)) sumElts
++;
50 const num
= parseInt(row
[i
], 10);
51 if (isNaN(num
) || num
<= 0) return false;
55 if (sumElts
!= V
.size
.y
) return false;
57 if (wKingCount
!= 1) return false;
61 static IsGoodFlags(flags
) {
62 // Only white can castle
63 return !!flags
.match(/^[a-z]{2,2}$/);
67 // Square of white king only:
68 this.kingPos
= { w: [-1, -1], b: [-1, -1] };
69 const fenRows
= V
.ParseFen(fen
).position
.split("/");
70 for (let i
= 0; i
< fenRows
.length
; i
++) {
71 let k
= 0; //column index on board
72 for (let j
= 0; j
< fenRows
[i
].length
; j
++) {
73 switch (fenRows
[i
].charAt(j
)) {
75 this.kingPos
["w"] = [i
, k
];
78 const num
= parseInt(fenRows
[i
].charAt(j
), 10);
79 if (!isNaN(num
)) k
+= num
- 1;
87 static GenRandInitFen(randomness
) {
88 const sFen
= ChessRules
.GenRandInitFen(Math
.max(randomness
, 1));
89 return "3mm3/8/" + sFen
.substring(18, 50);
93 return this.castleFlags
['w'].map(V
.CoordToColumn
).join("");
97 this.castleFlags
= { 'w': [-1, -1] };
98 for (let i
= 0; i
< 2; i
++)
99 this.castleFlags
['w'][i
] = V
.ColumnToCoord(fenflags
.charAt(i
));
102 getPotentialMovesFrom(sq
) {
103 if (this.turn
== 'w') return super.getPotentialMovesFrom(sq
);
104 return this.getPotentialMaharajahMoves(sq
);
107 getPotentialMaharajahMoves(sq
) {
108 let moves
= super.getPotentialQueenMoves(sq
);
109 moves
= moves
.concat(super.getPotentialKnightMoves(sq
));
110 const otherJumpMoves
=
111 super.getSlideNJumpMoves(sq
, V
.M_EXTRA_STEPS
, "oneStep")
113 moves
.every(mv
=> mv
.end
.x
!= m
.end
.x
|| mv
.end
.y
!= m
.end
.y
));
114 return moves
.concat(otherJumpMoves
);
127 updateCastleFlags(move, piece
) {
128 // Only white can castle:
130 if (piece
== V
.KING
&& move.appear
[0].c
== 'w')
131 this.castleFlags
['w'] = [8, 8];
133 move.start
.x
== firstRank
&&
134 this.castleFlags
['w'].includes(move.start
.y
)
136 const flagIdx
= (move.start
.y
== this.castleFlags
['w'][0] ? 0 : 1);
137 this.castleFlags
['w'][flagIdx
] = 8;
140 move.end
.x
== firstRank
&&
141 this.castleFlags
['w'].includes(move.end
.y
)
143 const flagIdx
= (move.end
.y
== this.castleFlags
['w'][0] ? 0 : 1);
144 this.castleFlags
['w'][flagIdx
] = 8;
149 if (this.turn
== 'b') super.postPlay(move);
151 // After a black move: white king may have disappeared
152 if (move.vanish
.length
== 2 && move.vanish
[1].p
== V
.KING
)
153 this.kingPos
['w'] = [-1, -1];
158 if (this.turn
== 'w') super.postUndo(move);
160 // After undoing a black move (may have captured king)
161 if (move.vanish
.length
== 2 && move.vanish
[1].p
== V
.KING
)
162 this.kingPos
['w'] = [move.end
.x
, move.end
.y
];
167 if (this.turn
== 'w' && this.kingPos
['w'][0] < 0) return "0-1";
170 this.board
.every(row
=> row
.every(cell
=> cell
.charAt(0) != 'b'))
177 static get VALUES() {
178 return Object
.assign({ m: 15 }, ChessRules
.VALUES
);
181 static get SEARCH_DEPTH() {