Skip to content

Commit b9b080f

Browse files
committed
Extend Xiangqi tactical search
1 parent 74f797a commit b9b080f

1 file changed

Lines changed: 38 additions & 1 deletion

File tree

scripts/xiangqi_ai.rss

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,42 @@ fn evaluate(side: int) -> int {
199199
score
200200
}
201201

202+
// Extends a leaf through the best legal capture to reduce horizon errors.
203+
fn capture_extension(side: int, stand_pat: int) -> int {
204+
let mut best = stand_pat;
205+
let mut fy = 0;
206+
while fy < 10 {
207+
let mut fx = 0;
208+
while fx < 9 {
209+
if bevy::Xiangqi::cell(fx, fy) * side > 0 {
210+
let mut ty = 0;
211+
while ty < 10 {
212+
let mut tx = 0;
213+
while tx < 9 {
214+
let target: int = bevy::Xiangqi::cell(tx, ty);
215+
if target * side < 0 && legal_after_trial(side, fx, fy, tx, ty) {
216+
let piece: int = bevy::Xiangqi::cell(fx, fy);
217+
bevy::Xiangqi::set_cell(fx, fy, 0);
218+
bevy::Xiangqi::set_cell(tx, ty, piece);
219+
let score = if target == -side => { 900000000 } else => { evaluate(side) };
220+
bevy::Xiangqi::set_cell(fx, fy, piece);
221+
bevy::Xiangqi::set_cell(tx, ty, target);
222+
if score > best {
223+
best = score;
224+
}
225+
}
226+
tx = tx + 1;
227+
}
228+
ty = ty + 1;
229+
}
230+
}
231+
fx = fx + 1;
232+
}
233+
fy = fy + 1;
234+
}
235+
best
236+
}
237+
202238
let opponent: int = -ai_player;
203239
let mut top_fx: [int] = [-1, -1, -1, -1, -1, -1];
204240
let mut top_fy: [int] = [-1, -1, -1, -1, -1, -1];
@@ -304,7 +340,8 @@ while candidate < 6 && top_fx[candidate].copy() >= 0 {
304340
let reply_score = if reply_target == ai_player => {
305341
-900000000
306342
} else => {
307-
evaluate(ai_player)
343+
let stand_pat = evaluate(ai_player);
344+
capture_extension(ai_player, stand_pat)
308345
};
309346
bevy::Xiangqi::set_cell(rfx, rfy, reply_piece);
310347
bevy::Xiangqi::set_cell(rtx, rty, reply_target);

0 commit comments

Comments
 (0)