1 import { ChessRules
, PiPo
, Move
} from "@/base_rules";
2 import { ArrayFun
} from "@/utils/array";
3 import { sample
, shuffle
} from "@/utils/alea";
5 export class DobutsuRules
extends ChessRules
{
11 static get HasFlags() {
15 static get HasEnpassant() {
19 static get Monochrome() {
27 static IsGoodFen(fen
) {
28 if (!ChessRules
.IsGoodFen(fen
)) return false;
29 const fenParsed
= V
.ParseFen(fen
);
31 if (!fenParsed
.reserve
|| !fenParsed
.reserve
.match(/^[0-9]{14,14}$/))
36 static ParseFen(fen
) {
37 const fenParts
= fen
.split(" ");
39 ChessRules
.ParseFen(fen
),
40 { reserve: fenParts
[3] }
44 static get ELEPHANT() {
47 static get GIRAFFE() {
64 getPpath(b
, color
, score
, orientation
) {
65 // 'i' for "inversed":
66 const suffix
= (b
[0] == orientation
? "" : "i");
67 return "Dobutsu/" + b
+ suffix
;
70 getPPpath(m
, orientation
) {
73 m
.appear
[0].c
+ m
.appear
[0].p
,
81 static GenRandInitFen() {
82 return "gke/1p1/1P1/EKG w 0 00000000";
86 return super.getFen() + " " + this.getReserveFen();
90 return super.getFenForRepeat() + "_" + this.getReserveFen();
94 let counts
= new Array(6);
95 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
96 counts
[i
] = this.reserve
["w"][V
.RESERVE_PIECES
[i
]];
97 counts
[3 + i
] = this.reserve
["b"][V
.RESERVE_PIECES
[i
]];
99 return counts
.join("");
102 setOtherVariables(fen
) {
103 super.setOtherVariables(fen
);
104 // Also init reserves (used by the interface to show landable pieces)
106 V
.ParseFen(fen
).reserve
.split("").map(x
=> parseInt(x
, 10));
109 [V
.PAWN
]: reserve
[0],
110 [V
.ELEPHANT
]: reserve
[1],
111 [V
.GIRAFFE
]: reserve
[2]
114 [V
.PAWN
]: reserve
[3],
115 [V
.ELEPHANT
]: reserve
[4],
116 [V
.GIRAFFE
]: reserve
[5]
121 // Goal is to capture the king, easier to not track kings
125 if (i
>= V
.size
.x
) return i
== V
.size
.x
? "w" : "b";
126 return this.board
[i
][j
].charAt(0);
130 if (i
>= V
.size
.x
) return V
.RESERVE_PIECES
[j
];
131 return this.board
[i
][j
].charAt(1);
135 return { x: 4, y: 3};
138 getReservePpath(index
, color
, orientation
) {
140 "Dobutsu/" + color
+ V
.RESERVE_PIECES
[index
] +
141 (color
!= orientation
? 'i' : '')
145 // Ordering on reserve pieces
146 static get RESERVE_PIECES() {
148 // No king, since the goal is to capture it
149 [V
.PAWN
, V
.ELEPHANT
, V
.GIRAFFE
]
153 getReserveMoves([x
, y
]) {
154 const color
= this.turn
;
155 const p
= V
.RESERVE_PIECES
[y
];
156 if (this.reserve
[color
][p
] == 0) return [];
158 for (let i
= 0; i
< V
.size
.x
; i
++) {
159 for (let j
= 0; j
< V
.size
.y
; j
++) {
160 if (this.board
[i
][j
] == V
.EMPTY
) {
171 start: { x: x
, y: y
}, //a bit artificial...
181 getPotentialMovesFrom(sq
) {
182 if (sq
[0] >= V
.size
.x
) {
183 // Reserves, outside of board: x == sizeX(+1)
184 return this.getReserveMoves(sq
);
186 switch (this.getPiece(sq
[0], sq
[1])) {
187 case V
.PAWN: return this.getPotentialPawnMoves(sq
);
188 case V
.HEN: return this.getPotentialHenMoves(sq
);
189 case V
.ELEPHANT: return this.getPotentialElephantMoves(sq
);
190 case V
.GIRAFFE: return this.getPotentialGiraffeMoves(sq
);
191 case V
.KING: return super.getPotentialKingMoves(sq
);
193 return []; //never reached
196 getPotentialPawnMoves([x
, y
]) {
198 const beforeLastRank
= (c
== 'w' ? 1 : 2);
199 const forward
= (c
== 'w' ? -1 : 1);
200 if (!V
.OnBoard(x
+ forward
, y
)) return []; //stuck pawn
202 this.board
[x
+ forward
][y
] == V
.EMPTY
||
203 this.getColor(x
+ forward
, y
) != c
205 const tr
= (x
== beforeLastRank
? { p: V
.HEN
, c: c
} : null);
206 return [super.getBasicMove([x
, y
], [x
+ forward
, y
], tr
)];
210 getPotentialHenMoves(sq
) {
212 const forward
= (c
== 'w' ? -1 : 1);
213 const steps
= V
.steps
[V
.ROOK
].concat([[forward
, 1], [forward
, -1]]);
214 return super.getSlideNJumpMoves(sq
, steps
, 1);
217 getPotentialElephantMoves(sq
) {
218 return super.getSlideNJumpMoves(sq
, V
.steps
[V
.BISHOP
], 1);
221 getPotentialGiraffeMoves(sq
) {
222 return super.getSlideNJumpMoves(sq
, V
.steps
[V
.ROOK
], 1);
226 let moves
= super.getAllPotentialMoves();
227 const color
= this.turn
;
228 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
229 moves
= moves
.concat(
230 this.getReserveMoves([V
.size
.x
+ (color
== "w" ? 0 : 1), i
])
236 // Goal is to capture the king:
247 static MayDecode(piece
) {
248 if (piece
== V
.HEN
) return V
.PAWN
;
253 const color
= move.appear
[0].c
;
254 if (move.vanish
.length
== 0)
255 // Drop unpromoted piece:
256 this.reserve
[color
][move.appear
[0].p
]--;
257 else if (move.vanish
.length
== 2)
258 // May capture a promoted piece:
259 this.reserve
[color
][V
.MayDecode(move.vanish
[1].p
)]++;
263 const color
= this.turn
;
264 if (move.vanish
.length
== 0)
265 this.reserve
[color
][move.appear
[0].p
]++;
266 else if (move.vanish
.length
== 2)
267 this.reserve
[color
][V
.MayDecode(move.vanish
[1].p
)]--;
272 if (this.board
.every(row
=> row
.every(cell
=> cell
!= c
+ 'k')))
273 return (c
== 'w' ? "0-1" : "1-0");
274 const oppCol
= V
.GetOppCol(c
);
275 const oppLastRank
= (c
== 'w' ? 3 : 0);
276 for (let j
=0; j
< V
.size
.y
; j
++) {
277 if (this.board
[oppLastRank
][j
] == oppCol
+ 'k')
278 return (oppCol
== 'w' ? "1-0" : "0-1");
283 static get SEARCH_DEPTH() {
287 static get VALUES() {
288 // NOTE: very arbitrary
299 let evaluation
= super.evalPosition();
301 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
302 const p
= V
.RESERVE_PIECES
[i
];
303 evaluation
+= this.reserve
["w"][p
] * V
.VALUES
[p
];
304 evaluation
-= this.reserve
["b"][p
] * V
.VALUES
[p
];
310 const finalSquare
= V
.CoordsToSquare(move.end
);
311 if (move.vanish
.length
== 0) {
313 const piece
= move.appear
[0].p
.toUpperCase();
314 return (piece
!= 'P' ? piece : "") + "@" + finalSquare
;
316 const piece
= move.vanish
[0].p
.toUpperCase();
318 (piece
!= 'P' || move.vanish
.length
== 2 ? piece : "") +
319 (move.vanish
.length
== 2 ? "x" : "") +
322 move.appear
[0].p
!= move.vanish
[0].p
323 ? "=" + move.appear
[0].p
.toUpperCase()