b721273b501dc56cf680614b2a5718f261ed3029
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
{
7 static get HasFlags() {
11 static get HasEnpassant() {
15 static get Monochrome() {
19 static get Notoodark() {
23 static get PawnSpecs() {
27 { promotions: [V
.QUEEN
] }
32 return ChessRules
.PIECES
.concat(V
.PROMOTED
);
35 static get PROMOTED() {
39 static GenRandInitFen(options
) {
40 if (options
.randomness
== 0)
41 return "rnbqkbnr/8/pppppppp/8/8/PPPPPPPP/8/RNBKQBNR w 0";
43 let pieces
= { w: new Array(8), b: new Array(8) };
44 for (let c
of ["w", "b"]) {
45 if (c
== 'b' && options
.randomness
== 1) {
46 pieces
['b'] = pieces
['w'];
50 // Get random squares for every piece, totally freely (no castling)
51 let positions
= shuffle(ArrayFun
.range(8));
52 const composition
= ['b', 'b', 'r', 'r', 'n', 'n', 'k', 'q'];
53 for (let i
= 0; i
< 8; i
++) pieces
[c
][positions
[i
]] = composition
[i
];
56 pieces
["b"].join("") +
57 "/8/pppppppp/8/8/PPPPPPPP/8/" +
58 pieces
["w"].join("").toUpperCase() +
67 getPotentialMovesFrom([x
, y
]) {
68 if (this.getPiece(x
, y
) == V
.PROMOTED
)
69 return this.getPotentialQueenMoves([x
, y
]);
70 return super.getPotentialMovesFrom([x
, y
]);
73 getPotentialPawnMoves([x
, y
]) {
74 const color
= this.turn
;
75 const shiftX
= V
.PawnSpecs
.directions
[color
];
76 const sixthRank
= (color
== 'w' ? 2 : 5);
77 const tr
= (x
+ shiftX
== sixthRank
? { p: V
.PROMOTED
, c: color
} : null);
79 if (this.board
[x
+ shiftX
][y
] == V
.EMPTY
)
81 moves
.push(this.getBasicMove([x
, y
], [x
+ shiftX
, y
], tr
));
83 for (let shiftY
of [-1, 1]) {
85 y
+ shiftY
>= 0 && y
+ shiftY
< 8 &&
86 this.board
[x
+ shiftX
][y
+ shiftY
] != V
.EMPTY
&&
87 this.canTake([x
, y
], [x
+ shiftX
, y
+ shiftY
])
89 moves
.push(this.getBasicMove([x
, y
], [x
+ shiftX
, y
+ shiftY
], tr
));
95 getPotentialBishopMoves(sq
) {
96 const forward
= (this.turn
== 'w' ? -1 : 1);
97 return this.getSlideNJumpMoves(
98 sq
, V
.steps
[V
.BISHOP
].concat([ [forward
, 0] ]), 1);
101 getPotentialQueenMoves(sq
) {
102 return this.getSlideNJumpMoves(sq
, V
.steps
[V
.BISHOP
], 1);
105 isAttacked(sq
, color
) {
107 super.isAttacked(sq
, color
) || this.isAttackedByPromoted(sq
, color
)
111 isAttackedByBishop(sq
, color
) {
112 const forward
= (color
== 'w' ? 1 : -1);
113 return this.isAttackedBySlideNJump(
114 sq
, color
, V
.BISHOP
, V
.steps
[V
.BISHOP
].concat([ [forward
, 0] ]), 1);
117 isAttackedByQueen(sq
, color
) {
118 return this.isAttackedBySlideNJump(
119 sq
, color
, V
.QUEEN
, V
.steps
[V
.BISHOP
], 1);
122 isAttackedByPromoted(sq
, color
) {
123 return super.isAttackedBySlideNJump(
124 sq
, color
, V
.PROMOTED
, V
.steps
[V
.BISHOP
], 1);
127 static get VALUES() {