1 import { ChessRules
} from "@/base_rules";
2 import { ArrayFun
} from "@/utils/array";
3 import { randInt
, shuffle
} from "@/utils/alea";
5 export 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 "92/92/92/92/92/92/92/92/92/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 "92/92/92/92/92/92/92/92/92/" +
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
]);
137 if (moves
.length
== 0) return [];
138 const color
= this.turn
;
139 const oppCol
= V
.GetOppCol(color
);
140 return moves
.filter(m
=> {
142 // Giving check is forbidden as well:
143 const res
= !this.underCheck(color
) && !this.underCheck(oppCol
);
149 isAttackedByPawn([x
, y
], color
) {
150 // Pawns can capture forward and backward:
151 for (let pawnShift
of [-1, 1]) {
152 if (0 < x
+ pawnShift
&& x
+ pawnShift
< V
.size
.x
) {
153 for (let i
of [-1, 1]) {
157 this.getPiece(x
+ pawnShift
, y
+ i
) == V
.PAWN
&&
158 this.getColor(x
+ pawnShift
, y
+ i
) == color
168 isAttackedByKnight(sq
, color
) {
169 return this.isAttackedBySlideNJump(
179 const color
= V
.GetOppCol(this.turn
);
180 if (this.kingPos
[color
][0] == 0)
181 // The opposing edge is reached!
182 return color
== "w" ? "1-0" : "0-1";
183 if (this.atLeastOneMove()) return "*";
184 // Stalemate (will probably never happen)
188 static get SEARCH_DEPTH() {
192 static get VALUES() {
205 let evaluation
= super.evalPosition();
206 // Ponder with king position:
207 return evaluation
/5 + this.kingPos
["b"][0] - this.kingPos
["w"][0];
211 // Since pawns are much more mobile, treat them as other pieces:
213 move.vanish
[0].p
.toUpperCase() +
214 (move.vanish
.length
> move.appear
.length
? "x" : "") +
215 V
.CoordsToSquare(move.end
)