web sight
This commit is contained in:
parent
bc397cfd25
commit
f821dc877c
26 changed files with 11917 additions and 7 deletions
44
Cargo.lock
generated
44
Cargo.lock
generated
|
@ -30,6 +30,12 @@ dependencies = [
|
||||||
"wasm-bindgen",
|
"wasm-bindgen",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "fnv"
|
||||||
|
version = "1.0.7"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "getrandom"
|
name = "getrandom"
|
||||||
version = "0.2.5"
|
version = "0.2.5"
|
||||||
|
@ -37,8 +43,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "d39cd93900197114fa1fcb7ae84ca742095eed9442088988ae74fa744e930e77"
|
checksum = "d39cd93900197114fa1fcb7ae84ca742095eed9442088988ae74fa744e930e77"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"cfg-if 1.0.0",
|
"cfg-if 1.0.0",
|
||||||
|
"js-sys",
|
||||||
"libc",
|
"libc",
|
||||||
"wasi",
|
"wasi",
|
||||||
|
"wasm-bindgen",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -146,6 +154,38 @@ version = "1.0.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "ea6a9290e3c9cf0f18145ef7ffa62d68ee0bf5fcd651017e586dc7fd5da448c2"
|
checksum = "ea6a9290e3c9cf0f18145ef7ffa62d68ee0bf5fcd651017e586dc7fd5da448c2"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "serde"
|
||||||
|
version = "1.0.136"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "ce31e24b01e1e524df96f1c2fdd054405f8d7376249a5110886fb4b658484789"
|
||||||
|
dependencies = [
|
||||||
|
"serde_derive",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "serde-wasm-bindgen"
|
||||||
|
version = "0.4.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d5d0927d4115e78b52dfd4cabcb3cc79bd56b94a53cf27c074bf8b83af1765d1"
|
||||||
|
dependencies = [
|
||||||
|
"fnv",
|
||||||
|
"js-sys",
|
||||||
|
"serde",
|
||||||
|
"wasm-bindgen",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "serde_derive"
|
||||||
|
version = "1.0.136"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "08597e7152fcd306f41838ed3e37be9eaeed2b61c42e2117266a554fab4662f9"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "syn"
|
name = "syn"
|
||||||
version = "1.0.86"
|
version = "1.0.86"
|
||||||
|
@ -264,9 +304,12 @@ name = "web-optimle"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"console_error_panic_hook",
|
"console_error_panic_hook",
|
||||||
|
"serde",
|
||||||
|
"serde-wasm-bindgen",
|
||||||
"wasm-bindgen",
|
"wasm-bindgen",
|
||||||
"wasm-bindgen-test",
|
"wasm-bindgen-test",
|
||||||
"wee_alloc",
|
"wee_alloc",
|
||||||
|
"wordlelike",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -317,5 +360,6 @@ checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
|
||||||
name = "wordlelike"
|
name = "wordlelike"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"getrandom",
|
||||||
"rand",
|
"rand",
|
||||||
]
|
]
|
||||||
|
|
|
@ -8,10 +8,13 @@ edition = "2018"
|
||||||
crate-type = ["cdylib", "rlib"]
|
crate-type = ["cdylib", "rlib"]
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = ["console_error_panic_hook"]
|
default = ["console_error_panic_hook", "wee_alloc"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
wasm-bindgen = "0.2.63"
|
wordlelike = { path = "../wordlelike" }
|
||||||
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
|
wasm-bindgen = "0.2.43"
|
||||||
|
serde-wasm-bindgen = "0.4.2"
|
||||||
|
|
||||||
# The `console_error_panic_hook` crate provides better debugging of panics by
|
# The `console_error_panic_hook` crate provides better debugging of panics by
|
||||||
# logging them with `console.error`. This is great for development, but requires
|
# logging them with `console.error`. This is great for development, but requires
|
||||||
|
@ -24,6 +27,8 @@ console_error_panic_hook = { version = "0.1.6", optional = true }
|
||||||
# allocator, however.
|
# allocator, however.
|
||||||
wee_alloc = { version = "0.4.5", optional = true }
|
wee_alloc = { version = "0.4.5", optional = true }
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
wasm-bindgen-test = "0.3.13"
|
wasm-bindgen-test = "0.3.13"
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,10 @@
|
||||||
mod utils;
|
mod utils;
|
||||||
|
|
||||||
|
use std::convert::TryInto;
|
||||||
|
|
||||||
use wasm_bindgen::prelude::*;
|
use wasm_bindgen::prelude::*;
|
||||||
|
use wordlelike::*;
|
||||||
|
use wordlelike::evaluator::Evaluator;
|
||||||
|
|
||||||
// When the `wee_alloc` feature is enabled, use `wee_alloc` as the global
|
// When the `wee_alloc` feature is enabled, use `wee_alloc` as the global
|
||||||
// allocator.
|
// allocator.
|
||||||
|
@ -8,12 +12,119 @@ use wasm_bindgen::prelude::*;
|
||||||
#[global_allocator]
|
#[global_allocator]
|
||||||
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
|
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
|
||||||
|
|
||||||
#[wasm_bindgen]
|
#[macro_export]
|
||||||
extern {
|
macro_rules! matches_to_wordlelike {
|
||||||
fn alert(s: &str);
|
( $x:expr ) => {
|
||||||
|
{
|
||||||
|
$x.iter()
|
||||||
|
.map(|n| match n {
|
||||||
|
0 => wordlelike::guess::LetterMatch::NOWHERE,
|
||||||
|
1 => wordlelike::guess::LetterMatch::ELSEWHERE,
|
||||||
|
2 => wordlelike::guess::LetterMatch::HERE,
|
||||||
|
_ => wordlelike::guess::LetterMatch::NOWHERE,
|
||||||
|
})
|
||||||
|
.collect::<Vec<wordlelike::guess::LetterMatch>>()
|
||||||
|
.try_into()
|
||||||
|
.unwrap()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! matches_from_wordlelike {
|
||||||
|
( $x:expr ) => {
|
||||||
|
{
|
||||||
|
$x.iter()
|
||||||
|
.map(|n| match n {
|
||||||
|
wordlelike::guess::LetterMatch::NOWHERE => 0,
|
||||||
|
wordlelike::guess::LetterMatch::ELSEWHERE => 1,
|
||||||
|
wordlelike::guess::LetterMatch::HERE => 2,
|
||||||
|
})
|
||||||
|
.collect::<Vec<u8>>()
|
||||||
|
.try_into()
|
||||||
|
.unwrap()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[wasm_bindgen]
|
#[wasm_bindgen]
|
||||||
pub fn greet() {
|
#[derive(serde::Serialize, serde::Deserialize)]
|
||||||
alert("Hello, web-optimle!");
|
pub struct GuessResult {
|
||||||
|
word: String,
|
||||||
|
matches: Vec<u8>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[wasm_bindgen]
|
||||||
|
impl GuessResult {
|
||||||
|
pub fn word(&self) -> JsValue {
|
||||||
|
serde_wasm_bindgen::to_value(&self.word).unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn matches(&self) -> JsValue {
|
||||||
|
serde_wasm_bindgen::to_value(&self.matches).unwrap()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[wasm_bindgen]
|
||||||
|
pub struct Game {
|
||||||
|
dictionary: Vec<guess::Word<5>>,
|
||||||
|
initial_history: Vec<GuessResult>,
|
||||||
|
current_attempt: Vec<GuessResult>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Game {
|
||||||
|
fn build_adversary(&self) -> evaluator::AdversarialEvaluator<5> {
|
||||||
|
let mut adversary = evaluator::AdversarialEvaluator::new(&self.dictionary);
|
||||||
|
for result in &self.initial_history {
|
||||||
|
adversary.push(guess::GuessResult {
|
||||||
|
word: word!(result.word),
|
||||||
|
matches: matches_to_wordlelike!(result.matches),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
for result in &self.current_attempt {
|
||||||
|
adversary.push(guess::GuessResult {
|
||||||
|
word: word!(result.word),
|
||||||
|
matches: matches_to_wordlelike!(result.matches),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
adversary
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[wasm_bindgen]
|
||||||
|
impl Game {
|
||||||
|
pub fn new(dictionary: JsValue, initial_history: JsValue) -> Self {
|
||||||
|
utils::set_panic_hook();
|
||||||
|
let dict: Vec<String> = serde_wasm_bindgen::from_value(dictionary).unwrap();
|
||||||
|
Self {
|
||||||
|
dictionary: dict.iter().map(|word| word!(word)).collect(),
|
||||||
|
initial_history: serde_wasm_bindgen::from_value(initial_history).unwrap(),
|
||||||
|
current_attempt: Vec::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn current_attempt(&self) -> JsValue {
|
||||||
|
serde_wasm_bindgen::to_value(&self.current_attempt).unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn guess(&mut self, word: String) -> JsValue {
|
||||||
|
let word_: guess::Word<5> = word!(word);
|
||||||
|
let adversary = self.build_adversary();
|
||||||
|
let matches: Vec<u8> = matches_from_wordlelike!(adversary.evaluate(word_));
|
||||||
|
self.current_attempt.push(GuessResult { word, matches: matches.to_vec() });
|
||||||
|
|
||||||
|
serde_wasm_bindgen::to_value(&matches).unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn possible_word(&self) -> String {
|
||||||
|
let adversary = self.build_adversary();
|
||||||
|
adversary.possible_word()
|
||||||
|
.unwrap()
|
||||||
|
.iter()
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn reset(&mut self) {
|
||||||
|
self.current_attempt = Vec::new();
|
||||||
|
}
|
||||||
|
}
|
24
web-optimle/www/.bin/create-wasm-app.js
Executable file
24
web-optimle/www/.bin/create-wasm-app.js
Executable file
|
@ -0,0 +1,24 @@
|
||||||
|
#!/usr/bin/env node
|
||||||
|
|
||||||
|
const { spawn } = require("child_process");
|
||||||
|
const fs = require("fs");
|
||||||
|
|
||||||
|
let folderName = '.';
|
||||||
|
|
||||||
|
if (process.argv.length >= 3) {
|
||||||
|
folderName = process.argv[2];
|
||||||
|
if (!fs.existsSync(folderName)) {
|
||||||
|
fs.mkdirSync(folderName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const clone = spawn("git", ["clone", "https://github.com/rustwasm/create-wasm-app.git", folderName]);
|
||||||
|
|
||||||
|
clone.on("close", code => {
|
||||||
|
if (code !== 0) {
|
||||||
|
console.error("cloning the template failed!")
|
||||||
|
process.exit(code);
|
||||||
|
} else {
|
||||||
|
console.log("🦀 Rust + 🕸 Wasm = ❤");
|
||||||
|
}
|
||||||
|
});
|
2
web-optimle/www/.gitignore
vendored
Normal file
2
web-optimle/www/.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
node_modules
|
||||||
|
dist
|
5
web-optimle/www/.travis.yml
Normal file
5
web-optimle/www/.travis.yml
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
language: node_js
|
||||||
|
node_js: "10"
|
||||||
|
|
||||||
|
script:
|
||||||
|
- ./node_modules/.bin/webpack
|
201
web-optimle/www/LICENSE-APACHE
Normal file
201
web-optimle/www/LICENSE-APACHE
Normal file
|
@ -0,0 +1,201 @@
|
||||||
|
Apache License
|
||||||
|
Version 2.0, January 2004
|
||||||
|
http://www.apache.org/licenses/
|
||||||
|
|
||||||
|
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||||
|
|
||||||
|
1. Definitions.
|
||||||
|
|
||||||
|
"License" shall mean the terms and conditions for use, reproduction,
|
||||||
|
and distribution as defined by Sections 1 through 9 of this document.
|
||||||
|
|
||||||
|
"Licensor" shall mean the copyright owner or entity authorized by
|
||||||
|
the copyright owner that is granting the License.
|
||||||
|
|
||||||
|
"Legal Entity" shall mean the union of the acting entity and all
|
||||||
|
other entities that control, are controlled by, or are under common
|
||||||
|
control with that entity. For the purposes of this definition,
|
||||||
|
"control" means (i) the power, direct or indirect, to cause the
|
||||||
|
direction or management of such entity, whether by contract or
|
||||||
|
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||||
|
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||||
|
|
||||||
|
"You" (or "Your") shall mean an individual or Legal Entity
|
||||||
|
exercising permissions granted by this License.
|
||||||
|
|
||||||
|
"Source" form shall mean the preferred form for making modifications,
|
||||||
|
including but not limited to software source code, documentation
|
||||||
|
source, and configuration files.
|
||||||
|
|
||||||
|
"Object" form shall mean any form resulting from mechanical
|
||||||
|
transformation or translation of a Source form, including but
|
||||||
|
not limited to compiled object code, generated documentation,
|
||||||
|
and conversions to other media types.
|
||||||
|
|
||||||
|
"Work" shall mean the work of authorship, whether in Source or
|
||||||
|
Object form, made available under the License, as indicated by a
|
||||||
|
copyright notice that is included in or attached to the work
|
||||||
|
(an example is provided in the Appendix below).
|
||||||
|
|
||||||
|
"Derivative Works" shall mean any work, whether in Source or Object
|
||||||
|
form, that is based on (or derived from) the Work and for which the
|
||||||
|
editorial revisions, annotations, elaborations, or other modifications
|
||||||
|
represent, as a whole, an original work of authorship. For the purposes
|
||||||
|
of this License, Derivative Works shall not include works that remain
|
||||||
|
separable from, or merely link (or bind by name) to the interfaces of,
|
||||||
|
the Work and Derivative Works thereof.
|
||||||
|
|
||||||
|
"Contribution" shall mean any work of authorship, including
|
||||||
|
the original version of the Work and any modifications or additions
|
||||||
|
to that Work or Derivative Works thereof, that is intentionally
|
||||||
|
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||||
|
or by an individual or Legal Entity authorized to submit on behalf of
|
||||||
|
the copyright owner. For the purposes of this definition, "submitted"
|
||||||
|
means any form of electronic, verbal, or written communication sent
|
||||||
|
to the Licensor or its representatives, including but not limited to
|
||||||
|
communication on electronic mailing lists, source code control systems,
|
||||||
|
and issue tracking systems that are managed by, or on behalf of, the
|
||||||
|
Licensor for the purpose of discussing and improving the Work, but
|
||||||
|
excluding communication that is conspicuously marked or otherwise
|
||||||
|
designated in writing by the copyright owner as "Not a Contribution."
|
||||||
|
|
||||||
|
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||||
|
on behalf of whom a Contribution has been received by Licensor and
|
||||||
|
subsequently incorporated within the Work.
|
||||||
|
|
||||||
|
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||||
|
this License, each Contributor hereby grants to You a perpetual,
|
||||||
|
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||||
|
copyright license to reproduce, prepare Derivative Works of,
|
||||||
|
publicly display, publicly perform, sublicense, and distribute the
|
||||||
|
Work and such Derivative Works in Source or Object form.
|
||||||
|
|
||||||
|
3. Grant of Patent License. Subject to the terms and conditions of
|
||||||
|
this License, each Contributor hereby grants to You a perpetual,
|
||||||
|
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||||
|
(except as stated in this section) patent license to make, have made,
|
||||||
|
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||||
|
where such license applies only to those patent claims licensable
|
||||||
|
by such Contributor that are necessarily infringed by their
|
||||||
|
Contribution(s) alone or by combination of their Contribution(s)
|
||||||
|
with the Work to which such Contribution(s) was submitted. If You
|
||||||
|
institute patent litigation against any entity (including a
|
||||||
|
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||||
|
or a Contribution incorporated within the Work constitutes direct
|
||||||
|
or contributory patent infringement, then any patent licenses
|
||||||
|
granted to You under this License for that Work shall terminate
|
||||||
|
as of the date such litigation is filed.
|
||||||
|
|
||||||
|
4. Redistribution. You may reproduce and distribute copies of the
|
||||||
|
Work or Derivative Works thereof in any medium, with or without
|
||||||
|
modifications, and in Source or Object form, provided that You
|
||||||
|
meet the following conditions:
|
||||||
|
|
||||||
|
(a) You must give any other recipients of the Work or
|
||||||
|
Derivative Works a copy of this License; and
|
||||||
|
|
||||||
|
(b) You must cause any modified files to carry prominent notices
|
||||||
|
stating that You changed the files; and
|
||||||
|
|
||||||
|
(c) You must retain, in the Source form of any Derivative Works
|
||||||
|
that You distribute, all copyright, patent, trademark, and
|
||||||
|
attribution notices from the Source form of the Work,
|
||||||
|
excluding those notices that do not pertain to any part of
|
||||||
|
the Derivative Works; and
|
||||||
|
|
||||||
|
(d) If the Work includes a "NOTICE" text file as part of its
|
||||||
|
distribution, then any Derivative Works that You distribute must
|
||||||
|
include a readable copy of the attribution notices contained
|
||||||
|
within such NOTICE file, excluding those notices that do not
|
||||||
|
pertain to any part of the Derivative Works, in at least one
|
||||||
|
of the following places: within a NOTICE text file distributed
|
||||||
|
as part of the Derivative Works; within the Source form or
|
||||||
|
documentation, if provided along with the Derivative Works; or,
|
||||||
|
within a display generated by the Derivative Works, if and
|
||||||
|
wherever such third-party notices normally appear. The contents
|
||||||
|
of the NOTICE file are for informational purposes only and
|
||||||
|
do not modify the License. You may add Your own attribution
|
||||||
|
notices within Derivative Works that You distribute, alongside
|
||||||
|
or as an addendum to the NOTICE text from the Work, provided
|
||||||
|
that such additional attribution notices cannot be construed
|
||||||
|
as modifying the License.
|
||||||
|
|
||||||
|
You may add Your own copyright statement to Your modifications and
|
||||||
|
may provide additional or different license terms and conditions
|
||||||
|
for use, reproduction, or distribution of Your modifications, or
|
||||||
|
for any such Derivative Works as a whole, provided Your use,
|
||||||
|
reproduction, and distribution of the Work otherwise complies with
|
||||||
|
the conditions stated in this License.
|
||||||
|
|
||||||
|
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||||
|
any Contribution intentionally submitted for inclusion in the Work
|
||||||
|
by You to the Licensor shall be under the terms and conditions of
|
||||||
|
this License, without any additional terms or conditions.
|
||||||
|
Notwithstanding the above, nothing herein shall supersede or modify
|
||||||
|
the terms of any separate license agreement you may have executed
|
||||||
|
with Licensor regarding such Contributions.
|
||||||
|
|
||||||
|
6. Trademarks. This License does not grant permission to use the trade
|
||||||
|
names, trademarks, service marks, or product names of the Licensor,
|
||||||
|
except as required for reasonable and customary use in describing the
|
||||||
|
origin of the Work and reproducing the content of the NOTICE file.
|
||||||
|
|
||||||
|
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||||
|
agreed to in writing, Licensor provides the Work (and each
|
||||||
|
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||||
|
implied, including, without limitation, any warranties or conditions
|
||||||
|
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||||
|
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||||
|
appropriateness of using or redistributing the Work and assume any
|
||||||
|
risks associated with Your exercise of permissions under this License.
|
||||||
|
|
||||||
|
8. Limitation of Liability. In no event and under no legal theory,
|
||||||
|
whether in tort (including negligence), contract, or otherwise,
|
||||||
|
unless required by applicable law (such as deliberate and grossly
|
||||||
|
negligent acts) or agreed to in writing, shall any Contributor be
|
||||||
|
liable to You for damages, including any direct, indirect, special,
|
||||||
|
incidental, or consequential damages of any character arising as a
|
||||||
|
result of this License or out of the use or inability to use the
|
||||||
|
Work (including but not limited to damages for loss of goodwill,
|
||||||
|
work stoppage, computer failure or malfunction, or any and all
|
||||||
|
other commercial damages or losses), even if such Contributor
|
||||||
|
has been advised of the possibility of such damages.
|
||||||
|
|
||||||
|
9. Accepting Warranty or Additional Liability. While redistributing
|
||||||
|
the Work or Derivative Works thereof, You may choose to offer,
|
||||||
|
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||||
|
or other liability obligations and/or rights consistent with this
|
||||||
|
License. However, in accepting such obligations, You may act only
|
||||||
|
on Your own behalf and on Your sole responsibility, not on behalf
|
||||||
|
of any other Contributor, and only if You agree to indemnify,
|
||||||
|
defend, and hold each Contributor harmless for any liability
|
||||||
|
incurred by, or claims asserted against, such Contributor by reason
|
||||||
|
of your accepting any such warranty or additional liability.
|
||||||
|
|
||||||
|
END OF TERMS AND CONDITIONS
|
||||||
|
|
||||||
|
APPENDIX: How to apply the Apache License to your work.
|
||||||
|
|
||||||
|
To apply the Apache License to your work, attach the following
|
||||||
|
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||||
|
replaced with your own identifying information. (Don't include
|
||||||
|
the brackets!) The text should be enclosed in the appropriate
|
||||||
|
comment syntax for the file format. We also recommend that a
|
||||||
|
file or class name and description of purpose be included on the
|
||||||
|
same "printed page" as the copyright notice for easier
|
||||||
|
identification within third-party archives.
|
||||||
|
|
||||||
|
Copyright [yyyy] [name of copyright owner]
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
25
web-optimle/www/LICENSE-MIT
Normal file
25
web-optimle/www/LICENSE-MIT
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
Copyright (c) [year] [name]
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any
|
||||||
|
person obtaining a copy of this software and associated
|
||||||
|
documentation files (the "Software"), to deal in the
|
||||||
|
Software without restriction, including without
|
||||||
|
limitation the rights to use, copy, modify, merge,
|
||||||
|
publish, distribute, sublicense, and/or sell copies of
|
||||||
|
the Software, and to permit persons to whom the Software
|
||||||
|
is furnished to do so, subject to the following
|
||||||
|
conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice
|
||||||
|
shall be included in all copies or substantial portions
|
||||||
|
of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
|
||||||
|
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
|
||||||
|
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
||||||
|
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
|
||||||
|
SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||||
|
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||||
|
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
|
||||||
|
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||||
|
DEALINGS IN THE SOFTWARE.
|
67
web-optimle/www/README.md
Normal file
67
web-optimle/www/README.md
Normal file
|
@ -0,0 +1,67 @@
|
||||||
|
<div align="center">
|
||||||
|
|
||||||
|
<h1><code>create-wasm-app</code></h1>
|
||||||
|
|
||||||
|
<strong>An <code>npm init</code> template for kick starting a project that uses NPM packages containing Rust-generated WebAssembly and bundles them with Webpack.</strong>
|
||||||
|
|
||||||
|
<p>
|
||||||
|
<a href="https://travis-ci.org/rustwasm/create-wasm-app"><img src="https://img.shields.io/travis/rustwasm/create-wasm-app.svg?style=flat-square" alt="Build Status" /></a>
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<h3>
|
||||||
|
<a href="#usage">Usage</a>
|
||||||
|
<span> | </span>
|
||||||
|
<a href="https://discordapp.com/channels/442252698964721669/443151097398296587">Chat</a>
|
||||||
|
</h3>
|
||||||
|
|
||||||
|
<sub>Built with 🦀🕸 by <a href="https://rustwasm.github.io/">The Rust and WebAssembly Working Group</a></sub>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
## About
|
||||||
|
|
||||||
|
This template is designed for depending on NPM packages that contain
|
||||||
|
Rust-generated WebAssembly and using them to create a Website.
|
||||||
|
|
||||||
|
* Want to create an NPM package with Rust and WebAssembly? [Check out
|
||||||
|
`wasm-pack-template`.](https://github.com/rustwasm/wasm-pack-template)
|
||||||
|
* Want to make a monorepo-style Website without publishing to NPM? Check out
|
||||||
|
[`rust-webpack-template`](https://github.com/rustwasm/rust-webpack-template)
|
||||||
|
and/or
|
||||||
|
[`rust-parcel-template`](https://github.com/rustwasm/rust-parcel-template).
|
||||||
|
|
||||||
|
## 🚴 Usage
|
||||||
|
|
||||||
|
```
|
||||||
|
npm init wasm-app
|
||||||
|
```
|
||||||
|
|
||||||
|
## 🔋 Batteries Included
|
||||||
|
|
||||||
|
- `.gitignore`: ignores `node_modules`
|
||||||
|
- `LICENSE-APACHE` and `LICENSE-MIT`: most Rust projects are licensed this way, so these are included for you
|
||||||
|
- `README.md`: the file you are reading now!
|
||||||
|
- `index.html`: a bare bones html document that includes the webpack bundle
|
||||||
|
- `index.js`: example js file with a comment showing how to import and use a wasm pkg
|
||||||
|
- `package.json` and `package-lock.json`:
|
||||||
|
- pulls in devDependencies for using webpack:
|
||||||
|
- [`webpack`](https://www.npmjs.com/package/webpack)
|
||||||
|
- [`webpack-cli`](https://www.npmjs.com/package/webpack-cli)
|
||||||
|
- [`webpack-dev-server`](https://www.npmjs.com/package/webpack-dev-server)
|
||||||
|
- defines a `start` script to run `webpack-dev-server`
|
||||||
|
- `webpack.config.js`: configuration file for bundling your js with webpack
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
Licensed under either of
|
||||||
|
|
||||||
|
* Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
|
||||||
|
* MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
|
||||||
|
|
||||||
|
at your option.
|
||||||
|
|
||||||
|
### Contribution
|
||||||
|
|
||||||
|
Unless you explicitly state otherwise, any contribution intentionally
|
||||||
|
submitted for inclusion in the work by you, as defined in the Apache-2.0
|
||||||
|
license, shall be dual licensed as above, without any additional terms or
|
||||||
|
conditions.
|
12
web-optimle/www/index.html
Normal file
12
web-optimle/www/index.html
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<title>Hello wasm-pack!</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<noscript>This game requires JavaScript. Sorry for the inconvenience!</noscript>
|
||||||
|
<div id="app"></div>
|
||||||
|
<script src="./bootstrap.js"></script>
|
||||||
|
</body>
|
||||||
|
</html>
|
8074
web-optimle/www/package-lock.json
generated
Normal file
8074
web-optimle/www/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load diff
37
web-optimle/www/package.json
Normal file
37
web-optimle/www/package.json
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
{
|
||||||
|
"name": "web-optimle",
|
||||||
|
"version": "0.1.0",
|
||||||
|
"description": "Ash's cool new Wordlelike",
|
||||||
|
"main": "index.js",
|
||||||
|
"bin": {
|
||||||
|
"create-wasm-app": ".bin/create-wasm-app.js"
|
||||||
|
},
|
||||||
|
"scripts": {
|
||||||
|
"build": "webpack --config webpack.config.js",
|
||||||
|
"start": "webpack-dev-server"
|
||||||
|
},
|
||||||
|
"keywords": [
|
||||||
|
"webassembly",
|
||||||
|
"wasm",
|
||||||
|
"rust",
|
||||||
|
"webpack"
|
||||||
|
],
|
||||||
|
"author": "ashastral",
|
||||||
|
"license": "GPL-3.0-or-later",
|
||||||
|
"dependencies": {
|
||||||
|
"preact": "^10.6.6",
|
||||||
|
"web-optimle": "file:../pkg"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@tsconfig/recommended": "^1.0.1",
|
||||||
|
"@types/eslint": "^8.4.1",
|
||||||
|
"html-webpack-plugin": "^5.5.0",
|
||||||
|
"css-loader": "^6.6.0",
|
||||||
|
"style-loader": "^3.3.1",
|
||||||
|
"ts-loader": "^9.2.6",
|
||||||
|
"typescript": "^4.5.5",
|
||||||
|
"webpack": "^5.69.1",
|
||||||
|
"webpack-cli": "^4.9.2",
|
||||||
|
"webpack-dev-server": "^4.7.4"
|
||||||
|
}
|
||||||
|
}
|
47
web-optimle/www/src/About.tsx
Normal file
47
web-optimle/www/src/About.tsx
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
import { h } from 'preact';
|
||||||
|
import { gameName } from "./util";
|
||||||
|
|
||||||
|
export function About() {
|
||||||
|
return (
|
||||||
|
<div className="App-about">
|
||||||
|
<p>
|
||||||
|
<i>{gameName}</i> is a spin on the popular word game{" "}
|
||||||
|
<a href="https://www.powerlanguage.co.uk/wordle/">
|
||||||
|
<i>Wordle</i>
|
||||||
|
</a>{" "}
|
||||||
|
by <a href="https://twitter.com/powerlanguish">powerlanguage</a>,
|
||||||
|
built on <a href="https://twitter.com/chordbug">chordbug</a>'s{" "}
|
||||||
|
<a href="https://github.com/lynn/hello-wordl">hello wordl</a> and
|
||||||
|
inspired by <a href="https://qntm.org/">qntm</a>'s{" "}
|
||||||
|
<a href="https://qntm.org/files/absurdle/absurdle.html">Absurdle</a>.
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
You start with a prepopulated table of guesses and must find a way to
|
||||||
|
<i>guarantee</i> a win in exactly 2 guesses. There are always more than
|
||||||
|
two possible words at the start, so brute force won't work, but feel
|
||||||
|
free to try if you're stuck!
|
||||||
|
<br />
|
||||||
|
<br />
|
||||||
|
The feedback you get on your guesses is <i>adversarial</i>: the game
|
||||||
|
always responds in a way that minimizes the amount of information you
|
||||||
|
get, while staying within the set of possible words. To win, you must
|
||||||
|
force the game to narrow down the possibilities to a single word,
|
||||||
|
then guess that word.
|
||||||
|
<br />
|
||||||
|
<br />
|
||||||
|
The word list is fairly small and only includes "common" words.
|
||||||
|
Unlike Wordle & hello wordl, your guesses are constrained to this
|
||||||
|
small list as well, because losing on the 2nd guess to choosing a word
|
||||||
|
that you didn't know was "uncommon" would be silly.
|
||||||
|
</p>
|
||||||
|
<hr />
|
||||||
|
<p>
|
||||||
|
Like "hello wordl", this game will be free and ad-free for as long as
|
||||||
|
it's online.
|
||||||
|
<br />
|
||||||
|
<a href="https://meow.garden/contact-me">Contact Ash</a> if you have
|
||||||
|
any questions.
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
286
web-optimle/www/src/App.css
Normal file
286
web-optimle/www/src/App.css
Normal file
|
@ -0,0 +1,286 @@
|
||||||
|
body {
|
||||||
|
margin: 10px;
|
||||||
|
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Bylynn Sans', 'Oxygen',
|
||||||
|
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
|
||||||
|
sans-serif;
|
||||||
|
-webkit-font-smoothing: antialiased;
|
||||||
|
-moz-osx-font-smoothing: grayscale;
|
||||||
|
}
|
||||||
|
|
||||||
|
code {
|
||||||
|
font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
|
||||||
|
monospace;
|
||||||
|
}
|
||||||
|
|
||||||
|
* {
|
||||||
|
box-sizing: border-box;
|
||||||
|
}
|
||||||
|
|
||||||
|
body {
|
||||||
|
text-align: center;
|
||||||
|
background-color: #eeeeee;
|
||||||
|
}
|
||||||
|
|
||||||
|
.Row {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
.Row-letter {
|
||||||
|
margin: 2px;
|
||||||
|
border: 2px solid rgba(128, 128, 128, 0.8);
|
||||||
|
flex: 1;
|
||||||
|
max-width: 40px;
|
||||||
|
height: 40px;
|
||||||
|
font-size: 28px;
|
||||||
|
display: flex;
|
||||||
|
justify-content: center;
|
||||||
|
align-items: center;
|
||||||
|
text-transform: uppercase;
|
||||||
|
font-weight: bold;
|
||||||
|
}
|
||||||
|
.Row-letter-color {
|
||||||
|
margin: 2px;
|
||||||
|
border: 2px solid rgba(128, 128, 128, 0.8);
|
||||||
|
flex: 1;
|
||||||
|
max-width: 40px;
|
||||||
|
height: 40px;
|
||||||
|
font-size: 28px;
|
||||||
|
display: flex;
|
||||||
|
justify-content: center;
|
||||||
|
align-items: center;
|
||||||
|
text-transform: uppercase;
|
||||||
|
font-weight: bold;
|
||||||
|
}
|
||||||
|
|
||||||
|
.Row-annotation {
|
||||||
|
margin-inline-start: 16px;
|
||||||
|
width: 5em;
|
||||||
|
text-align: start;
|
||||||
|
}
|
||||||
|
|
||||||
|
.App-container {
|
||||||
|
position: relative;
|
||||||
|
max-width: 500px;
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
margin: 0 auto;
|
||||||
|
justify-content: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
.App-container h1 {
|
||||||
|
margin-top: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.Game,
|
||||||
|
h1 {
|
||||||
|
user-select: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.Game {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
}
|
||||||
|
|
||||||
|
table.Game-rows {
|
||||||
|
margin: auto;
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
}
|
||||||
|
|
||||||
|
table.Game-rows:focus {
|
||||||
|
outline: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
table.Game-rows > tbody {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
}
|
||||||
|
|
||||||
|
.Game-keyboard {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
}
|
||||||
|
|
||||||
|
.Game-keyboard-row {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: row;
|
||||||
|
justify-content: stretch;
|
||||||
|
}
|
||||||
|
|
||||||
|
.Game-keyboard-button {
|
||||||
|
margin: 2px;
|
||||||
|
background-color: #cdcdcd;
|
||||||
|
padding: 2px;
|
||||||
|
text-transform: capitalize;
|
||||||
|
border-radius: 4px;
|
||||||
|
min-height: 40px;
|
||||||
|
display: flex;
|
||||||
|
flex: 1;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
font-family: inherit;
|
||||||
|
font-size: 20px;
|
||||||
|
color: inherit;
|
||||||
|
text-decoration: inherit;
|
||||||
|
border: 2px solid transparent;
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
|
||||||
|
.Game-keyboard-button-wide {
|
||||||
|
flex: 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
.Game-keyboard-button:focus {
|
||||||
|
outline: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.letter-correct {
|
||||||
|
border: 2px solid rgba(0, 0, 0, 0.3);
|
||||||
|
background-color: rgb(87, 172, 120);
|
||||||
|
color: white !important;
|
||||||
|
}
|
||||||
|
|
||||||
|
.letter-elsewhere {
|
||||||
|
border: 2px dotted rgba(0, 0, 0, 0.3);
|
||||||
|
background-color: #e9c601;
|
||||||
|
color: white !important;
|
||||||
|
}
|
||||||
|
|
||||||
|
.letter-absent {
|
||||||
|
border: 2px solid transparent;
|
||||||
|
background-color: rgb(162, 162, 162);
|
||||||
|
color: white !important;
|
||||||
|
}
|
||||||
|
|
||||||
|
body.dark {
|
||||||
|
background-color: #404040;
|
||||||
|
color: #e0e0e0;
|
||||||
|
color-scheme: dark;
|
||||||
|
}
|
||||||
|
|
||||||
|
body.dark .Game-keyboard-button {
|
||||||
|
color: #404040;
|
||||||
|
}
|
||||||
|
|
||||||
|
a,
|
||||||
|
a:visited {
|
||||||
|
color: #8080ff;
|
||||||
|
}
|
||||||
|
|
||||||
|
a:active {
|
||||||
|
color: #cc77ff;
|
||||||
|
}
|
||||||
|
|
||||||
|
.Game-options {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
margin-bottom: 1rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.Game-options > * + * {
|
||||||
|
margin-inline-start: 0.5rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.Game-options button {
|
||||||
|
min-width: 4rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.App-footer {
|
||||||
|
margin: -1rem 0 2rem 0;
|
||||||
|
font-size: 80%;
|
||||||
|
line-height: 1.5;
|
||||||
|
}
|
||||||
|
|
||||||
|
.App-about {
|
||||||
|
margin-top: -1rem;
|
||||||
|
line-height: 1.4;
|
||||||
|
}
|
||||||
|
|
||||||
|
.App-about b {
|
||||||
|
background-color: #888;
|
||||||
|
color: white;
|
||||||
|
padding: 1px 3px;
|
||||||
|
border-radius: 2px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.App-about b.green-bg {
|
||||||
|
background-color: rgb(87, 172, 120);
|
||||||
|
}
|
||||||
|
|
||||||
|
.App-about b.yellow-bg {
|
||||||
|
background-color: #e9c601;
|
||||||
|
}
|
||||||
|
|
||||||
|
.Game-seed-info {
|
||||||
|
opacity: 0.7;
|
||||||
|
margin-top: 1em;
|
||||||
|
font-variant-numeric: tabular-nums;
|
||||||
|
}
|
||||||
|
|
||||||
|
.Game-sr-feedback,
|
||||||
|
.sr-only {
|
||||||
|
position: absolute;
|
||||||
|
left: -10000px;
|
||||||
|
top: auto;
|
||||||
|
width: 1px;
|
||||||
|
height: 1px;
|
||||||
|
overflow: hidden;
|
||||||
|
}
|
||||||
|
|
||||||
|
.Settings {
|
||||||
|
text-align: left;
|
||||||
|
font-size: 18px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.Settings-setting {
|
||||||
|
margin: 8px;
|
||||||
|
display: flex;
|
||||||
|
align-items: top;
|
||||||
|
}
|
||||||
|
|
||||||
|
.Settings-setting input {
|
||||||
|
width: 18px;
|
||||||
|
height: 18px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.Settings-setting input[type="range"] {
|
||||||
|
width: 50px;
|
||||||
|
height: 18px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.Settings-setting label {
|
||||||
|
margin-inline-start: 8px;
|
||||||
|
margin-inline-end: 8px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.top-right {
|
||||||
|
position: absolute;
|
||||||
|
top: 5px;
|
||||||
|
right: 5px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.emoji-link {
|
||||||
|
font-size: 24px;
|
||||||
|
text-decoration: none;
|
||||||
|
background: none;
|
||||||
|
border: none;
|
||||||
|
padding: 0;
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
|
||||||
|
.top-right > * + * {
|
||||||
|
margin-inline-start: 8px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.App-container.color-blind .letter-correct,
|
||||||
|
.App-container.color-blind .App-about b.green-bg {
|
||||||
|
background-color: #f5793a;
|
||||||
|
}
|
||||||
|
|
||||||
|
.App-container.color-blind .letter-elsewhere,
|
||||||
|
.App-container.color-blind .App-about b.yellow-bg {
|
||||||
|
background-color: #85c0f9;
|
||||||
|
}
|
219
web-optimle/www/src/Game.tsx
Normal file
219
web-optimle/www/src/Game.tsx
Normal file
|
@ -0,0 +1,219 @@
|
||||||
|
import { h } from 'preact';
|
||||||
|
import { useState, useRef, useMemo, useCallback, useEffect } from 'preact/hooks';
|
||||||
|
|
||||||
|
import * as wasm from 'web-optimle';
|
||||||
|
import { clue, describeClue, type Clue, type GuessResult } from './clue';
|
||||||
|
import { gameName, speak } from './util';
|
||||||
|
|
||||||
|
import { DICTIONARY } from './dictionary';
|
||||||
|
import { Row, RowState } from './Row';
|
||||||
|
import { Keyboard } from './Keyboard';
|
||||||
|
const MAX_GUESSES = 2;
|
||||||
|
const WORD_LENGTH = 5;
|
||||||
|
|
||||||
|
enum GameState {
|
||||||
|
Playing,
|
||||||
|
Won,
|
||||||
|
Lost,
|
||||||
|
}
|
||||||
|
|
||||||
|
interface GameProps {
|
||||||
|
hidden: boolean;
|
||||||
|
colorBlind: boolean;
|
||||||
|
keyboardLayout: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
type History = GuessResult[];
|
||||||
|
|
||||||
|
export function Game(props: GameProps) {
|
||||||
|
const history: History = [
|
||||||
|
{ word: 'batch', matches: [0, 0, 1, 0, 2] },
|
||||||
|
{ word: 'youth', matches: [0, 2, 0, 2, 2] },
|
||||||
|
{ word: 'tooth', matches: [0, 2, 0, 2, 2] },
|
||||||
|
];
|
||||||
|
const [gameState, setGameState] = useState(GameState.Playing);
|
||||||
|
const [guesses, setGuesses] = useState<GuessResult[]>([...history]);
|
||||||
|
const [currentGuess, setCurrentGuess] = useState<string>("");
|
||||||
|
const [tryNumber, setTryNumber] = useState(1);
|
||||||
|
const [hint, setHint] = useState<string>(
|
||||||
|
"Try to guarantee a win in 2 guesses, no matter the word!"
|
||||||
|
);
|
||||||
|
const tableRef = useRef<HTMLTableElement>(null);
|
||||||
|
const game = useMemo(() => wasm.Game.new(DICTIONARY, history), []);
|
||||||
|
const reset = useCallback(() => {
|
||||||
|
game.reset();
|
||||||
|
setGuesses([...history]);
|
||||||
|
setCurrentGuess("");
|
||||||
|
setGameState(GameState.Playing);
|
||||||
|
setTryNumber((x) => x + 1);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
async function share(copiedHint: string, text?: string) {
|
||||||
|
const url = window.location.origin + window.location.pathname;
|
||||||
|
const body = `${text}\n\n${url}`;
|
||||||
|
if (
|
||||||
|
/android|iphone|ipad|ipod|webos/i.test(navigator.userAgent) &&
|
||||||
|
!/firefox/i.test(navigator.userAgent)
|
||||||
|
) {
|
||||||
|
try {
|
||||||
|
await navigator.share({ text: body });
|
||||||
|
return;
|
||||||
|
} catch (e) {
|
||||||
|
console.warn("navigator.share failed:", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
await navigator.clipboard.writeText(body);
|
||||||
|
setHint(copiedHint);
|
||||||
|
return;
|
||||||
|
} catch (e) {
|
||||||
|
console.warn("navigator.clipboard.writeText failed:", e);
|
||||||
|
}
|
||||||
|
setHint(url);
|
||||||
|
}
|
||||||
|
|
||||||
|
const onKey = (key: string) => {
|
||||||
|
if (gameState !== GameState.Playing) {
|
||||||
|
if (key === "Enter") {
|
||||||
|
reset();
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (guesses.length === MAX_GUESSES) return;
|
||||||
|
if (/^[a-z]$/i.test(key)) {
|
||||||
|
setCurrentGuess((guess) =>
|
||||||
|
(guess + key.toLowerCase()).slice(0, WORD_LENGTH)
|
||||||
|
);
|
||||||
|
tableRef.current?.focus();
|
||||||
|
setHint("");
|
||||||
|
} else if (key === "Backspace") {
|
||||||
|
setCurrentGuess((guess) => guess.slice(0, -1));
|
||||||
|
setHint("");
|
||||||
|
} else if (key === "Enter") {
|
||||||
|
if (currentGuess.length !== WORD_LENGTH) {
|
||||||
|
setHint("Too short");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!DICTIONARY.includes(currentGuess)) {
|
||||||
|
setHint("Not a valid word");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
for (const g of guesses) {
|
||||||
|
const c = clue(g);
|
||||||
|
}
|
||||||
|
const matches: number[] = game.guess(currentGuess);
|
||||||
|
const result: GuessResult = {
|
||||||
|
word: currentGuess,
|
||||||
|
matches: matches,
|
||||||
|
}
|
||||||
|
setGuesses((guesses) => guesses.concat([result]));
|
||||||
|
setCurrentGuess("");
|
||||||
|
|
||||||
|
if (matches.every((m) => m === 2)) {
|
||||||
|
if (tryNumber === 1) {
|
||||||
|
setHint("You won on your first try! Amazing job.");
|
||||||
|
} else {
|
||||||
|
setHint(`You won in ${tryNumber} tries! Great job.`);
|
||||||
|
}
|
||||||
|
setGameState(GameState.Won);
|
||||||
|
} else if (guesses.length + 1 === history.length + MAX_GUESSES) {
|
||||||
|
setHint(`Not quite! The answer could've been ${game.possible_word().toUpperCase()}. (Enter to try again)`);
|
||||||
|
setGameState(GameState.Lost);
|
||||||
|
} else {
|
||||||
|
speak(describeClue(clue(result)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const onKeyDown = (e: KeyboardEvent) => {
|
||||||
|
if (!e.ctrlKey && !e.metaKey) {
|
||||||
|
onKey(e.key);
|
||||||
|
}
|
||||||
|
if (e.key === "Backspace") {
|
||||||
|
e.preventDefault();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
document.addEventListener("keydown", onKeyDown);
|
||||||
|
return () => {
|
||||||
|
document.removeEventListener("keydown", onKeyDown);
|
||||||
|
};
|
||||||
|
}, [currentGuess, gameState]);
|
||||||
|
|
||||||
|
let letterInfo = new Map<string, Clue>();
|
||||||
|
const tableRows = Array(history.length + MAX_GUESSES)
|
||||||
|
.fill(undefined)
|
||||||
|
.map((_, i) => {
|
||||||
|
const result = [
|
||||||
|
...guesses,
|
||||||
|
{ word: currentGuess, matches: [0, 0, 0, 0, 0] },
|
||||||
|
][i] ?? { word: "", matches: [0, 0, 0, 0, 0] };
|
||||||
|
const cluedLetters = clue(result);
|
||||||
|
const lockedIn = i < guesses.length;
|
||||||
|
if (lockedIn) {
|
||||||
|
for (const { clue, letter } of cluedLetters) {
|
||||||
|
if (clue === undefined) break;
|
||||||
|
const old = letterInfo.get(letter);
|
||||||
|
if (old === undefined || clue > old) {
|
||||||
|
letterInfo.set(letter, clue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return (
|
||||||
|
<Row
|
||||||
|
key={i}
|
||||||
|
wordLength={WORD_LENGTH}
|
||||||
|
rowState={
|
||||||
|
lockedIn
|
||||||
|
? RowState.LockedIn
|
||||||
|
: i === guesses.length
|
||||||
|
? RowState.Editing
|
||||||
|
: RowState.Pending
|
||||||
|
}
|
||||||
|
cluedLetters={cluedLetters}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="Game" style={{ display: props.hidden ? "none" : "block" }}>
|
||||||
|
<table
|
||||||
|
className="Game-rows"
|
||||||
|
tabIndex={0}
|
||||||
|
aria-label="Table of guesses"
|
||||||
|
ref={tableRef}
|
||||||
|
>
|
||||||
|
<tbody>{tableRows}</tbody>
|
||||||
|
</table>
|
||||||
|
<p
|
||||||
|
role="alert"
|
||||||
|
style={{
|
||||||
|
userSelect: /https?:/.test(hint) ? "text" : "none",
|
||||||
|
whiteSpace: "pre-wrap",
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{hint || `\u00a0`}
|
||||||
|
</p>
|
||||||
|
<Keyboard
|
||||||
|
layout={props.keyboardLayout}
|
||||||
|
letterInfo={letterInfo}
|
||||||
|
onKey={onKey}
|
||||||
|
/>
|
||||||
|
<p>
|
||||||
|
{gameState === GameState.Won && (
|
||||||
|
<button
|
||||||
|
onClick={() => {
|
||||||
|
const score = tryNumber === 1 ? "my first try!" : `try #${tryNumber}.`;
|
||||||
|
share(
|
||||||
|
"Result copied to clipboard!",
|
||||||
|
`I solved ${gameName} on ${score}`
|
||||||
|
);
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
Share results
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
49
web-optimle/www/src/Keyboard.tsx
Normal file
49
web-optimle/www/src/Keyboard.tsx
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
import { h } from 'preact';
|
||||||
|
import { Clue, clueClass } from "./clue";
|
||||||
|
|
||||||
|
interface KeyboardProps {
|
||||||
|
layout: string;
|
||||||
|
letterInfo: Map<string, Clue>;
|
||||||
|
onKey: (key: string) => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function Keyboard(props: KeyboardProps) {
|
||||||
|
const keyboard = props.layout
|
||||||
|
.split("-")
|
||||||
|
.map((row) =>
|
||||||
|
row
|
||||||
|
.split("")
|
||||||
|
.map((key) => key.replace("B", "Backspace").replace("E", "Enter"))
|
||||||
|
);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="Game-keyboard" aria-hidden="true">
|
||||||
|
{keyboard.map((row, i) => (
|
||||||
|
<div key={i} className="Game-keyboard-row">
|
||||||
|
{row.map((label, j) => {
|
||||||
|
let className = "Game-keyboard-button";
|
||||||
|
const clue = props.letterInfo.get(label);
|
||||||
|
if (clue !== undefined) {
|
||||||
|
className += " " + clueClass(clue);
|
||||||
|
}
|
||||||
|
if (label.length > 1) {
|
||||||
|
className += " Game-keyboard-button-wide";
|
||||||
|
}
|
||||||
|
return (
|
||||||
|
<button
|
||||||
|
tabIndex={-1}
|
||||||
|
key={j}
|
||||||
|
className={className}
|
||||||
|
onClick={() => {
|
||||||
|
props.onKey(label);
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{label.replace("Backspace", "⌫")}
|
||||||
|
</button>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
54
web-optimle/www/src/Row.tsx
Normal file
54
web-optimle/www/src/Row.tsx
Normal file
|
@ -0,0 +1,54 @@
|
||||||
|
import { h } from "preact";
|
||||||
|
import { Clue, clueClass, CluedLetter, clueWord } from "./clue";
|
||||||
|
|
||||||
|
export enum RowState {
|
||||||
|
LockedIn,
|
||||||
|
Editing,
|
||||||
|
Pending,
|
||||||
|
}
|
||||||
|
|
||||||
|
interface RowProps {
|
||||||
|
rowState: RowState;
|
||||||
|
wordLength: number;
|
||||||
|
cluedLetters: CluedLetter[];
|
||||||
|
annotation?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function Row(props: RowProps) {
|
||||||
|
const isLockedIn = props.rowState === RowState.LockedIn;
|
||||||
|
const isEditing = props.rowState === RowState.Editing;
|
||||||
|
const letterDivs = props.cluedLetters
|
||||||
|
.concat(Array(props.wordLength).fill({ clue: Clue.Absent, letter: "" }))
|
||||||
|
.slice(0, props.wordLength)
|
||||||
|
.map(({ clue, letter }, i) => {
|
||||||
|
let letterClass = "Row-letter";
|
||||||
|
if (isLockedIn && clue !== undefined) {
|
||||||
|
letterClass += " " + clueClass(clue);
|
||||||
|
}
|
||||||
|
return (
|
||||||
|
<td
|
||||||
|
key={i}
|
||||||
|
className={letterClass}
|
||||||
|
aria-live={isEditing ? "assertive" : "off"}
|
||||||
|
aria-label={
|
||||||
|
isLockedIn
|
||||||
|
? letter.toUpperCase() +
|
||||||
|
(clue === undefined ? "" : ": " + clueWord(clue))
|
||||||
|
: ""
|
||||||
|
}
|
||||||
|
>
|
||||||
|
{letter}
|
||||||
|
</td>
|
||||||
|
);
|
||||||
|
});
|
||||||
|
let rowClass = "Row";
|
||||||
|
if (isLockedIn) rowClass += " Row-locked-in";
|
||||||
|
return (
|
||||||
|
<tr className={rowClass}>
|
||||||
|
{letterDivs}
|
||||||
|
{props.annotation && (
|
||||||
|
<span className="Row-annotation">{props.annotation}</span>
|
||||||
|
)}
|
||||||
|
</tr>
|
||||||
|
);
|
||||||
|
}
|
5
web-optimle/www/src/bootstrap.js
vendored
Normal file
5
web-optimle/www/src/bootstrap.js
vendored
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
// A dependency graph that contains any wasm must all be imported
|
||||||
|
// asynchronously. This `bootstrap.js` file does the single async import, so
|
||||||
|
// that no one else needs to worry about it again.
|
||||||
|
import("./index.tsx")
|
||||||
|
.catch(e => console.error("Error importing `index.js`:", e));
|
52
web-optimle/www/src/clue.ts
Normal file
52
web-optimle/www/src/clue.ts
Normal file
|
@ -0,0 +1,52 @@
|
||||||
|
export enum Clue {
|
||||||
|
Absent,
|
||||||
|
Elsewhere,
|
||||||
|
Correct,
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface CluedLetter {
|
||||||
|
clue?: Clue;
|
||||||
|
letter: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export type GuessResult = {
|
||||||
|
word: string;
|
||||||
|
matches: number[];
|
||||||
|
};
|
||||||
|
|
||||||
|
export function clue(result: GuessResult): CluedLetter[] {
|
||||||
|
return result.word.split("").map((letter, i) => {
|
||||||
|
const match = result.matches[i];
|
||||||
|
return {
|
||||||
|
clue: match === 0 ? Clue.Absent : (match === 1 ? Clue.Elsewhere : (match === 2 ? Clue.Correct : Clue.Absent)),
|
||||||
|
letter: letter,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export function clueClass(clue: Clue): string {
|
||||||
|
if (clue === Clue.Absent) {
|
||||||
|
return "letter-absent";
|
||||||
|
} else if (clue === Clue.Elsewhere) {
|
||||||
|
return "letter-elsewhere";
|
||||||
|
} else {
|
||||||
|
return "letter-correct";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function clueWord(clue: Clue): string {
|
||||||
|
if (clue === Clue.Absent) {
|
||||||
|
return "no";
|
||||||
|
} else if (clue === Clue.Elsewhere) {
|
||||||
|
return "elsewhere";
|
||||||
|
} else {
|
||||||
|
return "correct";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function describeClue(clue: CluedLetter[]): string {
|
||||||
|
return clue
|
||||||
|
.map(({ letter, clue }) => letter.toUpperCase() + " " + clueWord(clue!))
|
||||||
|
.join(", ");
|
||||||
|
}
|
2317
web-optimle/www/src/dictionary.ts
Normal file
2317
web-optimle/www/src/dictionary.ts
Normal file
File diff suppressed because it is too large
Load diff
138
web-optimle/www/src/index.tsx
Normal file
138
web-optimle/www/src/index.tsx
Normal file
|
@ -0,0 +1,138 @@
|
||||||
|
import { h, render, Fragment } from 'preact';
|
||||||
|
import { useState, useEffect } from 'preact/hooks';
|
||||||
|
import { Game } from './Game';
|
||||||
|
import { gameName, urlParam } from "./util";
|
||||||
|
import "./App.css";
|
||||||
|
import { About } from './About';
|
||||||
|
|
||||||
|
function useSetting<T>(
|
||||||
|
key: string,
|
||||||
|
initial: T
|
||||||
|
): [T, (value: T | ((t: T) => T)) => void] {
|
||||||
|
const [current, setCurrent] = useState<T>(() => {
|
||||||
|
try {
|
||||||
|
const item = window.localStorage.getItem(key);
|
||||||
|
return item ? JSON.parse(item) : initial;
|
||||||
|
} catch (e) {
|
||||||
|
return initial;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
const setSetting = (value: T | ((t: T) => T)) => {
|
||||||
|
try {
|
||||||
|
const v = value instanceof Function ? value(current) : value;
|
||||||
|
setCurrent(v);
|
||||||
|
window.localStorage.setItem(key, JSON.stringify(v));
|
||||||
|
} catch (e) { }
|
||||||
|
};
|
||||||
|
return [current, setSetting];
|
||||||
|
}
|
||||||
|
|
||||||
|
export function App() {
|
||||||
|
type Page = "game" | "about" | "settings";
|
||||||
|
const [page, setPage] = useState<Page>("game");
|
||||||
|
const prefersDark =
|
||||||
|
window.matchMedia &&
|
||||||
|
window.matchMedia("(prefers-color-scheme: dark)").matches;
|
||||||
|
const [dark, setDark] = useSetting<boolean>("dark", prefersDark);
|
||||||
|
const [colorBlind, setColorBlind] = useSetting<boolean>("colorblind", false);
|
||||||
|
const [keyboard, setKeyboard] = useSetting<string>(
|
||||||
|
"keyboard",
|
||||||
|
"qwertyuiop-asdfghjkl-BzxcvbnmE"
|
||||||
|
);
|
||||||
|
const [enterLeft, setEnterLeft] = useSetting<boolean>("enter-left", false);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
document.body.className = dark ? "dark" : "";
|
||||||
|
setTimeout(() => {
|
||||||
|
// Avoid transition on page load
|
||||||
|
document.body.style.transition = "0.3s background-color ease-out";
|
||||||
|
}, 1);
|
||||||
|
}, [dark]);
|
||||||
|
|
||||||
|
const link = (emoji: string, label: string, page: Page) => (
|
||||||
|
<button
|
||||||
|
className="emoji-link"
|
||||||
|
onClick={() => setPage(page)}
|
||||||
|
title={label}
|
||||||
|
aria-label={label}
|
||||||
|
>
|
||||||
|
{emoji}
|
||||||
|
</button>
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className={"App-container" + (colorBlind ? " color-blind" : "")}>
|
||||||
|
<h1>
|
||||||
|
{gameName}
|
||||||
|
</h1>
|
||||||
|
<div className="top-right">
|
||||||
|
{page !== "game" ? (
|
||||||
|
link("❌", "Close", "game")
|
||||||
|
) : (
|
||||||
|
<>
|
||||||
|
{link("❓", "About", "about")}
|
||||||
|
{link("⚙️", "Settings", "settings")}
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
{page === "about" && <About />}
|
||||||
|
{page === "settings" && (
|
||||||
|
<div className="Settings">
|
||||||
|
<div className="Settings-setting">
|
||||||
|
<input
|
||||||
|
id="dark-setting"
|
||||||
|
type="checkbox"
|
||||||
|
checked={dark}
|
||||||
|
onChange={() => setDark((x: boolean) => !x)}
|
||||||
|
/>
|
||||||
|
<label htmlFor="dark-setting">Dark theme</label>
|
||||||
|
</div>
|
||||||
|
<div className="Settings-setting">
|
||||||
|
<input
|
||||||
|
id="colorblind-setting"
|
||||||
|
type="checkbox"
|
||||||
|
checked={colorBlind}
|
||||||
|
onChange={() => setColorBlind((x: boolean) => !x)}
|
||||||
|
/>
|
||||||
|
<label htmlFor="colorblind-setting">High-contrast colors</label>
|
||||||
|
</div>
|
||||||
|
<div className="Settings-setting">
|
||||||
|
<label htmlFor="keyboard-setting">Keyboard layout:</label>
|
||||||
|
<select
|
||||||
|
name="keyboard-setting"
|
||||||
|
id="keyboard-setting"
|
||||||
|
value={keyboard}
|
||||||
|
onChange={(e) => setKeyboard((e as any).target.value)}
|
||||||
|
>
|
||||||
|
<option value="qwertyuiop-asdfghjkl-BzxcvbnmE">QWERTY</option>
|
||||||
|
<option value="azertyuiop-qsdfghjklm-BwxcvbnE">AZERTY</option>
|
||||||
|
<option value="qwertzuiop-asdfghjkl-ByxcvbnmE">QWERTZ</option>
|
||||||
|
<option value="BpyfgcrlE-aoeuidhtns-qjkxbmwvz">Dvorak</option>
|
||||||
|
<option value="qwfpgjluy-arstdhneio-BzxcvbkmE">Colemak</option>
|
||||||
|
</select>
|
||||||
|
<input
|
||||||
|
style={{ marginLeft: 20 }}
|
||||||
|
id="enter-left-setting"
|
||||||
|
type="checkbox"
|
||||||
|
checked={enterLeft}
|
||||||
|
onChange={() => setEnterLeft((x: boolean) => !x)}
|
||||||
|
/>
|
||||||
|
<label htmlFor="enter-left-setting">"Enter" on left side</label>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
<Game
|
||||||
|
hidden={page !== "game"}
|
||||||
|
colorBlind={colorBlind}
|
||||||
|
keyboardLayout={keyboard.replaceAll(
|
||||||
|
/[BE]/g,
|
||||||
|
(x) => (enterLeft ? "EB" : "BE")["BE".indexOf(x)]
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
render(<App />, document.body);
|
79
web-optimle/www/src/util.ts
Normal file
79
web-optimle/www/src/util.ts
Normal file
|
@ -0,0 +1,79 @@
|
||||||
|
export const gameName = "Optimle";
|
||||||
|
|
||||||
|
function mulberry32(a: number) {
|
||||||
|
return function () {
|
||||||
|
var t = (a += 0x6d2b79f5);
|
||||||
|
t = Math.imul(t ^ (t >>> 15), t | 1);
|
||||||
|
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
|
||||||
|
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function urlParam(name: string): string | null {
|
||||||
|
return new URLSearchParams(window.location.search).get(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
export const seed = Number(urlParam("seed"));
|
||||||
|
const makeRandom = () => (seed ? mulberry32(seed) : () => Math.random());
|
||||||
|
let random = makeRandom();
|
||||||
|
|
||||||
|
export function resetRng(): void {
|
||||||
|
random = makeRandom();
|
||||||
|
}
|
||||||
|
|
||||||
|
export function pick<T>(array: Array<T>): T {
|
||||||
|
return array[Math.floor(array.length * random())];
|
||||||
|
}
|
||||||
|
|
||||||
|
// https://a11y-guidelines.orange.com/en/web/components-examples/make-a-screen-reader-talk/
|
||||||
|
export function speak(
|
||||||
|
text: string,
|
||||||
|
priority: "polite" | "assertive" = "assertive"
|
||||||
|
) {
|
||||||
|
var el = document.createElement("div");
|
||||||
|
var id = "speak-" + Date.now();
|
||||||
|
el.setAttribute("id", id);
|
||||||
|
el.setAttribute("aria-live", priority || "polite");
|
||||||
|
el.classList.add("sr-only");
|
||||||
|
document.body.appendChild(el);
|
||||||
|
|
||||||
|
window.setTimeout(function () {
|
||||||
|
document.getElementById(id)!.innerHTML = text;
|
||||||
|
}, 100);
|
||||||
|
|
||||||
|
window.setTimeout(function () {
|
||||||
|
document.body.removeChild(document.getElementById(id)!);
|
||||||
|
}, 1000);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function ordinal(n: number): string {
|
||||||
|
return n + ([, "st", "nd", "rd"][(n % 100 >> 3) ^ 1 && n % 10] || "th");
|
||||||
|
}
|
||||||
|
|
||||||
|
export const englishNumbers =
|
||||||
|
"zero one two three four five six seven eight nine ten eleven".split(" ");
|
||||||
|
|
||||||
|
export function describeSeed(seed: number): string {
|
||||||
|
const year = Math.floor(seed / 10000);
|
||||||
|
const month = Math.floor(seed / 100) % 100;
|
||||||
|
const day = seed % 100;
|
||||||
|
const isLeap = year % (year % 25 ? 4 : 16) === 0;
|
||||||
|
const feb = isLeap ? 29 : 28;
|
||||||
|
const days = [0, 31, feb, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
|
||||||
|
if (
|
||||||
|
year >= 2000 &&
|
||||||
|
year <= 2100 &&
|
||||||
|
month >= 1 &&
|
||||||
|
month <= 12 &&
|
||||||
|
day >= 1 &&
|
||||||
|
day <= days[month]
|
||||||
|
) {
|
||||||
|
return new Date(year, month - 1, day).toLocaleDateString("en-US", {
|
||||||
|
day: "numeric",
|
||||||
|
month: "long",
|
||||||
|
year: "numeric",
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
return "seed " + seed;
|
||||||
|
}
|
||||||
|
}
|
16
web-optimle/www/tsconfig.json
Normal file
16
web-optimle/www/tsconfig.json
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
{
|
||||||
|
"extends": "@tsconfig/recommended/tsconfig.json",
|
||||||
|
"compilerOptions": {
|
||||||
|
"sourceMap": true,
|
||||||
|
"jsx": "react",
|
||||||
|
"jsxFactory": "h",
|
||||||
|
"jsxFragmentFactory": "Fragment",
|
||||||
|
"lib": [
|
||||||
|
"ES2021.String"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"include": [
|
||||||
|
"src/*.ts",
|
||||||
|
"src/*.tsx"
|
||||||
|
]
|
||||||
|
}
|
36
web-optimle/www/webpack.config.js
Normal file
36
web-optimle/www/webpack.config.js
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
const HtmlWebpackPlugin = require('html-webpack-plugin');
|
||||||
|
const path = require('path');
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
entry: "./src/bootstrap.js",
|
||||||
|
output: {
|
||||||
|
path: path.resolve(__dirname, "dist"),
|
||||||
|
filename: "bootstrap.js",
|
||||||
|
},
|
||||||
|
mode: "development",
|
||||||
|
devtool: "source-map",
|
||||||
|
resolve: {
|
||||||
|
extensions: ['', '.js', '.ts', '.tsx'],
|
||||||
|
},
|
||||||
|
experiments: {
|
||||||
|
syncWebAssembly: true,
|
||||||
|
},
|
||||||
|
module: {
|
||||||
|
rules: [
|
||||||
|
{
|
||||||
|
test: /\.tsx?$/,
|
||||||
|
exclude: /node_modules/,
|
||||||
|
use: 'ts-loader',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
test: /\.css$/,
|
||||||
|
use: ['style-loader', 'css-loader'],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
plugins: [
|
||||||
|
new HtmlWebpackPlugin({
|
||||||
|
title: 'Optimle, a word guessing game',
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
};
|
|
@ -8,4 +8,5 @@ edition = "2018"
|
||||||
crate-type = ["rlib"]
|
crate-type = ["rlib"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
getrandom = { version = "*", features = ["js"] }
|
||||||
rand = "0.8.5"
|
rand = "0.8.5"
|
||||||
|
|
|
@ -43,6 +43,10 @@ impl<const N: usize> AdversarialEvaluator<N> {
|
||||||
constraints: constraints::Total::new(),
|
constraints: constraints::Total::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn possible_word(&self) -> Option<&Word<N>> {
|
||||||
|
self.possible_words.first()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<const N: usize> Evaluator<N> for AdversarialEvaluator<N> {
|
impl<const N: usize> Evaluator<N> for AdversarialEvaluator<N> {
|
||||||
|
|
Loading…
Reference in a new issue