30256fa6f8d0d1074d91f018814c1e069d19a79d
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(options
) {
24 // Capturing and non-capturing reserves:
25 return ChessRules
.GenRandInitFen(options
) + " 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 canTake([x1
, y1
], [x2
, y2
]) {
200 this.getColor(x1
, y1
) !== this.getColor(x2
, y2
) &&
201 ChessRules
.PIECES
.includes(this.getPiece(x1
, y1
))
206 let moves
= super.getAllPotentialMoves();
207 const color
= this.turn
;
208 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
209 moves
= moves
.concat(
210 this.getReserveMoves([V
.size
.x
+ (color
== "w" ? 0 : 1), i
])
213 return this.filterValid(moves
);
217 if (!super.atLeastOneMove()) {
218 // Search one reserve move
219 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
220 let moves
= this.filterValid(
221 this.getReserveMoves([V
.size
.x
+ (this.turn
== "w" ? 0 : 1), i
])
223 if (moves
.length
> 0) return true;
233 if (move.vanish
.length
== 2 && move.appear
.length
== 2) return;
234 const color
= this.turn
;
235 if (move.vanish
.length
== 0) this.reserve
[color
][move.appear
[0].p
]--;
236 else if (move.vanish
.length
== 2) {
238 const normal
= ChessRules
.PIECES
.includes(move.vanish
[1].p
);
241 ? ChessRules
.PIECES
.findIndex(p
=> p
== move.vanish
[1].p
)
242 : V
.NON_VIOLENT
.findIndex(p
=> p
== move.vanish
[1].p
);
243 const rPiece
= (normal
? V
.NON_VIOLENT : ChessRules
.PIECES
)[pIdx
];
244 this.reserve
[move.vanish
[1].c
][rPiece
]++;
249 super.postUndo(move);
250 if (move.vanish
.length
== 2 && move.appear
.length
== 2) return;
251 const color
= this.turn
;
252 if (move.vanish
.length
== 0) this.reserve
[color
][move.appear
[0].p
]++;
253 else if (move.vanish
.length
== 2) {
254 const normal
= ChessRules
.PIECES
.includes(move.vanish
[1].p
);
257 ? ChessRules
.PIECES
.findIndex(p
=> p
== move.vanish
[1].p
)
258 : V
.NON_VIOLENT
.findIndex(p
=> p
== move.vanish
[1].p
);
259 const rPiece
= (normal
? V
.NON_VIOLENT : ChessRules
.PIECES
)[pIdx
];
260 this.reserve
[move.vanish
[1].c
][rPiece
]--;
264 static get SEARCH_DEPTH() {
268 static get VALUES() {
269 return Object
.assign(
282 let evaluation
= super.evalPosition();
284 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
285 const p
= V
.RESERVE_PIECES
[i
];
286 evaluation
+= this.reserve
["w"][p
] * V
.VALUES
[p
];
287 evaluation
-= this.reserve
["b"][p
] * V
.VALUES
[p
];
293 const finalSquare
= V
.CoordsToSquare(move.end
);
294 if (move.vanish
.length
> 0) {
295 // Standard move (maybe with non-violent piece)
296 let notation
= super.getNotation(move);
297 if (move.vanish
[0].p
== 's' && move.appear
[0].p
!= 's')
298 // Fix non-violent promotions:
299 notation
+= "=" + move.appear
[0].p
.toUpperCase();
304 move.appear
[0].p
!= V
.PAWN
? move.appear
[0].p
.toUpperCase() : "";
305 return piece
+ "@" + V
.CoordsToSquare(move.end
);