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() {
19 static IsGoodFen(fen
) {
20 if (!ChessRules
.IsGoodFen(fen
)) return false;
21 const fenParsed
= V
.ParseFen(fen
);
23 if (!fenParsed
.reserve
|| !fenParsed
.reserve
.match(/^[0-9]{14,14}$/))
28 static ParseFen(fen
) {
29 const fenParts
= fen
.split(" ");
31 ChessRules
.ParseFen(fen
),
32 { reserve: fenParts
[3] }
36 static get ELEPHANT() {
39 static get GIRAFFE() {
56 getPpath(b
, color
, score
, orientation
) {
57 // 'i' for "inversed":
58 const suffix
= (b
[0] == orientation
? "" : "i");
59 return "Dobutsu/" + b
+ suffix
;
62 getPPpath(m
, orientation
) {
65 m
.appear
[0].c
+ m
.appear
[0].p
,
73 static GenRandInitFen() {
74 return "gke/1p1/1P1/EKG w 0 00000000";
78 return super.getFen() + " " + this.getReserveFen();
82 return super.getFenForRepeat() + "_" + this.getReserveFen();
86 let counts
= new Array(6);
87 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
88 counts
[i
] = this.reserve
["w"][V
.RESERVE_PIECES
[i
]];
89 counts
[3 + i
] = this.reserve
["b"][V
.RESERVE_PIECES
[i
]];
91 return counts
.join("");
94 setOtherVariables(fen
) {
95 super.setOtherVariables(fen
);
96 // Also init reserves (used by the interface to show landable pieces)
98 V
.ParseFen(fen
).reserve
.split("").map(x
=> parseInt(x
, 10));
101 [V
.PAWN
]: reserve
[0],
102 [V
.ELEPHANT
]: reserve
[1],
103 [V
.GIRAFFE
]: reserve
[2]
106 [V
.PAWN
]: reserve
[3],
107 [V
.ELEPHANT
]: reserve
[4],
108 [V
.GIRAFFE
]: reserve
[5]
113 // Goal is to capture the king, easier to not track kings
117 if (i
>= V
.size
.x
) return i
== V
.size
.x
? "w" : "b";
118 return this.board
[i
][j
].charAt(0);
122 if (i
>= V
.size
.x
) return V
.RESERVE_PIECES
[j
];
123 return this.board
[i
][j
].charAt(1);
127 return { x: 4, y: 3};
130 getReservePpath(index
, color
, orientation
) {
132 "Dobutsu/" + color
+ V
.RESERVE_PIECES
[index
] +
133 (color
!= orientation
? 'i' : '')
137 // Ordering on reserve pieces
138 static get RESERVE_PIECES() {
140 // No king, since the goal is to capture it
141 [V
.PAWN
, V
.ELEPHANT
, V
.GIRAFFE
]
145 getReserveMoves([x
, y
]) {
146 const color
= this.turn
;
147 const p
= V
.RESERVE_PIECES
[y
];
148 if (this.reserve
[color
][p
] == 0) return [];
150 for (let i
= 0; i
< V
.size
.x
; i
++) {
151 for (let j
= 0; j
< V
.size
.y
; j
++) {
152 if (this.board
[i
][j
] == V
.EMPTY
) {
163 start: { x: x
, y: y
}, //a bit artificial...
173 getPotentialMovesFrom(sq
) {
174 if (sq
[0] >= V
.size
.x
) {
175 // Reserves, outside of board: x == sizeX(+1)
176 return this.getReserveMoves(sq
);
178 switch (this.getPiece(sq
[0], sq
[1])) {
179 case V
.PAWN: return this.getPotentialPawnMoves(sq
);
180 case V
.ELEPHANT: return this.getPotentialElephantMoves(sq
);
181 case V
.GIRAFFE: return this.getPotentialGiraffeMoves(sq
);
182 case V
.KING: return super.getPotentialKingMoves(sq
);
184 return []; //never reached
187 getPotentialPawnMoves([x
, y
]) {
189 const beforeLastRank
= (c
== 'w' ? 1 : 2);
190 const forward
= (c
== 'w' ? -1 : 1);
191 if (!V
.OnBoard(x
+ forward
, y
)) return []; //stuck pawn
193 this.board
[x
+ forward
][y
] == V
.EMPTY
||
194 this.getColor(x
+ forward
, y
) != c
196 const tr
= (x
== beforeLastRank
? { p: V
.HEN
, c: c
} : null);
197 return [super.getBasicMove([x
, y
], [x
+ forward
, y
], tr
)];
201 getPotentialElephantMoves(sq
) {
202 return super.getSlideNJumpMoves(sq
, V
.steps
[V
.BISHOP
], "oneStep");
205 getPotentialGiraffeMoves(sq
) {
206 return super.getSlideNJumpMoves(sq
, V
.steps
[V
.ROOK
], "oneStep");
210 let moves
= super.getAllPotentialMoves();
211 const color
= this.turn
;
212 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
213 moves
= moves
.concat(
214 this.getReserveMoves([V
.size
.x
+ (color
== "w" ? 0 : 1), i
])
220 // Goal is to capture the king:
231 static MayDecode(piece
) {
232 if (piece
== V
.HEN
) return V
.PAWN
;
237 const color
= move.appear
[0].c
;
238 if (move.vanish
.length
== 0)
239 // Drop unpromoted piece:
240 this.reserve
[color
][move.appear
[0].p
]--;
241 else if (move.vanish
.length
== 2)
242 // May capture a promoted piece:
243 this.reserve
[color
][V
.MayDecode(move.vanish
[1].p
)]++;
247 const color
= this.turn
;
248 if (move.vanish
.length
== 0)
249 this.reserve
[color
][move.appear
[0].p
]++;
250 else if (move.vanish
.length
== 2)
251 this.reserve
[color
][V
.MayDecode(move.vanish
[1].p
)]--;
256 if (this.board
.every(row
=> row
.every(cell
=> cell
!= c
+ 'k')))
257 return (c
== 'w' ? "0-1" : "1-0");
258 const oppCol
= V
.GetOppCol(c
);
259 const oppLastRank
= (c
== 'w' ? 3 : 0);
260 for (let j
=0; j
< V
.size
.y
; j
++) {
261 if (this.board
[oppLastRank
][j
] == oppCol
+ 'k')
262 return (oppCol
== 'w' ? "1-0" : "0-1");
267 static get SEARCH_DEPTH() {
271 static get VALUES() {
272 // NOTE: very arbitrary
283 let evaluation
= super.evalPosition();
285 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
286 const p
= V
.RESERVE_PIECES
[i
];
287 evaluation
+= this.reserve
["w"][p
] * V
.VALUES
[p
];
288 evaluation
-= this.reserve
["b"][p
] * V
.VALUES
[p
];
294 const finalSquare
= V
.CoordsToSquare(move.end
);
295 if (move.vanish
.length
== 0) {
297 const piece
= move.appear
[0].p
.toUpperCase();
298 return (piece
!= 'P' ? piece : "") + "@" + finalSquare
;
300 const piece
= move.vanish
[0].p
.toUpperCase();
302 (piece
!= 'P' || move.vanish
.length
== 2 ? piece : "") +
303 (move.vanish
.length
== 2 ? "x" : "") +
306 move.appear
[0].p
!= move.vanish
[0].p
307 ? "=" + move.appear
[0].p
.toUpperCase()