aboutsummaryrefslogtreecommitdiff
path: root/src/solve.rs
diff options
context:
space:
mode:
authorCharles Cabergs <me@cacharle.xyz>2021-02-02 13:18:39 +0100
committerCharles Cabergs <me@cacharle.xyz>2021-02-02 13:18:39 +0100
commit15efc494b99492df189389474bb981d0ec5ce2b3 (patch)
tree69fd81b49f0c3f52fd8aa90e3194b06011b68317 /src/solve.rs
parent5aa6abf0166868f13f9ab64c91f3f4b62e05ed58 (diff)
downloadconnect4-15efc494b99492df189389474bb981d0ec5ce2b3.tar.gz
connect4-15efc494b99492df189389474bb981d0ec5ce2b3.tar.bz2
connect4-15efc494b99492df189389474bb981d0ec5ce2b3.zip
Solving bad cache value, Added weak solver
Diffstat (limited to 'src/solve.rs')
-rw-r--r--src/solve.rs61
1 files changed, 36 insertions, 25 deletions
diff --git a/src/solve.rs b/src/solve.rs
index b950ab6..9561ff1 100644
--- a/src/solve.rs
+++ b/src/solve.rs
@@ -1,18 +1,22 @@
use std::collections::HashMap;
-use crate::position::{Position, WIDTH, HEIGHT};
+use crate::position::{Position, WIDTH, HEIGHT, MIN_SCORE};
const COLUMNS_ORDER: [u64; 7] = [3, 2, 4, 1, 5, 0, 6];
pub fn solve(p: Position) -> i32 {
let mut cache = HashMap::with_capacity(30000);
- solve_rec(p, -100000, 100000, &mut cache)
+ solve_rec(p, -1000, 1000, &mut cache)
+}
+
+// the weak solver only tells if the position is a win/lose/draw
+// it's faster but less precise
+pub fn solve_weak(p: Position) -> i32 {
+ let mut cache = HashMap::with_capacity(30000);
+ solve_rec(p, -1, 1, &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;
- }
if p.is_draw() {
return 0;
}
@@ -25,32 +29,39 @@ fn solve_rec(p: Position, a: i32, b: i32, cache: &mut HashMap<u64, i32>) -> i32
let mut alpha = a;
let mut beta = b;
- let max = (((WIDTH * HEIGHT + 1) as i32) - (p.play_count as i32)) / 2;
-
- if beta > max {
- beta = max;
- if alpha >= beta {
- return beta;
+ if let Some(max_score) = cache.get(&p.key()) {
+ // can't return max_score directly
+ // because the alpha-beta context in the cache may be
+ // different than the current alpha-beta
+ if beta > *max_score {
+ beta = *max_score;
+ if alpha >= beta {
+ return beta;
+ }
}
}
- for x_unordered in 0..(WIDTH as usize) {
- let x = COLUMNS_ORDER[x_unordered];
- if !p.is_valid_play(x) {
- continue
- }
-
+ let mut best = MIN_SCORE;
+ for x in (0..(WIDTH as usize))
+ .map(|x| COLUMNS_ORDER[x])
+ .filter(|x| p.is_valid_play(*x))
+ {
+ // using negamax, variante of minimax where:
+ // max(player1, player2) == -min(-player1, -player2)
let score = -solve_rec(p.play(x), -beta, -alpha, cache);
-
- if score >= beta {
- return score;
+ if score > best {
+ best = score;
}
-
- if score > alpha {
- alpha = score;
+ // reduce alpha-beta range if found better score
+ if best > alpha {
+ alpha = best;
+ }
+ // impossible alpha-beta range reached (alpha is supposed to be < to beta)
+ if alpha >= beta {
+ return score;
}
}
- cache.insert(p.key(), alpha);
- return alpha;
+ cache.insert(p.key(), best);
+ return best;
}