1 import { ChessRules
, PiPo
, Move
} from "@/base_rules";
2 import { ArrayFun
} from "@/utils/array";
4 export class ClorangeRules
extends ChessRules
{
6 static IsGoodFen(fen
) {
7 if (!ChessRules
.IsGoodFen(fen
)) return false;
8 const fenParsed
= V
.ParseFen(fen
);
10 if (!fenParsed
.reserve
|| !fenParsed
.reserve
.match(/^[0-9]{20,20}$/))
15 static ParseFen(fen
) {
16 const fenParts
= fen
.split(" ");
18 ChessRules
.ParseFen(fen
),
19 { reserve: fenParts
[5] }
23 static GenRandInitFen(randomness
) {
24 // Capturing and non-capturing reserves:
25 return ChessRules
.GenRandInitFen(randomness
) + " 00000000000000000000";
29 return super.getFen() + " " + this.getReserveFen();
33 return super.getFenForRepeat() + "_" + this.getReserveFen();
38 Object
.keys(this.reserve
).map(
39 c
=> Object
.values(this.reserve
[c
]).join("")).join("")
43 getEpSquare(moveOrSquare
) {
44 if (!moveOrSquare
) return undefined;
45 if (typeof moveOrSquare
=== "string") {
46 const square
= moveOrSquare
;
47 if (square
== "-") return undefined;
48 return V
.SquareToCoords(square
);
50 const move = moveOrSquare
;
55 Math
.abs(s
.x
- e
.x
) == 2 &&
56 move.vanish
.length
> 0 && ['p', 's'].includes(move.vanish
[0].p
)
66 setOtherVariables(fen
) {
67 super.setOtherVariables(fen
);
68 // Also init reserves (used by the interface to show landable pieces)
70 V
.ParseFen(fen
).reserve
.split("").map(x
=> parseInt(x
, 10));
100 if (i
>= V
.size
.x
) return i
== V
.size
.x
? "w" : "b";
101 return this.board
[i
][j
].charAt(0);
105 if (i
>= V
.size
.x
) return V
.RESERVE_PIECES
[j
];
106 return this.board
[i
][j
].charAt(1);
110 return (V
.NON_VIOLENT
.includes(b
[1]) ? "Clorange/" : "") + b
;
113 getReservePpath(index
, color
) {
115 (V
.NON_VIOLENT
.includes(V
.RESERVE_PIECES
[index
]) ? "Clorange/" : "");
116 return prefix
+ color
+ V
.RESERVE_PIECES
[index
];
119 static get NON_VIOLENT() {
120 return ['s', 'u', 'o', 'c', 't'];
123 static get PIECES() {
124 return ChessRules
.PIECES
.concat(V
.NON_VIOLENT
);
127 // Ordering on reserve pieces
128 static get RESERVE_PIECES() {
129 return V
.PIECES
.filter(p
=> p
!= 'k');
132 getReserveMoves([x
, y
]) {
133 const color
= this.turn
;
134 const p
= V
.RESERVE_PIECES
[y
];
135 if (this.reserve
[color
][p
] == 0) return [];
138 let rank2
= V
.size
.x
- 1;
139 if (['p', 's'].includes(p
)) {
140 if (color
== 'w') rank1
++;
143 for (let i
= rank1
; i
<= rank2
; i
++) {
144 for (let j
= 0; j
< V
.size
.y
; j
++) {
145 if (this.board
[i
][j
] == V
.EMPTY
) {
156 start: { x: x
, y: y
}, //a bit artificial...
166 getPotentialMovesFrom([x
, y
]) {
168 // Reserves, outside of board: x == sizeX(+1)
169 return this.getReserveMoves([x
, y
]);
171 switch (this.getPiece(x
, y
)) {
172 case 's': return this.getPotentialPawnMoves([x
, y
]);
173 case 'u': return super.getPotentialRookMoves([x
, y
]);
174 case 'o': return super.getPotentialKnightMoves([x
, y
]);
175 case 'c': return super.getPotentialBishopMoves([x
, y
]);
176 case 't': return super.getPotentialQueenMoves([x
, y
]);
177 default: return super.getPotentialMovesFrom([x
, y
]);
179 return []; //never reached
182 getPotentialPawnMoves(sq
) {
183 let moves
= super.getPotentialPawnMoves(sq
);
184 if (moves
.length
> 0 && moves
[0].vanish
[0].p
== 's') {
185 // Remove captures for non-violent pawns:
186 moves
= moves
.filter(m
=> m
.vanish
.length
== 1);
188 if (m
.appear
[0].p
!= 's') {
189 // Promotion pieces should be non-violent as well:
190 const pIdx
= ChessRules
.PIECES
.findIndex(p
=> p
== m
.appear
[0].p
)
191 m
.appear
[0].p
= V
.NON_VIOLENT
[pIdx
];
198 getSlideNJumpMoves([x
, y
], steps
, oneStep
) {
200 const canTake
= ChessRules
.PIECES
.includes(this.getPiece(x
, y
));
201 outerLoop: for (let step
of steps
) {
204 while (V
.OnBoard(i
, j
) && this.board
[i
][j
] == V
.EMPTY
) {
205 moves
.push(this.getBasicMove([x
, y
], [i
, j
]));
206 if (oneStep
) continue outerLoop
;
210 if (V
.OnBoard(i
, j
) && canTake
&& this.canTake([x
, y
], [i
, j
]))
211 moves
.push(this.getBasicMove([x
, y
], [i
, j
]));
217 let moves
= super.getAllPotentialMoves();
218 const color
= this.turn
;
219 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
220 moves
= moves
.concat(
221 this.getReserveMoves([V
.size
.x
+ (color
== "w" ? 0 : 1), i
])
224 return this.filterValid(moves
);
228 if (!super.atLeastOneMove()) {
229 // Search one reserve move
230 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
231 let moves
= this.filterValid(
232 this.getReserveMoves([V
.size
.x
+ (this.turn
== "w" ? 0 : 1), i
])
234 if (moves
.length
> 0) return true;
244 if (move.vanish
.length
== 2 && move.appear
.length
== 2) return;
245 const color
= this.turn
;
246 if (move.vanish
.length
== 0) this.reserve
[color
][move.appear
[0].p
]--;
247 else if (move.vanish
.length
== 2) {
249 const normal
= ChessRules
.PIECES
.includes(move.vanish
[1].p
);
252 ? ChessRules
.PIECES
.findIndex(p
=> p
== move.vanish
[1].p
)
253 : V
.NON_VIOLENT
.findIndex(p
=> p
== move.vanish
[1].p
);
254 const rPiece
= (normal
? V
.NON_VIOLENT : ChessRules
.PIECES
)[pIdx
];
255 this.reserve
[move.vanish
[1].c
][rPiece
]++;
260 super.postUndo(move);
261 if (move.vanish
.length
== 2 && move.appear
.length
== 2) return;
262 const color
= this.turn
;
263 if (move.vanish
.length
== 0) this.reserve
[color
][move.appear
[0].p
]++;
264 else if (move.vanish
.length
== 2) {
265 const normal
= ChessRules
.PIECES
.includes(move.vanish
[1].p
);
268 ? ChessRules
.PIECES
.findIndex(p
=> p
== move.vanish
[1].p
)
269 : V
.NON_VIOLENT
.findIndex(p
=> p
== move.vanish
[1].p
);
270 const rPiece
= (normal
? V
.NON_VIOLENT : ChessRules
.PIECES
)[pIdx
];
271 this.reserve
[move.vanish
[1].c
][rPiece
]--;
275 static get SEARCH_DEPTH() {
279 static get VALUES() {
280 return Object
.assign(
293 let evaluation
= super.evalPosition();
295 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
296 const p
= V
.RESERVE_PIECES
[i
];
297 evaluation
+= this.reserve
["w"][p
] * V
.VALUES
[p
];
298 evaluation
-= this.reserve
["b"][p
] * V
.VALUES
[p
];
304 const finalSquare
= V
.CoordsToSquare(move.end
);
305 if (move.vanish
.length
> 0) {
306 // Standard move (maybe with non-violent piece)
307 let notation
= super.getNotation(move);
308 if (move.vanish
[0].p
== 's' && move.appear
[0].p
!= 's')
309 // Fix non-violent promotions:
310 notation
+= "=" + move.appear
[0].p
.toUpperCase();
315 move.appear
[0].p
!= V
.PAWN
? move.appear
[0].p
.toUpperCase() : "";
316 return piece
+ "@" + V
.CoordsToSquare(move.end
);