f5c7550a16b920b507d38a19a7375e231b96cb2d
1 import { ChessRules
} from "@/base_rules";
2 import { ArrayFun
} from "@/utils/array";
3 import { randInt
, shuffle
} from "@/utils/alea";
5 export const VariantRules
= class RoyalraceRules
extends ChessRules
{
6 static get HasFlags() {
10 static get HasEnpassant() {
14 static get CanFlip() {
19 return { x: 11, y: 11 };
22 static GenRandInitFen(randomness
) {
24 return "11/11/11/11/11/11/11/11/11/qrbnp1PNBRQ/krbnp1PNBRK w 0";
26 let pieces
= { w: new Array(10), b: new Array(10) };
27 // Shuffle pieces on first and second rank
28 for (let c
of ["w", "b"]) {
29 if (c
== 'b' && randomness
== 1) {
30 pieces
['b'] = JSON
.parse(JSON
.stringify(pieces
['w'])).reverse();
32 pieces
['b'].splice(5,10).reverse().concat(
33 pieces
['b'].splice(0,5).reverse());
37 // Reserve 4 and 5 which are pawns positions
38 let positions
= ArrayFun
.range(10).filter(i
=> i
!= 4 && i
!= 5);
40 // Get random squares for bishops
41 let randIndex
= 2 * randInt(4);
42 const bishop1Pos
= positions
[randIndex
];
43 // The second bishop must be on a square of different color
44 let randIndex_tmp
= 2 * randInt(4) + 1;
45 const bishop2Pos
= positions
[randIndex_tmp
];
46 // Remove chosen squares
47 positions
.splice(Math
.max(randIndex
, randIndex_tmp
), 1);
48 positions
.splice(Math
.min(randIndex
, randIndex_tmp
), 1);
50 // Place the king at random on (remaining squares of) first row
52 if (positions
[maxIndex
-1] >= 4)
54 if (positions
[maxIndex
-1] >= 4)
56 randIndex
= randInt(maxIndex
);
57 const kingPos
= positions
[randIndex
];
58 positions
.splice(randIndex
, 1);
60 // Get random squares for knights
61 randIndex
= randInt(5);
62 const knight1Pos
= positions
[randIndex
];
63 positions
.splice(randIndex
, 1);
64 randIndex
= randInt(4);
65 const knight2Pos
= positions
[randIndex
];
66 positions
.splice(randIndex
, 1);
68 // Get random squares for rooks
69 randIndex
= randInt(3);
70 const rook1Pos
= positions
[randIndex
];
71 positions
.splice(randIndex
, 1);
72 randIndex
= randInt(2);
73 const rook2Pos
= positions
[randIndex
];
74 positions
.splice(randIndex
, 1);
76 // Queen position is now determined,
77 // because pawns are not placed at random
78 const queenPos
= positions
[0];
80 // Finally put the shuffled pieces in the board array
81 pieces
[c
][rook1Pos
] = "r";
82 pieces
[c
][knight1Pos
] = "n";
83 pieces
[c
][bishop1Pos
] = "b";
84 pieces
[c
][queenPos
] = "q";
85 pieces
[c
][kingPos
] = "k";
86 pieces
[c
][bishop2Pos
] = "b";
87 pieces
[c
][knight2Pos
] = "n";
88 pieces
[c
][rook2Pos
] = "r";
92 const whiteFen
= pieces
["w"].join("").toUpperCase();
93 const blackFen
= pieces
["b"].join("");
95 "11/11/11/11/11/11/11/11/11/" +
96 blackFen
.substr(5).split("").reverse().join("") +
98 whiteFen
.substr(5).split("").join("") +
100 blackFen
.substr(0,5) +
102 whiteFen
.substr(0,5).split("").reverse().join("") +
107 getPotentialPawnMoves([x
, y
]) {
108 // Normal moves (as a rook)
110 this.getSlideNJumpMoves([x
, y
], V
.steps
[V
.ROOK
]).filter(m
=> {
111 // Remove captures. Alt: redefine canTake
112 return m
.vanish
.length
== 1;
115 // Captures (in both directions)
116 for (let shiftX
of [-1, 1]) {
117 for (let shiftY
of [-1, 1]) {
119 V
.OnBoard(x
+ shiftX
, y
+ shiftY
) &&
120 this.board
[x
+ shiftX
][y
+ shiftY
] != V
.EMPTY
&&
121 this.canTake([x
, y
], [x
+ shiftX
, y
+ shiftY
])
123 moves
.push(this.getBasicMove([x
, y
], [x
+ shiftX
, y
+ shiftY
]));
131 getPotentialKnightMoves(sq
) {
132 // Knight becomes knightrider:
133 return this.getSlideNJumpMoves(sq
, V
.steps
[V
.KNIGHT
]);
136 // What are the king moves from square x,y ?
137 getPotentialKingMoves(sq
) {
138 return this.getSlideNJumpMoves(
140 V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]),
146 if (moves
.length
== 0) return [];
147 const color
= this.turn
;
148 const oppCol
= V
.GetOppCol(color
);
149 return moves
.filter(m
=> {
151 // Giving check is forbidden as well:
152 const res
= !this.underCheck(color
) && !this.underCheck(oppCol
);
158 isAttackedByPawn([x
, y
], colors
) {
160 if (x
+ pawnShift
< V
.size
.x
) {
161 for (let c
of colors
) {
162 for (let i
of [-1, 1]) {
166 this.getPiece(x
+ pawnShift
, y
+ i
) == V
.PAWN
&&
167 this.getColor(x
+ pawnShift
, y
+ i
) == c
177 isAttackedByKnight(sq
, colors
) {
178 return this.isAttackedBySlideNJump(
188 const color
= V
.GetOppCol(this.turn
);
189 if (this.kingPos
[color
][0] == 0)
190 // The opposing edge is reached!
191 return color
== "w" ? "1-0" : "0-1";
192 if (this.atLeastOneMove()) return "*";
193 // Stalemate (will probably never happen)
197 static get SEARCH_DEPTH() {
201 static get VALUES() {
214 let evaluation
= super.evalPosition();
215 // Ponder with king position:
216 return evaluation
/5 + this.kingPos
["b"][0] - this.kingPos
["w"][0];
220 // Since pawns are much more mobile, treat them as other pieces:
222 move.vanish
[0].p
.toUpperCase() +
223 (move.vanish
.length
> move.appear
.length
? "x" : "") +
224 V
.CoordsToSquare(move.end
)