Better greed

This commit is contained in:
2025-11-06 09:16:57 -08:00
parent eb084e1f07
commit 1c8a2695c4

View File

@@ -1,5 +1,3 @@
// Return a random valid action on the given board.
// Used as a last resort.
fn random_action(board) {
@@ -59,8 +57,8 @@ fn compute_influence(board) {
}
// Sort by increasing absolute score
influence.sort(|a, b| {
// Sort by increasing absolute score
influence.sort(|a, b| {
let a_abs = a[1].abs();
let b_abs = b[1].abs();
@@ -85,7 +83,6 @@ fn place_number(board, minimize) {
return random_action(board);
}
// Get the most influential position
let pos = influence[-1][0];
let val = influence[-1][1];
@@ -104,7 +101,7 @@ fn place_number(board, minimize) {
if val > 0 {
symbol = available_numbers[-1];
} else {
symbol = available_numbers[0];
symbol = available_numbers[0];
}
}
@@ -139,25 +136,20 @@ fn place_op(board, minimize) {
return ();
}
// Main step function (shared between min and max)
fn greed_step(board, minimize) {
let action = place_op(board, minimize);
// We could not place an op, so place a number
if action == () {
action = place_number(board, minimize);
}
if board.can_play(action) {
return action;
}
// Prevent invalid moves, random fallback
if board.can_play(action) { return action; }
return random_action(board);
}
// Minimizer step
fn step_min(board) {
greed_step(board, true)