aboutsummaryrefslogtreecommitdiff
path: root/src/solve.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/solve.rs')
-rw-r--r--src/solve.rs14
1 files changed, 10 insertions, 4 deletions
diff --git a/src/solve.rs b/src/solve.rs
index e9c8c04..b950ab6 100644
--- a/src/solve.rs
+++ b/src/solve.rs
@@ -2,8 +2,14 @@ use std::collections::HashMap;
use crate::position::{Position, WIDTH, HEIGHT};
+const COLUMNS_ORDER: [u64; 7] = [3, 2, 4, 1, 5, 0, 6];
-pub fn solve(p: Position, a: i32, b: i32, cache: &mut HashMap<u64, i32>) -> i32 {
+pub fn solve(p: Position) -> i32 {
+ let mut cache = HashMap::with_capacity(30000);
+ solve_rec(p, -100000, 100000, &mut cache)
+}
+
+fn solve_rec(p: Position, a: i32, b: i32, cache: &mut HashMap<u64, i32>) -> i32 {
if let Some(score) = cache.get(&p.key()) {
return *score;
}
@@ -28,12 +34,13 @@ pub fn solve(p: Position, a: i32, b: i32, cache: &mut HashMap<u64, i32>) -> i32
}
}
- for x in 0..WIDTH {
+ for x_unordered in 0..(WIDTH as usize) {
+ let x = COLUMNS_ORDER[x_unordered];
if !p.is_valid_play(x) {
continue
}
- let score = -solve(p.play(x), -beta, -alpha, cache);
+ let score = -solve_rec(p.play(x), -beta, -alpha, cache);
if score >= beta {
return score;
@@ -47,4 +54,3 @@ pub fn solve(p: Position, a: i32, b: i32, cache: &mut HashMap<u64, i32>) -> i32
return alpha;
}
-