2eeeaeaf8be2fd356fbac89c6056b7a4bd351619
1 import { ChessRules
, Move
, PiPo
} from "@/base_rules";
2 import { ArrayFun
} from "@/utils/array";
3 import { randInt
, shuffle
} from "@/utils/alea";
5 export class MakrukRules
extends ChessRules
{
6 static get HasFlags() {
10 static get HasEnpassant() {
14 static get PawnSpecs() {
18 { promotions: [V
.QUEEN
] }
23 return ChessRules
.PIECES
.concat(V
.PROMOTED
);
26 static get PROMOTED() {
30 static GenRandInitFen(randomness
) {
32 return "rnbqkbnr/8/pppppppp/8/8/PPPPPPPP/8/RNBKQBNR w 0";
34 let pieces
= { w: new Array(8), b: new Array(8) };
35 for (let c
of ["w", "b"]) {
36 if (c
== 'b' && randomness
== 1) {
37 pieces
['b'] = pieces
['w'];
41 // Get random squares for every piece, totally freely (no castling)
42 let positions
= shuffle(ArrayFun
.range(8));
43 const composition
= ['b', 'b', 'r', 'r', 'n', 'n', 'k', 'q'];
44 for (let i
= 0; i
< 8; i
++) pieces
[c
][positions
[i
]] = composition
[i
];
47 pieces
["b"].join("") +
48 "/8/pppppppp/8/8/PPPPPPPP/8/" +
49 pieces
["w"].join("").toUpperCase() +
58 getPotentialMovesFrom([x
, y
]) {
59 if (this.getPiece(x
, y
) == V
.PROMOTED
)
60 return this.getPotentialQueenMoves([x
, y
]);
61 return super.getPotentialMovesFrom([x
, y
]);
64 getPotentialPawnMoves([x
, y
]) {
65 const color
= this.turn
;
66 const shiftX
= V
.PawnSpecs
.directions
[color
];
67 const sixthRank
= (color
== 'w' ? 2 : 5);
68 const tr
= (x
+ shiftX
== sixthRank
? { p: V
.PROMOTED
, c: color
} : null);
70 if (this.board
[x
+ shiftX
][y
] == V
.EMPTY
)
72 moves
.push(this.getBasicMove([x
, y
], [x
+ shiftX
, y
], tr
));
74 for (let shiftY
of [-1, 1]) {
76 y
+ shiftY
>= 0 && y
+ shiftY
< 8 &&
77 this.board
[x
+ shiftX
][y
+ shiftY
] != V
.EMPTY
&&
78 this.canTake([x
, y
], [x
+ shiftX
, y
+ shiftY
])
80 moves
.push(this.getBasicMove([x
, y
], [x
+ shiftX
, y
+ shiftY
], tr
));
86 getPotentialBishopMoves(sq
) {
87 const forward
= (this.turn
== 'w' ? -1 : 1);
88 return this.getSlideNJumpMoves(
90 V
.steps
[V
.BISHOP
].concat([ [forward
, 0] ]),
95 getPotentialQueenMoves(sq
) {
96 return this.getSlideNJumpMoves(
103 isAttackedByBishop(sq
, color
) {
104 const forward
= (color
== 'w' ? 1 : -1);
105 return this.isAttackedBySlideNJump(
109 V
.steps
[V
.BISHOP
].concat([ [forward
, 0] ]),
114 isAttackedByQueen(sq
, color
) {
115 return this.isAttackedBySlideNJump(
124 static get VALUES() {