39ee02aaac604ee4306b3d03b845ef282022cf9f
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 HasCastle() {
14 static get HasEnpassant() {
18 static get CanFlip() {
23 return { x: 11, y: 11 };
26 static GenRandInitFen(randomness
) {
28 return "92/92/92/92/92/92/92/92/92/qrbnp1PNBRQ/krbnp1PNBRK w 0";
30 let pieces
= { w: new Array(10), b: new Array(10) };
31 // Shuffle pieces on first and second rank
32 for (let c
of ["w", "b"]) {
33 if (c
== 'b' && randomness
== 1) {
34 pieces
['b'] = JSON
.parse(JSON
.stringify(pieces
['w'])).reverse();
36 pieces
['b'].splice(5,10).reverse().concat(
37 pieces
['b'].splice(0,5).reverse());
41 // Reserve 4 and 5 which are pawns positions
42 let positions
= ArrayFun
.range(10).filter(i
=> i
!= 4 && i
!= 5);
44 // Get random squares for bishops
45 let randIndex
= 2 * randInt(4);
46 const bishop1Pos
= positions
[randIndex
];
47 // The second bishop must be on a square of different color
48 let randIndex_tmp
= 2 * randInt(4) + 1;
49 const bishop2Pos
= positions
[randIndex_tmp
];
50 // Remove chosen squares
51 positions
.splice(Math
.max(randIndex
, randIndex_tmp
), 1);
52 positions
.splice(Math
.min(randIndex
, randIndex_tmp
), 1);
54 // Place the king at random on (remaining squares of) first row
56 if (positions
[maxIndex
-1] >= 4)
58 if (positions
[maxIndex
-1] >= 4)
60 randIndex
= randInt(maxIndex
);
61 const kingPos
= positions
[randIndex
];
62 positions
.splice(randIndex
, 1);
64 // Get random squares for knights
65 randIndex
= randInt(5);
66 const knight1Pos
= positions
[randIndex
];
67 positions
.splice(randIndex
, 1);
68 randIndex
= randInt(4);
69 const knight2Pos
= positions
[randIndex
];
70 positions
.splice(randIndex
, 1);
72 // Get random squares for rooks
73 randIndex
= randInt(3);
74 const rook1Pos
= positions
[randIndex
];
75 positions
.splice(randIndex
, 1);
76 randIndex
= randInt(2);
77 const rook2Pos
= positions
[randIndex
];
78 positions
.splice(randIndex
, 1);
80 // Queen position is now determined,
81 // because pawns are not placed at random
82 const queenPos
= positions
[0];
84 // Finally put the shuffled pieces in the board array
85 pieces
[c
][rook1Pos
] = "r";
86 pieces
[c
][knight1Pos
] = "n";
87 pieces
[c
][bishop1Pos
] = "b";
88 pieces
[c
][queenPos
] = "q";
89 pieces
[c
][kingPos
] = "k";
90 pieces
[c
][bishop2Pos
] = "b";
91 pieces
[c
][knight2Pos
] = "n";
92 pieces
[c
][rook2Pos
] = "r";
96 const whiteFen
= pieces
["w"].join("").toUpperCase();
97 const blackFen
= pieces
["b"].join("");
99 "92/92/92/92/92/92/92/92/92/" +
100 blackFen
.substr(5).split("").reverse().join("") +
102 whiteFen
.substr(5).split("").join("") +
104 blackFen
.substr(0,5) +
106 whiteFen
.substr(0,5).split("").reverse().join("") +
111 getPotentialPawnMoves([x
, y
]) {
112 // Normal moves (as a rook)
114 this.getSlideNJumpMoves([x
, y
], V
.steps
[V
.ROOK
]).filter(m
=> {
115 // Remove captures. Alt: redefine canTake
116 return m
.vanish
.length
== 1;
119 // Captures (in both directions)
120 for (let shiftX
of [-1, 1]) {
121 for (let shiftY
of [-1, 1]) {
123 V
.OnBoard(x
+ shiftX
, y
+ shiftY
) &&
124 this.board
[x
+ shiftX
][y
+ shiftY
] != V
.EMPTY
&&
125 this.canTake([x
, y
], [x
+ shiftX
, y
+ shiftY
])
127 moves
.push(this.getBasicMove([x
, y
], [x
+ shiftX
, y
+ shiftY
]));
135 getPotentialKnightMoves(sq
) {
136 // Knight becomes knightrider:
137 return this.getSlideNJumpMoves(sq
, V
.steps
[V
.KNIGHT
]);
141 if (moves
.length
== 0) return [];
142 const color
= this.turn
;
143 const oppCol
= V
.GetOppCol(color
);
144 return moves
.filter(m
=> {
146 // Giving check is forbidden as well:
147 const res
= !this.underCheck(color
) && !this.underCheck(oppCol
);
153 isAttackedByPawn([x
, y
], color
) {
154 // Pawns can capture forward and backward:
155 for (let pawnShift
of [-1, 1]) {
156 if (0 < x
+ pawnShift
&& x
+ pawnShift
< V
.size
.x
) {
157 for (let i
of [-1, 1]) {
161 this.getPiece(x
+ pawnShift
, y
+ i
) == V
.PAWN
&&
162 this.getColor(x
+ pawnShift
, y
+ i
) == color
172 isAttackedByKnight(sq
, color
) {
173 return this.isAttackedBySlideNJump(
183 const color
= V
.GetOppCol(this.turn
);
184 if (this.kingPos
[color
][0] == 0)
185 // The opposing edge is reached!
186 return color
== "w" ? "1-0" : "0-1";
187 if (this.atLeastOneMove()) return "*";
188 // Stalemate (will probably never happen)
192 static get SEARCH_DEPTH() {
196 static get VALUES() {
209 let evaluation
= super.evalPosition();
210 // Ponder with king position:
211 return evaluation
/5 + this.kingPos
["b"][0] - this.kingPos
["w"][0];
215 // Since pawns are much more mobile, treat them as other pieces:
217 move.vanish
[0].p
.toUpperCase() +
218 (move.vanish
.length
> move.appear
.length
? "x" : "") +
219 V
.CoordsToSquare(move.end
)