From ca29e826f11c063632efc25b8f4cf0ba4854a06f Mon Sep 17 00:00:00 2001 From: Gleb Koval Date: Sat, 13 Aug 2022 15:27:38 +0000 Subject: [PATCH] Added max depth --- wasm/connect-four-rust/Cargo.lock | 2 +- wasm/connect-four-rust/Cargo.toml | 2 +- wasm/connect-four-rust/src/lib.rs | 24 ++++++++++++------------ 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/wasm/connect-four-rust/Cargo.lock b/wasm/connect-four-rust/Cargo.lock index e7a0879..208b0f7 100644 --- a/wasm/connect-four-rust/Cargo.lock +++ b/wasm/connect-four-rust/Cargo.lock @@ -16,7 +16,7 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "connect-four-rust" -version = "0.0.1" +version = "0.0.2" dependencies = [ "wasm-bindgen", "wasm-bindgen-test", diff --git a/wasm/connect-four-rust/Cargo.toml b/wasm/connect-four-rust/Cargo.toml index 9fb0585..3f01939 100644 --- a/wasm/connect-four-rust/Cargo.toml +++ b/wasm/connect-four-rust/Cargo.toml @@ -4,7 +4,7 @@ description = "Connect four WASM algorithms" repository = "https://git.koval.net/cyclane/game-algorithms/src/branch/main/wasm/connect-four-rust" license = "GNU GPLv3" readme = "README.md" -version = "0.0.1" +version = "0.0.2" edition = "2021" [lib] diff --git a/wasm/connect-four-rust/src/lib.rs b/wasm/connect-four-rust/src/lib.rs index 55f9c1c..3652cb0 100644 --- a/wasm/connect-four-rust/src/lib.rs +++ b/wasm/connect-four-rust/src/lib.rs @@ -103,24 +103,24 @@ pub fn get_turn(me: u8, other: u8, first: bool, empty: usize) -> u8 { } #[wasm_bindgen] -pub fn mm_get_score(me: u8, other: u8, first: bool, w: usize, board: &[u8], min: usize) -> f64 { - mm_sub_get_score(me, other, first, w, &board.to_vec(), min, board.len() as f64 + 1.0, 0.0, 0.0) +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, md) } #[wasm_bindgen] -pub fn mm_d_get_score(me: u8, other: u8, first: bool, w: usize, board: &[u8], min: usize) -> f64 { - mm_sub_get_score(me, other, first, w, &board.to_vec(), min, board.len() as f64 + 1.0, 0.0, 1.0) +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, md) } // m = score (without depth) multiplier // dm = depth multiplier -pub fn mm_sub_get_score(me: u8, other: u8, first: bool, w: usize, board: &Vec, 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, min: usize, m: f64, depth: f64, dm: f64, md: f64) -> f64 { let winner = find_winner(w, board, min); if winner != 0 { return if winner == me { m - depth*dm } else { -m + depth*dm }; } let empty = count_empty(board); - if empty == 0 { + if empty == 0 || depth > md { return 0.0; } 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 f64 { +pub fn get_score(me: u8, other: u8, first: bool, w: usize, board: &[u8], min: usize, md: f64, algorithm: &str) -> f64 { match algorithm { - "mm" => mm_get_score(me, other, first, w, board, min), - "mm+d" => mm_d_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, md), _ => 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+d: minmax algorithm with depth #[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 empty = count_empty(board); 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 { let copy = &mut vec_board.clone(); 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 { (max_p, max_s) = (x, score); }