From e160e60867c75d9f605d0cd1c9f359925388a302 Mon Sep 17 00:00:00 2001 From: Gleb Koval Date: Sun, 7 Aug 2022 19:29:37 +0000 Subject: [PATCH] Added predict to o-x-rust --- wasm/o-x-rust/Cargo.lock | 2 +- wasm/o-x-rust/Cargo.toml | 2 +- wasm/o-x-rust/src/lib.rs | 22 ++++++++++++++++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/wasm/o-x-rust/Cargo.lock b/wasm/o-x-rust/Cargo.lock index 8eecd9c..d3fcb2e 100644 --- a/wasm/o-x-rust/Cargo.lock +++ b/wasm/o-x-rust/Cargo.lock @@ -25,7 +25,7 @@ dependencies = [ [[package]] name = "o-x-rust" -version = "0.0.3" +version = "0.0.4" dependencies = [ "wasm-bindgen", ] diff --git a/wasm/o-x-rust/Cargo.toml b/wasm/o-x-rust/Cargo.toml index 6ce2416..f5ed655 100644 --- a/wasm/o-x-rust/Cargo.toml +++ b/wasm/o-x-rust/Cargo.toml @@ -4,7 +4,7 @@ description = "Naughts and crosses WASM algorithms" repository = "https://git.koval.net/cyclane/game-algorithms/src/branch/main/wasm/o-x-rust" license = "GNU GPLv3" readme = "README.md" -version = "0.0.3" +version = "0.0.4" edition = "2021" [lib] diff --git a/wasm/o-x-rust/src/lib.rs b/wasm/o-x-rust/src/lib.rs index e6a9a84..d6ff8bb 100644 --- a/wasm/o-x-rust/src/lib.rs +++ b/wasm/o-x-rust/src/lib.rs @@ -56,3 +56,25 @@ pub fn get_score(me: u8, other: u8, first: bool, board: &[u8]) -> i32 { } score } + +#[wasm_bindgen] +pub fn predict(me: u8, other: u8, first: bool, board: &[u8]) -> usize { + let (mut max_p, mut max_s) = (0, i32::MIN); + let empty = count_empty(board); + if empty == 0 { + return 0; + } + for i in 0..9 { + if board[i] != 0 { + continue; + } + let copy = &mut [0u8; 9]; + copy.copy_from_slice(board); + copy[i] = get_turn(me, other, first, empty); + let score = get_score(me, other, first, board); + if score > max_s { + (max_p, max_s) = (i, score); + } + } + max_p +}