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
{
7 static get HasFlags() {
11 static get HasEnpassant() {
15 static get Monochrome() {
23 static IsGoodFen(fen
) {
24 if (!ChessRules
.IsGoodFen(fen
)) return false;
25 const fenParsed
= V
.ParseFen(fen
);
27 if (!fenParsed
.reserve
|| !fenParsed
.reserve
.match(/^[0-9]{14,14}$/))
32 static ParseFen(fen
) {
33 const fenParts
= fen
.split(" ");
35 ChessRules
.ParseFen(fen
),
36 { reserve: fenParts
[3] }
40 static get ELEPHANT() {
43 static get GIRAFFE() {
60 getPpath(b
, color
, score
, orientation
) {
61 // 'i' for "inversed":
62 const suffix
= (b
[0] == orientation
? "" : "i");
63 return "Dobutsu/" + b
+ suffix
;
66 getPPpath(m
, orientation
) {
69 m
.appear
[0].c
+ m
.appear
[0].p
,
77 static GenRandInitFen() {
78 return "gke/1p1/1P1/EKG w 0 00000000";
82 return super.getFen() + " " + this.getReserveFen();
86 return super.getFenForRepeat() + "_" + this.getReserveFen();
90 let counts
= new Array(6);
91 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
92 counts
[i
] = this.reserve
["w"][V
.RESERVE_PIECES
[i
]];
93 counts
[3 + i
] = this.reserve
["b"][V
.RESERVE_PIECES
[i
]];
95 return counts
.join("");
98 setOtherVariables(fen
) {
99 super.setOtherVariables(fen
);
100 // Also init reserves (used by the interface to show landable pieces)
102 V
.ParseFen(fen
).reserve
.split("").map(x
=> parseInt(x
, 10));
105 [V
.PAWN
]: reserve
[0],
106 [V
.ELEPHANT
]: reserve
[1],
107 [V
.GIRAFFE
]: reserve
[2]
110 [V
.PAWN
]: reserve
[3],
111 [V
.ELEPHANT
]: reserve
[4],
112 [V
.GIRAFFE
]: reserve
[5]
117 // Goal is to capture the king, easier to not track kings
121 if (i
>= V
.size
.x
) return i
== V
.size
.x
? "w" : "b";
122 return this.board
[i
][j
].charAt(0);
126 if (i
>= V
.size
.x
) return V
.RESERVE_PIECES
[j
];
127 return this.board
[i
][j
].charAt(1);
131 return { x: 4, y: 3};
134 getReservePpath(index
, color
, orientation
) {
136 "Dobutsu/" + color
+ V
.RESERVE_PIECES
[index
] +
137 (color
!= orientation
? 'i' : '')
141 // Ordering on reserve pieces
142 static get RESERVE_PIECES() {
144 // No king, since the goal is to capture it
145 [V
.PAWN
, V
.ELEPHANT
, V
.GIRAFFE
]
149 getReserveMoves([x
, y
]) {
150 const color
= this.turn
;
151 const p
= V
.RESERVE_PIECES
[y
];
152 if (this.reserve
[color
][p
] == 0) return [];
154 for (let i
= 0; i
< V
.size
.x
; i
++) {
155 for (let j
= 0; j
< V
.size
.y
; j
++) {
156 if (this.board
[i
][j
] == V
.EMPTY
) {
167 start: { x: x
, y: y
}, //a bit artificial...
177 getPotentialMovesFrom(sq
) {
178 if (sq
[0] >= V
.size
.x
) {
179 // Reserves, outside of board: x == sizeX(+1)
180 return this.getReserveMoves(sq
);
182 switch (this.getPiece(sq
[0], sq
[1])) {
183 case V
.PAWN: return this.getPotentialPawnMoves(sq
);
184 case V
.HEN: return this.getPotentialHenMoves(sq
);
185 case V
.ELEPHANT: return this.getPotentialElephantMoves(sq
);
186 case V
.GIRAFFE: return this.getPotentialGiraffeMoves(sq
);
187 case V
.KING: return super.getPotentialKingMoves(sq
);
189 return []; //never reached
192 getPotentialPawnMoves([x
, y
]) {
194 const beforeLastRank
= (c
== 'w' ? 1 : 2);
195 const forward
= (c
== 'w' ? -1 : 1);
196 if (!V
.OnBoard(x
+ forward
, y
)) return []; //stuck pawn
198 this.board
[x
+ forward
][y
] == V
.EMPTY
||
199 this.getColor(x
+ forward
, y
) != c
201 const tr
= (x
== beforeLastRank
? { p: V
.HEN
, c: c
} : null);
202 return [super.getBasicMove([x
, y
], [x
+ forward
, y
], tr
)];
206 getPotentialHenMoves(sq
) {
208 const forward
= (c
== 'w' ? -1 : 1);
209 const steps
= V
.steps
[V
.ROOK
].concat([[forward
, 1], [forward
, -1]]);
210 return super.getSlideNJumpMoves(sq
, steps
, "oneStep");
213 getPotentialElephantMoves(sq
) {
214 return super.getSlideNJumpMoves(sq
, V
.steps
[V
.BISHOP
], "oneStep");
217 getPotentialGiraffeMoves(sq
) {
218 return super.getSlideNJumpMoves(sq
, V
.steps
[V
.ROOK
], "oneStep");
222 let moves
= super.getAllPotentialMoves();
223 const color
= this.turn
;
224 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
225 moves
= moves
.concat(
226 this.getReserveMoves([V
.size
.x
+ (color
== "w" ? 0 : 1), i
])
232 // Goal is to capture the king:
243 static MayDecode(piece
) {
244 if (piece
== V
.HEN
) return V
.PAWN
;
249 const color
= move.appear
[0].c
;
250 if (move.vanish
.length
== 0)
251 // Drop unpromoted piece:
252 this.reserve
[color
][move.appear
[0].p
]--;
253 else if (move.vanish
.length
== 2)
254 // May capture a promoted piece:
255 this.reserve
[color
][V
.MayDecode(move.vanish
[1].p
)]++;
259 const color
= this.turn
;
260 if (move.vanish
.length
== 0)
261 this.reserve
[color
][move.appear
[0].p
]++;
262 else if (move.vanish
.length
== 2)
263 this.reserve
[color
][V
.MayDecode(move.vanish
[1].p
)]--;
268 if (this.board
.every(row
=> row
.every(cell
=> cell
!= c
+ 'k')))
269 return (c
== 'w' ? "0-1" : "1-0");
270 const oppCol
= V
.GetOppCol(c
);
271 const oppLastRank
= (c
== 'w' ? 3 : 0);
272 for (let j
=0; j
< V
.size
.y
; j
++) {
273 if (this.board
[oppLastRank
][j
] == oppCol
+ 'k')
274 return (oppCol
== 'w' ? "1-0" : "0-1");
279 static get SEARCH_DEPTH() {
283 static get VALUES() {
284 // NOTE: very arbitrary
295 let evaluation
= super.evalPosition();
297 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
298 const p
= V
.RESERVE_PIECES
[i
];
299 evaluation
+= this.reserve
["w"][p
] * V
.VALUES
[p
];
300 evaluation
-= this.reserve
["b"][p
] * V
.VALUES
[p
];
306 const finalSquare
= V
.CoordsToSquare(move.end
);
307 if (move.vanish
.length
== 0) {
309 const piece
= move.appear
[0].p
.toUpperCase();
310 return (piece
!= 'P' ? piece : "") + "@" + finalSquare
;
312 const piece
= move.vanish
[0].p
.toUpperCase();
314 (piece
!= 'P' || move.vanish
.length
== 2 ? piece : "") +
315 (move.vanish
.length
== 2 ? "x" : "") +
318 move.appear
[0].p
!= move.vanish
[0].p
319 ? "=" + move.appear
[0].p
.toUpperCase()