1 import { ChessRules
} from "@/base_rules";
3 export class ArenaRules
extends ChessRules
{
4 static get HasFlags() {
8 static get PawnSpecs() {
12 { captureBackward: true }
16 static IsGoodPosition(position
) {
17 if (position
.length
== 0) return false;
18 const rows
= position
.split("/");
19 if (rows
.length
!= V
.size
.x
) return false;
20 // At most and at least one king or queen per color
21 let royals
= { "k": 0, "K": 0, "q": 0, "Q": 0 };
22 for (let row
of rows
) {
24 for (let i
= 0; i
< row
.length
; i
++) {
25 if (['K','k','Q','q'].includes(row
[i
])) royals
[row
[i
]]++;
26 if (V
.PIECES
.includes(row
[i
].toLowerCase())) sumElts
++;
28 const num
= parseInt(row
[i
]);
29 if (isNaN(num
)) return false;
33 if (sumElts
!= V
.size
.y
) return false;
36 Object
.values(royals
).some(v
=> v
>= 2) ||
37 royals
['K'] + royals
['Q'] == 0 ||
38 royals
['k'] + royals
['q'] == 0
47 static GenRandInitFen(randomness
) {
48 return ChessRules
.GenRandInitFen(randomness
).slice(0, -6) + "-";
52 return Math
.abs(3.5 - x
) <= 1.5;
55 getPotentialMovesFrom([x
, y
]) {
56 const moves
= super.getPotentialMovesFrom([x
, y
]);
57 // Eliminate moves which neither enter the arena or capture something
58 return moves
.filter(m
=> {
59 const startInArena
= V
.InArena(m
.start
.x
);
60 const endInArena
= V
.InArena(m
.end
.x
);
62 (startInArena
&& endInArena
&& m
.vanish
.length
== 2) ||
63 (!startInArena
&& endInArena
)
70 getPotentialQueenMoves(sq
) {
71 return this.getSlideNJumpMoves(
73 V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
])
75 // Filter out moves longer than 3 squares
77 Math
.abs(m
.end
.x
- m
.start
.x
),
78 Math
.abs(m
.end
.y
- m
.start
.y
)) <= 3;
82 getPotentialKingMoves(sq
) {
83 return this.getSlideNJumpMoves(
85 V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
])
87 // Filter out moves longer than 3 squares
89 Math
.abs(m
.end
.x
- m
.start
.x
),
90 Math
.abs(m
.end
.y
- m
.start
.y
)) <= 3;
99 // No check conditions
104 const color
= this.turn
;
105 if (!this.atLeastOneMove())
106 // I cannot move anymore
107 return color
== "w" ? "0-1" : "1-0";
108 // Win if the opponent has no more pieces left (in the Arena),
109 // (and/)or if he lost both his dukes.
110 let someUnitRemain
= false;
111 let atLeastOneDuke
= false;
112 let somethingInArena
= false;
113 outerLoop: for (let i
=0; i
<V
.size
.x
; i
++) {
114 for (let j
=0; j
<V
.size
.y
; j
++) {
115 if (this.getColor(i
,j
) == color
) {
116 someUnitRemain
= true;
117 if (this.movesCount
>= 2 && V
.InArena(i
)) {
118 somethingInArena
= true;
122 if ([V
.QUEEN
,V
.KING
].includes(this.getPiece(i
,j
))) {
123 atLeastOneDuke
= true;
124 if (this.movesCount
< 2 || somethingInArena
)
133 (this.movesCount
>= 2 && !somethingInArena
)
135 return color
== "w" ? "0-1" : "1-0";
140 static get SEARCH_DEPTH() {