o-x-rust intial complete
This commit is contained in:
parent
b2b2e149a8
commit
49816e8659
|
@ -5,21 +5,30 @@ pipeline:
|
|||
settings:
|
||||
actions: build
|
||||
folder: wasm/o-x-rust
|
||||
target: web
|
||||
scope: game-algorithms
|
||||
when:
|
||||
branch:
|
||||
exclude: [main]
|
||||
path:
|
||||
include:
|
||||
- wasm/o-x-rust/*
|
||||
- .woodpecker/wasm-o-x-rust.yml
|
||||
build and publish:
|
||||
test:
|
||||
image: git.koval.net/cyclane/game-algorithms/wasm-pack-plugin
|
||||
pull: true
|
||||
settings:
|
||||
actions: build,publish
|
||||
actions: test
|
||||
folder: wasm/o-x-rust
|
||||
when:
|
||||
path:
|
||||
include:
|
||||
- wasm/o-x-rust/*
|
||||
- .woodpecker/wasm-o-x-rust.yml
|
||||
publish:
|
||||
image: git.koval.net/cyclane/game-algorithms/wasm-pack-plugin
|
||||
pull: true
|
||||
settings:
|
||||
actions: publish
|
||||
folder: wasm/o-x-rust
|
||||
target: web
|
||||
scope: game-algorithms
|
||||
username: cyclane
|
||||
token:
|
||||
|
|
|
@ -17,6 +17,9 @@ When using this plugin it is best to pull every time since it can be updated oft
|
|||
- `target` (default=`bundler`): Target to build for
|
||||
- `make`: Optionally run a make target building
|
||||
|
||||
### Test
|
||||
(No options)
|
||||
|
||||
### Publish
|
||||
|
||||
- `username` (required): NPM registry username
|
||||
|
|
|
@ -2,9 +2,10 @@
|
|||
|
||||
# Properties: actions
|
||||
# username, token, registry=https://registry.npmjs.org, scope, folder=.
|
||||
# profile=release, target=bundler, make
|
||||
# profile=release, target=bundler, make, browser
|
||||
|
||||
# publish: username, token
|
||||
# test:
|
||||
# build:
|
||||
|
||||
echo wasm-pack plugin
|
||||
|
@ -34,6 +35,10 @@ if [[ "${PLUGIN_ACTIONS}" =~ '(^|,)build(,|$)' ]]; then
|
|||
fi
|
||||
fi
|
||||
|
||||
if [[ "${PLUGIN_ACTTIONS}" =~ '(^|,)test(,|$)' ]]; then
|
||||
wasm-pack test --node
|
||||
fi
|
||||
|
||||
if [[ "${PLUGIN_ACTIONS}" =~ '(^|,)publish(,|$)' ]]; then
|
||||
cd pkg
|
||||
npm pkg set type=module
|
||||
|
|
|
@ -14,6 +14,25 @@ version = "1.0.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||
|
||||
[[package]]
|
||||
name = "console_error_panic_hook"
|
||||
version = "0.1.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a06aeb73f470f66dcdbf7223caeebb85984942f22f1adb2a088cf9668146bbbc"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"wasm-bindgen",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "js-sys"
|
||||
version = "0.3.59"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "258451ab10b34f8af53416d1fdab72c22e805f0c92a1136d59470ec0b11138b2"
|
||||
dependencies = [
|
||||
"wasm-bindgen",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "log"
|
||||
version = "0.4.17"
|
||||
|
@ -25,9 +44,10 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "o-x-rust"
|
||||
version = "0.0.9"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"wasm-bindgen",
|
||||
"wasm-bindgen-test",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -54,6 +74,12 @@ dependencies = [
|
|||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "scoped-tls"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ea6a9290e3c9cf0f18145ef7ffa62d68ee0bf5fcd651017e586dc7fd5da448c2"
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "1.0.99"
|
||||
|
@ -96,6 +122,18 @@ dependencies = [
|
|||
"wasm-bindgen-shared",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-futures"
|
||||
version = "0.4.32"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fa76fb221a1f8acddf5b54ace85912606980ad661ac7a503b4570ffd3a624dad"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"js-sys",
|
||||
"wasm-bindgen",
|
||||
"web-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-macro"
|
||||
version = "0.2.82"
|
||||
|
@ -124,3 +162,37 @@ name = "wasm-bindgen-shared"
|
|||
version = "0.2.82"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6598dd0bd3c7d51095ff6531a5b23e02acdc81804e30d8f07afb77b7215a140a"
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-test"
|
||||
version = "0.3.32"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "513df541345bb9fcc07417775f3d51bbb677daf307d8035c0afafd87dc2e6599"
|
||||
dependencies = [
|
||||
"console_error_panic_hook",
|
||||
"js-sys",
|
||||
"scoped-tls",
|
||||
"wasm-bindgen",
|
||||
"wasm-bindgen-futures",
|
||||
"wasm-bindgen-test-macro",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-test-macro"
|
||||
version = "0.3.32"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6150d36a03e90a3cf6c12650be10626a9902d70c5270fd47d7a47e5389a10d56"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "web-sys"
|
||||
version = "0.3.59"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ed055ab27f941423197eb86b2035720b1a3ce40504df082cac2ecc6ed73335a1"
|
||||
dependencies = [
|
||||
"js-sys",
|
||||
"wasm-bindgen",
|
||||
]
|
||||
|
|
|
@ -4,7 +4,7 @@ description = "Noughts 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.9"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[lib]
|
||||
|
@ -12,3 +12,6 @@ crate-type = ["cdylib", "rlib"]
|
|||
|
||||
[dependencies]
|
||||
wasm-bindgen = "0.2.82"
|
||||
|
||||
[dev-dependencies]
|
||||
wasm-bindgen-test = "0.3.32"
|
||||
|
|
|
@ -4,17 +4,17 @@ use wasm_bindgen::prelude::wasm_bindgen;
|
|||
pub fn find_winner(board: &[u8]) -> u8 {
|
||||
for i in 0..3 {
|
||||
let h = i*3;
|
||||
if board[h] == board[h + 1] && board[h + 1] == board[h + 2] {
|
||||
if board[h] != 0 && board[h] == board[h + 1] && board[h + 1] == board[h + 2] {
|
||||
return board[h];
|
||||
}
|
||||
if board[i] == board[i + 3] && board[i + 3] == board[i + 6] {
|
||||
if board[i] != 0 && board[i] == board[i + 3] && board[i + 3] == board[i + 6] {
|
||||
return board[i];
|
||||
}
|
||||
}
|
||||
if board[0] == board[4] && board[4] == board[8] {
|
||||
if board[0] != 0 && board[0] == board[4] && board[4] == board[8] {
|
||||
return board[0];
|
||||
}
|
||||
if board[2] == board[4] && board[4] == board[6] {
|
||||
if board[2] != 0 && board[2] == board[4] && board[4] == board[6] {
|
||||
return board[2];
|
||||
}
|
||||
0
|
||||
|
|
|
@ -0,0 +1,55 @@
|
|||
extern crate wasm_bindgen_test;
|
||||
|
||||
use wasm_bindgen_test::*;
|
||||
use o_x_rust::*;
|
||||
|
||||
#[wasm_bindgen_test]
|
||||
fn find_winner_test() {
|
||||
assert_eq!(find_winner(&[0, 0, 0, 1, 1, 1, 0, 0, 0]), 1);
|
||||
assert_eq!(find_winner(&[2, 1, 1, 1, 2, 1, 0, 0, 2]), 2);
|
||||
assert_eq!(find_winner(&[2, 1, 1, 1, 2, 1, 0, 0, 1]), 1);
|
||||
assert_eq!(find_winner(&[2, 1, 2, 1, 2, 1, 2, 1, 0]), 2);
|
||||
}
|
||||
|
||||
#[wasm_bindgen_test]
|
||||
fn count_empty_test() {
|
||||
assert_eq!(count_empty(&[0, 0, 1, 0, 2, 0, 0, 0, 0]), 7);
|
||||
assert_eq!(count_empty(&[1, 2, 1, 1, 2, 1, 2, 2, 1]), 0);
|
||||
}
|
||||
|
||||
#[wasm_bindgen_test]
|
||||
fn get_turn_test() {
|
||||
assert_eq!(get_turn(1, 2, true, 6), 2);
|
||||
assert_eq!(get_turn(1, 2, false, 7), 2);
|
||||
assert_eq!(get_turn(2, 1, true, 2), 1);
|
||||
assert_eq!(get_turn(2, 1, false, 2), 2);
|
||||
}
|
||||
|
||||
// Go through the flow, testing whether anything panics (which it shouldn't)
|
||||
// and that predict doesn't return impossible moves
|
||||
#[wasm_bindgen_test]
|
||||
fn test_scores() {
|
||||
let board1: &[u8] = &[1, 2, 1, 2, 1, 2, 0, 1, 2];
|
||||
let board2: &[u8] = &[0, 0, 0, 0, 1, 0, 2, 0, 0];
|
||||
let me = 2;
|
||||
let other = 1;
|
||||
let first = false;
|
||||
|
||||
let algorithms = &["sa", "sa+rd", "sa+r-d", "mm", "mm+d"];
|
||||
|
||||
algorithms.iter()
|
||||
.for_each(|&algorithm| {
|
||||
let p1 = predict(me, other, first, board1, algorithm);
|
||||
assert_eq!(p1, 6);
|
||||
|
||||
let p2 = predict(me, other, first, board2, algorithm);
|
||||
assert!(board2.iter()
|
||||
.enumerate()
|
||||
.filter_map(|(idx, &v)| {
|
||||
if v != 0 {
|
||||
Some(idx)
|
||||
} else { None }
|
||||
})
|
||||
.all(|idx| p2 != idx));
|
||||
});
|
||||
}
|
Loading…
Reference in New Issue