ff6e1e1d34c6f02e949013b8e9228a67646666fe
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
) {
23 if (!randomness
) randomness
= 2;
25 return "11/11/11/11/11/11/11/11/11/QRBNP1pnbrq/KRBNP1pnbrk w 0";
27 let pieces
= { w: new Array(10), b: new Array(10) };
28 // Shuffle pieces on first and second rank
29 for (let c
of ["w", "b"]) {
30 if (c
== 'b' && randomness
== 1) {
31 pieces
['b'] = JSON
.parse(JSON
.stringify(pieces
['w'])).reverse();
33 pieces
['b'].splice(5,10).reverse().concat(
34 pieces
['b'].splice(0,5).reverse());
38 // Reserve 4 and 5 which are pawns positions
39 let positions
= ArrayFun
.range(10).filter(i
=> i
!= 4 && i
!= 5);
41 // Get random squares for bishops
42 let randIndex
= 2 * randInt(4);
43 const bishop1Pos
= positions
[randIndex
];
44 // The second bishop must be on a square of different color
45 let randIndex_tmp
= 2 * randInt(4) + 1;
46 const bishop2Pos
= positions
[randIndex_tmp
];
47 // Remove chosen squares
48 positions
.splice(Math
.max(randIndex
, randIndex_tmp
), 1);
49 positions
.splice(Math
.min(randIndex
, randIndex_tmp
), 1);
51 // Place the king at random on (remaining squares of) first row
53 if (positions
[maxIndex
-1] >= 4)
55 if (positions
[maxIndex
-1] >= 4)
57 randIndex
= randInt(maxIndex
);
58 const kingPos
= positions
[randIndex
];
59 positions
.splice(randIndex
, 1);
61 // Get random squares for knights
62 randIndex
= randInt(5);
63 const knight1Pos
= positions
[randIndex
];
64 positions
.splice(randIndex
, 1);
65 randIndex
= randInt(4);
66 const knight2Pos
= positions
[randIndex
];
67 positions
.splice(randIndex
, 1);
69 // Get random squares for rooks
70 randIndex
= randInt(3);
71 const rook1Pos
= positions
[randIndex
];
72 positions
.splice(randIndex
, 1);
73 randIndex
= randInt(2);
74 const rook2Pos
= positions
[randIndex
];
75 positions
.splice(randIndex
, 1);
77 // Queen position is now determined,
78 // because pawns are not placed at random
79 const queenPos
= positions
[0];
81 // Finally put the shuffled pieces in the board array
82 pieces
[c
][rook1Pos
] = "r";
83 pieces
[c
][knight1Pos
] = "n";
84 pieces
[c
][bishop1Pos
] = "b";
85 pieces
[c
][queenPos
] = "q";
86 pieces
[c
][kingPos
] = "k";
87 pieces
[c
][bishop2Pos
] = "b";
88 pieces
[c
][knight2Pos
] = "n";
89 pieces
[c
][rook2Pos
] = "r";
93 const whiteFen
= pieces
["w"].join("").toUpperCase();
94 const blackFen
= pieces
["b"].join("");
96 "11/11/11/11/11/11/11/11/11/" +
97 whiteFen
.substr(5).split("").reverse().join("") +
99 blackFen
.substr(5).split("").join("") +
101 whiteFen
.substr(0,5) +
103 blackFen
.substr(0,5).split("").reverse().join("") +
108 getPotentialPawnMoves([x
, y
]) {
109 // Normal moves (as a rook)
111 this.getSlideNJumpMoves([x
, y
], V
.steps
[V
.ROOK
]).filter(m
=> {
112 // Remove captures. Alt: redefine canTake
113 return m
.vanish
.length
== 1;
116 // Captures (in both directions)
117 for (let shiftX
of [-1, 1]) {
118 for (let shiftY
of [-1, 1]) {
120 V
.OnBoard(x
+ shiftX
, y
+ shiftY
) &&
121 this.board
[x
+ shiftX
][y
+ shiftY
] != V
.EMPTY
&&
122 this.canTake([x
, y
], [x
+ shiftX
, y
+ shiftY
])
124 moves
.push(this.getBasicMove([x
, y
], [x
+ shiftX
, y
+ shiftY
]));
132 getPotentialKnightMoves(sq
) {
133 // Knight becomes knightrider:
134 return this.getSlideNJumpMoves(sq
, V
.steps
[V
.KNIGHT
]);
137 // What are the king moves from square x,y ?
138 getPotentialKingMoves(sq
) {
139 return this.getSlideNJumpMoves(
141 V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]),
147 if (moves
.length
== 0) return [];
148 const color
= this.turn
;
149 const oppCol
= V
.GetOppCol(color
);
150 return moves
.filter(m
=> {
152 // Giving check is forbidden as well:
153 const res
= !this.underCheck(color
) && !this.underCheck(oppCol
);
159 isAttackedByPawn([x
, y
], colors
) {
161 if (x
+ pawnShift
< V
.size
.x
) {
162 for (let c
of colors
) {
163 for (let i
of [-1, 1]) {
167 this.getPiece(x
+ pawnShift
, y
+ i
) == V
.PAWN
&&
168 this.getColor(x
+ pawnShift
, y
+ i
) == c
178 isAttackedByKnight(sq
, colors
) {
179 return this.isAttackedBySlideNJump(
189 const color
= V
.GetOppCol(this.turn
);
190 if (this.kingPos
[color
][0] == 0)
191 // The opposing edge is reached!
192 return color
== "w" ? "1-0" : "0-1";
193 if (this.atLeastOneMove())
195 // Stalemate (will probably never happen)
199 static get SEARCH_DEPTH() {
203 static get VALUES() {
216 let evaluation
= super.evalPosition();
217 // Ponder with king position:
218 return evaluation
/5 + this.kingPos
["b"][0] - this.kingPos
["w"][0];
222 // Since pawns are much more mobile, treat them as other pieces:
224 move.vanish
[0].p
.toUpperCase() +
225 (move.vanish
.length
> move.appear
.length
? "x" : "") +
226 V
.CoordsToSquare(move.end
)