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