Added max depth
ci/woodpecker/push/tools-wasm-pack-plugin Pipeline was successful Details
ci/woodpecker/push/wasm-o-x-rust Pipeline was successful Details
ci/woodpecker/push/wasm-connect-four-rust Pipeline was successful Details
ci/woodpecker/push/frontend Pipeline was successful Details

This commit is contained in:
Gleb Koval 2022-08-13 15:27:38 +00:00
parent 677221603c
commit ca29e826f1
Signed by: cyclane
GPG Key ID: 15E168A8B332382C
3 changed files with 14 additions and 14 deletions

View File

@ -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",

View File

@ -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]

View File

@ -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<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);
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<u8
if let Some(y) = r {
let copy = &mut board.clone();
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) };
}
}
@ -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+d: minmax algorithm with depth
#[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 {
"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);
}