1 import { ChessRules
, PiPo
, Move
} from "@/base_rules";
2 import { ArrayFun
} from "@/utils/array";
4 export class ClorangeRules
extends ChessRules
{
5 static IsGoodFen(fen
) {
6 if (!ChessRules
.IsGoodFen(fen
)) return false;
7 const fenParsed
= V
.ParseFen(fen
);
9 if (!fenParsed
.reserve
|| !fenParsed
.reserve
.match(/^[0-9]{20,20}$/))
14 static ParseFen(fen
) {
15 const fenParts
= fen
.split(" ");
17 ChessRules
.ParseFen(fen
),
18 { reserve: fenParts
[5] }
22 static GenRandInitFen(randomness
) {
23 // Capturing and non-capturing reserves:
24 return ChessRules
.GenRandInitFen(randomness
) + " 00000000000000000000";
28 return super.getFen() + " " + this.getReserveFen();
32 return super.getFenForRepeat() + "_" + this.getReserveFen();
37 Object
.keys(this.reserve
).map(
38 c
=> Object
.values(this.reserve
[c
]).join("")).join("")
42 getEpSquare(moveOrSquare
) {
43 if (!moveOrSquare
) return undefined;
44 if (typeof moveOrSquare
=== "string") {
45 const square
= moveOrSquare
;
46 if (square
== "-") return undefined;
47 return V
.SquareToCoords(square
);
49 const move = moveOrSquare
;
54 Math
.abs(s
.x
- e
.x
) == 2 &&
55 move.vanish
.length
> 0 && ['p', 's'].includes(move.vanish
[0].p
)
65 setOtherVariables(fen
) {
66 super.setOtherVariables(fen
);
67 // Also init reserves (used by the interface to show landable pieces)
69 V
.ParseFen(fen
).reserve
.split("").map(x
=> parseInt(x
, 10));
99 if (i
>= V
.size
.x
) return i
== V
.size
.x
? "w" : "b";
100 return this.board
[i
][j
].charAt(0);
104 if (i
>= V
.size
.x
) return V
.RESERVE_PIECES
[j
];
105 return this.board
[i
][j
].charAt(1);
109 return (V
.NON_VIOLENT
.includes(b
[1]) ? "Clorange/" : "") + b
;
112 getReservePpath(index
, color
) {
114 (V
.NON_VIOLENT
.includes(V
.RESERVE_PIECES
[index
]) ? "Clorange/" : "");
115 return prefix
+ color
+ V
.RESERVE_PIECES
[index
];
118 static get NON_VIOLENT() {
119 return ['s', 'u', 'o', 'c', 't'];
122 static get PIECES() {
123 return ChessRules
.PIECES
.concat(V
.NON_VIOLENT
);
126 // Ordering on reserve pieces
127 static get RESERVE_PIECES() {
128 return V
.PIECES
.filter(p
=> p
!= 'k');
131 getReserveMoves([x
, y
]) {
132 const color
= this.turn
;
133 const p
= V
.RESERVE_PIECES
[y
];
134 if (this.reserve
[color
][p
] == 0) return [];
137 let rank2
= V
.size
.x
- 1;
138 if (['p', 's'].includes(p
)) {
139 if (color
== 'w') rank1
++;
142 for (let i
= rank1
; i
<= rank2
; i
++) {
143 for (let j
= 0; j
< V
.size
.y
; j
++) {
144 if (this.board
[i
][j
] == V
.EMPTY
) {
155 start: { x: x
, y: y
}, //a bit artificial...
165 getPotentialMovesFrom([x
, y
]) {
167 // Reserves, outside of board: x == sizeX(+1)
168 return this.getReserveMoves([x
, y
]);
170 switch (this.getPiece(x
, y
)) {
171 case 's': return super.getPotentialPawnMoves([x
, y
]);
172 case 'u': return super.getPotentialRookMoves([x
, y
]);
173 case 'o': return super.getPotentialKnightMoves([x
, y
]);
174 case 'c': return super.getPotentialBishopMoves([x
, y
]);
175 case 't': return super.getPotentialQueenMoves([x
, y
]);
176 default: return super.getPotentialMovesFrom([x
, y
]);
178 return []; //never reached
181 getPotentialPawnMoves(sq
) {
182 let moves
= super.getPotentialPawnMoves(sq
);
184 if (m
.vanish
[0].p
== 's' && m
.appear
[0].p
!= 's') {
185 // Promotion pieces should be non-violent as well:
186 const pIdx
= ChessRules
.PIECES
.findIndex(p
=> p
== m
.appear
[0].p
)
187 m
.appear
[0].p
= V
.NON_VIOLENT
[pIdx
];
193 getSlideNJumpMoves([x
, y
], steps
, oneStep
) {
195 const canTake
= ChessRules
.PIECES
.includes(this.getPiece(x
, y
));
196 outerLoop: for (let step
of steps
) {
199 while (V
.OnBoard(i
, j
) && this.board
[i
][j
] == V
.EMPTY
) {
200 moves
.push(this.getBasicMove([x
, y
], [i
, j
]));
201 if (oneStep
) continue outerLoop
;
205 if (V
.OnBoard(i
, j
) && canTake
&& this.canTake([x
, y
], [i
, j
]))
206 moves
.push(this.getBasicMove([x
, y
], [i
, j
]));
212 let moves
= super.getAllPotentialMoves();
213 const color
= this.turn
;
214 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
215 moves
= moves
.concat(
216 this.getReserveMoves([V
.size
.x
+ (color
== "w" ? 0 : 1), i
])
219 return this.filterValid(moves
);
223 if (!super.atLeastOneMove()) {
224 // Search one reserve move
225 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
226 let moves
= this.filterValid(
227 this.getReserveMoves([V
.size
.x
+ (this.turn
== "w" ? 0 : 1), i
])
229 if (moves
.length
> 0) return true;
239 if (move.vanish
.length
== 2 && move.appear
.length
== 2) return;
240 const color
= this.turn
;
241 if (move.vanish
.length
== 0) this.reserve
[color
][move.appear
[0].p
]--;
242 else if (move.vanish
.length
== 2) {
244 const normal
= ChessRules
.PIECES
.includes(move.vanish
[1].p
);
247 ? ChessRules
.PIECES
.findIndex(p
=> p
== move.vanish
[1].p
)
248 : V
.NON_VIOLENT
.findIndex(p
=> p
== move.vanish
[1].p
);
249 const rPiece
= (normal
? V
.NON_VIOLENT : ChessRules
.PIECES
)[pIdx
];
250 this.reserve
[move.vanish
[1].c
][rPiece
]++;
255 super.postUndo(move);
256 if (move.vanish
.length
== 2 && move.appear
.length
== 2) return;
257 const color
= this.turn
;
258 if (move.vanish
.length
== 0) this.reserve
[color
][move.appear
[0].p
]++;
259 else if (move.vanish
.length
== 2) {
260 const normal
= ChessRules
.PIECES
.includes(move.vanish
[1].p
);
263 ? ChessRules
.PIECES
.findIndex(p
=> p
== move.vanish
[1].p
)
264 : V
.NON_VIOLENT
.findIndex(p
=> p
== move.vanish
[1].p
);
265 const rPiece
= (normal
? V
.NON_VIOLENT : ChessRules
.PIECES
)[pIdx
];
266 this.reserve
[move.vanish
[1].c
][rPiece
]--;
270 static get SEARCH_DEPTH() {
274 static get VALUES() {
275 return Object
.assign(
288 let evaluation
= super.evalPosition();
290 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
291 const p
= V
.RESERVE_PIECES
[i
];
292 evaluation
+= this.reserve
["w"][p
] * V
.VALUES
[p
];
293 evaluation
-= this.reserve
["b"][p
] * V
.VALUES
[p
];
299 const finalSquare
= V
.CoordsToSquare(move.end
);
300 if (move.vanish
.length
> 0) {
301 // Standard move (maybe with non-violent piece)
302 let notation
= super.getNotation(move);
303 if (move.vanish
[0].p
== 's' && move.appear
[0].p
!= 's')
304 // Fix non-violent promotions:
305 notation
+= "=" + move.appear
[0].p
.toUpperCase();
310 move.appear
[0].p
!= V
.PAWN
? move.appear
[0].p
.toUpperCase() : "";
311 return piece
+ "@" + V
.CoordsToSquare(move.end
);