#十字谜 #单词 #生成 #游戏

wasm_crossword_generator

一个支持一流WebAssembly(WASM)的纯Rust库,用于生成十字谜

4个版本

0.0.4 2024年3月3日
0.0.3 2024年2月5日
0.0.2 2024年2月4日
0.0.1 2024年2月3日

#3 in #十字谜

每月下载量27次

MIT/Apache

76KB
1.5K SLoC

WASM十字谜生成器

wasm_crossword_generator是一个库,用于生成和操作带有一流WebAssembly (WASM)支持的十字谜,针对浏览器和其他WASM环境。虽然作为一个Rust库功能齐全且易于使用,但设计以WASM为先,因此做出了一些权衡,例如不使用const generics,避免在JSON反/序列化过程中的歧义,并使用结构体而非元组。

在纯Rust中使用的基本示例如下

use wasm_crossword_generator::*;

let words: Vec<Word> = vec![
  Word { text: "library".to_string(), clue: Some("Not a framework.".to_string()) },
  // In a real usage, there would be more entries.
];

// A real SolutionConf would probably want "requirements" to allow for retrying crossword
// generation. Because there is only one word, we know we'll get the world's simplest puzzle in
// one try.
let solution_conf = SolutionConf {
    words: words,
    max_words: 20,
    width: 10,
    height: 10,
    requirements: None,
    initial_placement: None,
};

// A PerWordPuzzle is one where the player only guesses words, not locations. The player is
// immediately informed if the guess is correct or not.
let mut puzzle = PerWordPuzzle::new(solution_conf)?;

let guess = PlacedWord {
  // Because it is a PerWordPuzzle, the placement is ignored, unlike other Playmodes.
  placement: Placement { x: 0, y: 0, direction: rand::random() },
  // NOTE: you don't need to match the "clue" field, it is ignored for purposes of PartialEq
  word: Word { text: "library".to_string(), clue: None }
};
let guess_result = puzzle.guess_word(guess)?;

// Because there is only one word, the guess will result in "Complete" instead of "Correct"
assert_eq!(guess_result, GuessResult::Complete);

更多详情请参阅官方文档此处。在此项目目录之上,此crate打包用于浏览器使用,并发布到NPM,更多详情请参阅那里。

使用此库的示例网站可以在线查看和玩此处。该网站的源代码旨在作为示例/演示,并可在此处找到。

依赖关系

~2–3.5MB
~58K SLoC