1 import { ChessRules
, PiPo
, Move
} from "@/base_rules";
2 import { ArrayFun
} from "@/utils/array";
4 export class ClorangeRules
extends ChessRules
{
5 static get PawnSpecs() {
9 // TODO: pawns reaching last rank promote normally? Seems better
10 { promotions: [V
.PAWN
] }
14 static IsGoodFen(fen
) {
15 if (!ChessRules
.IsGoodFen(fen
)) return false;
16 const fenParsed
= V
.ParseFen(fen
);
18 if (!fenParsed
.reserve
|| !fenParsed
.reserve
.match(/^[0-9]{20,20}$/))
23 static ParseFen(fen
) {
24 const fenParts
= fen
.split(" ");
26 ChessRules
.ParseFen(fen
),
27 { reserve: fenParts
[5] }
31 static GenRandInitFen(randomness
) {
32 // Capturing and non-capturing reserves:
33 return ChessRules
.GenRandInitFen(randomness
) + " 00000000000000000000";
37 return super.getFen() + " " + this.getReserveFen();
41 return super.getFenForRepeat() + "_" + this.getReserveFen();
45 let counts
= new Array(10);
48 i
< V
.PIECES
.length
- 1;
49 i
++ //-1: no king reserve
52 counts
[i
] = this.reserve
["w"][V
.PIECES
[i
]];
53 counts
[5 + i
] = this.reserve
["b"][V
.PIECES
[i
]];
55 return counts
.join("");
58 setOtherVariables(fen
) {
59 super.setOtherVariables(fen
);
60 const fenParsed
= V
.ParseFen(fen
);
61 // Also init reserves (used by the interface to show landable pieces)
65 [V
.PAWN
]: parseInt(fenParsed
.reserve
[0]),
66 [V
.ROOK
]: parseInt(fenParsed
.reserve
[1]),
67 [V
.KNIGHT
]: parseInt(fenParsed
.reserve
[2]),
68 [V
.BISHOP
]: parseInt(fenParsed
.reserve
[3]),
69 [V
.QUEEN
]: parseInt(fenParsed
.reserve
[4])
72 [V
.PAWN
]: parseInt(fenParsed
.reserve
[5]),
73 [V
.ROOK
]: parseInt(fenParsed
.reserve
[6]),
74 [V
.KNIGHT
]: parseInt(fenParsed
.reserve
[7]),
75 [V
.BISHOP
]: parseInt(fenParsed
.reserve
[8]),
76 [V
.QUEEN
]: parseInt(fenParsed
.reserve
[9])
82 if (i
>= V
.size
.x
) return i
== V
.size
.x
? "w" : "b";
83 return this.board
[i
][j
].charAt(0);
87 if (i
>= V
.size
.x
) return V
.RESERVE_PIECES
[j
];
88 return this.board
[i
][j
].charAt(1);
91 getReservePpath(index
, color
) {
92 return color
+ V
.RESERVE_PIECES
[index
];
95 static get NON_VIOLENT() {
96 return ['s', 'u', 'o', 'c', 't', 'l'];
99 // Ordering on reserve pieces
100 static get RESERVE_PIECES() {
101 return ChessRules
.PIECES
.concat(V
.NON_VIOLENT
);
104 getReserveMoves([x
, y
]) {
105 const color
= this.turn
;
106 const p
= V
.RESERVE_PIECES
[y
];
107 if (this.reserve
[color
][p
] == 0) return [];
109 const pawnShift
= p
== V
.PAWN
? 1 : 0;
110 for (let i
= pawnShift
; i
< V
.size
.x
- pawnShift
; i
++) {
111 for (let j
= 0; j
< V
.size
.y
; j
++) {
112 if (this.board
[i
][j
] == V
.EMPTY
) {
123 start: { x: x
, y: y
}, //a bit artificial...
133 // TODO: adapt all below:
134 getPotentialMovesFrom([x
, y
]) {
136 // Reserves, outside of board: x == sizeX(+1)
137 return this.getReserveMoves([x
, y
]);
140 return super.getPotentialMovesFrom([x
, y
]);
143 getPotentialPawnMoves([x
, y
]) {
145 let moves
= super.getPotentialPawnMoves([x
, y
]);
146 // Remove pawns on 8th rank ("fallen"):
147 const color
= this.turn
;
148 const lastRank
= (color
== "w" ? 0 : V
.size
.x
- 1);
150 if (m
.appear
[0].x
== lastRank
) m
.appear
.pop();
156 let moves
= super.getAllPotentialMoves();
157 const color
= this.turn
;
158 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
159 moves
= moves
.concat(
160 this.getReserveMoves([V
.size
.x
+ (color
== "w" ? 0 : 1), i
])
163 return this.filterValid(moves
);
167 if (!super.atLeastOneMove()) {
168 // Search one reserve move
169 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
170 let moves
= this.filterValid(
171 this.getReserveMoves([V
.size
.x
+ (this.turn
== "w" ? 0 : 1), i
])
173 if (moves
.length
> 0) return true;
180 canTake([x1
, y1
], [x2
, y2
]) {
181 // Self-captures allowed, except for the king:
182 return this.getPiece(x2
, y2
) != V
.KING
;
188 if (move.vanish
.length
== 2 && move.appear
.length
== 2) return;
189 const color
= this.turn
;
190 if (move.vanish
.length
== 0) this.reserve
[color
][move.appear
[0].p
]--;
191 else if (move.vanish
.length
== 2 && move.vanish
[1].c
== color
)
193 this.reserve
[color
][move.vanish
[1].p
]++;
197 super.postUndo(move);
198 if (move.vanish
.length
== 2 && move.appear
.length
== 2) return;
199 const color
= this.turn
;
200 if (move.vanish
.length
== 0) this.reserve
[color
][move.appear
[0].p
]++;
201 else if (move.vanish
.length
== 2 && move.vanish
[1].c
== color
)
202 this.reserve
[color
][move.vanish
[1].p
]--;
205 static get SEARCH_DEPTH() {
210 let evaluation
= super.evalPosition();
212 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
213 const p
= V
.RESERVE_PIECES
[i
];
214 evaluation
+= this.reserve
["w"][p
] * V
.VALUES
[p
];
215 evaluation
-= this.reserve
["b"][p
] * V
.VALUES
[p
];
221 const finalSquare
= V
.CoordsToSquare(move.end
);
222 if (move.vanish
.length
> 0) {
223 if (move.appear
.length
> 0) {
225 return super.getNotation(move);
227 // Pawn fallen: capturing or not
229 if (move.vanish
.length
== 2)
230 res
+= V
.CoordToColumn(move.start
.y
) + "x";
231 return res
+ finalSquare
;
236 move.appear
[0].p
!= V
.PAWN
? move.appear
[0].p
.toUpperCase() : "";
237 return piece
+ "@" + V
.CoordsToSquare(move.end
);