Added max depth
This commit is contained in:
parent
677221603c
commit
ca29e826f1
|
@ -16,7 +16,7 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "connect-four-rust"
|
name = "connect-four-rust"
|
||||||
version = "0.0.1"
|
version = "0.0.2"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"wasm-bindgen",
|
"wasm-bindgen",
|
||||||
"wasm-bindgen-test",
|
"wasm-bindgen-test",
|
||||||
|
|
|
@ -4,7 +4,7 @@ description = "Connect four WASM algorithms"
|
||||||
repository = "https://git.koval.net/cyclane/game-algorithms/src/branch/main/wasm/connect-four-rust"
|
repository = "https://git.koval.net/cyclane/game-algorithms/src/branch/main/wasm/connect-four-rust"
|
||||||
license = "GNU GPLv3"
|
license = "GNU GPLv3"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
version = "0.0.1"
|
version = "0.0.2"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[lib]
|
[lib]
|
||||||
|
|
|
@ -103,24 +103,24 @@ pub fn get_turn(me: u8, other: u8, first: bool, empty: usize) -> u8 {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[wasm_bindgen]
|
#[wasm_bindgen]
|
||||||
pub fn mm_get_score(me: u8, other: u8, first: bool, w: usize, board: &[u8], min: usize) -> f64 {
|
pub fn mm_get_score(me: u8, other: u8, first: bool, w: usize, board: &[u8], min: usize, md: f64) -> f64 {
|
||||||
mm_sub_get_score(me, other, first, w, &board.to_vec(), min, board.len() as f64 + 1.0, 0.0, 0.0)
|
mm_sub_get_score(me, other, first, w, &board.to_vec(), min, board.len() as f64 + 1.0, 0.0, 0.0, md)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[wasm_bindgen]
|
#[wasm_bindgen]
|
||||||
pub fn mm_d_get_score(me: u8, other: u8, first: bool, w: usize, board: &[u8], min: usize) -> f64 {
|
pub fn mm_d_get_score(me: u8, other: u8, first: bool, w: usize, board: &[u8], min: usize, md: f64) -> f64 {
|
||||||
mm_sub_get_score(me, other, first, w, &board.to_vec(), min, board.len() as f64 + 1.0, 0.0, 1.0)
|
mm_sub_get_score(me, other, first, w, &board.to_vec(), min, board.len() as f64 + 1.0, 0.0, 1.0, md)
|
||||||
}
|
}
|
||||||
|
|
||||||
// m = score (without depth) multiplier
|
// m = score (without depth) multiplier
|
||||||
// dm = depth multiplier
|
// dm = depth multiplier
|
||||||
pub fn mm_sub_get_score(me: u8, other: u8, first: bool, w: usize, board: &Vec<u8>, min: usize, m: f64, depth: f64, dm: f64) -> f64 {
|
pub fn mm_sub_get_score(me: u8, other: u8, first: bool, w: usize, board: &Vec<u8>, min: usize, m: f64, depth: f64, dm: f64, md: f64) -> f64 {
|
||||||
let winner = find_winner(w, board, min);
|
let winner = find_winner(w, board, min);
|
||||||
if winner != 0 {
|
if winner != 0 {
|
||||||
return if winner == me { m - depth*dm } else { -m + depth*dm };
|
return if winner == me { m - depth*dm } else { -m + depth*dm };
|
||||||
}
|
}
|
||||||
let empty = count_empty(board);
|
let empty = count_empty(board);
|
||||||
if empty == 0 {
|
if empty == 0 || depth > md {
|
||||||
return 0.0;
|
return 0.0;
|
||||||
}
|
}
|
||||||
let turn = get_turn(me, other, first, empty);
|
let turn = get_turn(me, other, first, empty);
|
||||||
|
@ -138,7 +138,7 @@ pub fn mm_sub_get_score(me: u8, other: u8, first: bool, w: usize, board: &Vec<u8
|
||||||
if let Some(y) = r {
|
if let Some(y) = r {
|
||||||
let copy = &mut board.clone();
|
let copy = &mut board.clone();
|
||||||
copy[get_idx(w, x, y)] = get_turn(me, other, first, empty);
|
copy[get_idx(w, x, y)] = get_turn(me, other, first, empty);
|
||||||
let value = mm_sub_get_score(me, other, first, w, copy, min, m, depth + 1.0, dm);
|
let value = mm_sub_get_score(me, other, first, w, copy, min, m, depth + 1.0, dm, md);
|
||||||
best = if turn == me { best.max(value) } else { best.min(value) };
|
best = if turn == me { best.max(value) } else { best.min(value) };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -149,10 +149,10 @@ pub fn mm_sub_get_score(me: u8, other: u8, first: bool, w: usize, board: &Vec<u8
|
||||||
// mm: minimax algorithm
|
// mm: minimax algorithm
|
||||||
// mm+d: minmax algorithm with depth
|
// mm+d: minmax algorithm with depth
|
||||||
#[wasm_bindgen]
|
#[wasm_bindgen]
|
||||||
pub fn get_score(me: u8, other: u8, first: bool, w: usize, board: &[u8], min: usize, algorithm: &str) -> f64 {
|
pub fn get_score(me: u8, other: u8, first: bool, w: usize, board: &[u8], min: usize, md: f64, algorithm: &str) -> f64 {
|
||||||
match algorithm {
|
match algorithm {
|
||||||
"mm" => mm_get_score(me, other, first, w, board, min),
|
"mm" => mm_get_score(me, other, first, w, board, min, md),
|
||||||
"mm+d" => mm_d_get_score(me, other, first, w, board, min),
|
"mm+d" => mm_d_get_score(me, other, first, w, board, min, md),
|
||||||
_ => 0.0
|
_ => 0.0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -161,7 +161,7 @@ pub fn get_score(me: u8, other: u8, first: bool, w: usize, board: &[u8], min: us
|
||||||
// mm: minimax algorithm
|
// mm: minimax algorithm
|
||||||
// mm+d: minmax algorithm with depth
|
// mm+d: minmax algorithm with depth
|
||||||
#[wasm_bindgen]
|
#[wasm_bindgen]
|
||||||
pub fn predict(me: u8, other: u8, first: bool, w: usize, board: &[u8], min: usize, algorithm: &str) -> usize {
|
pub fn predict(me: u8, other: u8, first: bool, w: usize, board: &[u8], min: usize, md: f64, algorithm: &str) -> usize {
|
||||||
let (mut max_p, mut max_s) = (0, f64::MIN);
|
let (mut max_p, mut max_s) = (0, f64::MIN);
|
||||||
let empty = count_empty(board);
|
let empty = count_empty(board);
|
||||||
if empty == 0 {
|
if empty == 0 {
|
||||||
|
@ -181,7 +181,7 @@ pub fn predict(me: u8, other: u8, first: bool, w: usize, board: &[u8], min: usiz
|
||||||
if let Some(y) = r {
|
if let Some(y) = r {
|
||||||
let copy = &mut vec_board.clone();
|
let copy = &mut vec_board.clone();
|
||||||
copy[get_idx(w, x, y)] = get_turn(me, other, first, empty);
|
copy[get_idx(w, x, y)] = get_turn(me, other, first, empty);
|
||||||
let score = get_score(me, other, first, w, copy, min, algorithm);
|
let score = get_score(me, other, first, w, copy, min, md, algorithm);
|
||||||
if score > max_s {
|
if score > max_s {
|
||||||
(max_p, max_s) = (x, score);
|
(max_p, max_s) = (x, score);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue