learn-hieroglyphs/src/game.rs

153 lines
3.9 KiB
Rust
Raw Normal View History

2024-03-11 22:37:13 +03:00
use rand::Rng;
2024-03-21 01:30:47 +03:00
const VOWELS: usize = 5;
const CONSONANTS: usize = 11;
const OBS: &str = "";
type KanaCharacters<'a> = [[&'a str; VOWELS]; CONSONANTS];
2024-03-26 13:37:19 +03:00
#[derive(Clone)]
2024-03-11 22:37:13 +03:00
pub enum Kanas {
Romaji,
Hiragana,
2024-03-21 01:30:47 +03:00
Katakana,
2024-03-11 22:37:13 +03:00
}
2024-03-13 01:05:02 +03:00
impl std::fmt::Display for Kanas {
2024-03-26 13:37:19 +03:00
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}",
match self {
Kanas::Romaji => "Romaji",
Kanas::Hiragana => "Hiragana",
Kanas::Katakana => "Katakana",
}
)
2024-03-26 13:37:19 +03:00
}
}
2024-03-21 01:30:47 +03:00
pub struct Hieroglyph {
syllable: String,
row: usize,
column: usize,
}
2024-03-11 22:37:13 +03:00
2024-03-25 05:20:18 +03:00
pub trait Useful {
// fn get_syllable (&self) -> String;
fn get_kana(&self) -> Kanas;
2024-03-25 05:20:18 +03:00
}
impl Useful for Hieroglyph {
// fn get_syllable (&self) -> String {
// self.syllable.to_string()
// }
fn get_kana(&self) -> Kanas {
2024-03-25 05:20:18 +03:00
for row in ROMAJI {
if row.contains(&(self.syllable).as_str()) {
return Kanas::Romaji;
}
2024-03-25 05:20:18 +03:00
}
for row in HIRAGANA {
if row.contains(&(self.syllable).as_str()) {
return Kanas::Hiragana;
}
2024-03-25 05:20:18 +03:00
}
for row in KATAKANA {
if row.contains(&(self.syllable).as_str()) {
return Kanas::Katakana;
}
2024-03-25 05:20:18 +03:00
}
Kanas::Romaji // /shrug
}
}
2024-03-11 22:37:13 +03:00
impl std::fmt::Display for Hieroglyph {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.syllable)
}
}
2024-03-21 01:30:47 +03:00
impl std::fmt::Debug for Hieroglyph {
2024-03-11 22:37:13 +03:00
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
2024-03-21 01:30:47 +03:00
write!(
f,
"Hieroglyph: {} => ({}, {})",
self.syllable, self.row, self.column
)
2024-03-11 22:37:13 +03:00
}
}
2024-03-21 01:30:47 +03:00
const ROMAJI: KanaCharacters = [
["a", "i", "u", "e", "o"],
2024-03-11 22:37:13 +03:00
["ka", "ki", "ku", "ke", "ko"],
["sa", "si", "su", "se", "so"],
["ta", "ti", "tu", "te", "to"],
["na", "ni", "nu", "ne", "no"],
["ha", "hi", "hu", "he", "ho"],
["ma", "mi", "mu", "me", "mo"],
2024-03-21 01:30:47 +03:00
["ya", OBS, "yu", OBS, "yo"],
2024-03-11 22:37:13 +03:00
["ra", "ri", "ru", "re", "ro"],
2024-03-21 01:30:47 +03:00
["wa", OBS, OBS, OBS, "wo"],
["n", OBS, OBS, OBS, OBS],
2024-03-11 22:37:13 +03:00
];
2024-03-21 01:30:47 +03:00
const HIRAGANA: KanaCharacters = [
["", "", "", "", ""],
2024-03-11 22:37:13 +03:00
["", "", "", "", ""],
["", "", "", "", ""],
["", "", "", "", ""],
["", "", "", "", ""],
["", "", "", "", ""],
["", "", "", "", ""],
2024-03-21 01:30:47 +03:00
["", OBS, "", OBS, ""],
2024-03-11 22:37:13 +03:00
["", "", "", "", ""],
2024-03-21 01:30:47 +03:00
["", OBS, OBS, OBS, ""],
["", OBS, OBS, OBS, OBS],
2024-03-11 22:37:13 +03:00
];
2024-03-21 01:30:47 +03:00
const KATAKANA: KanaCharacters = [
["", "", "", "", ""],
2024-03-11 22:37:13 +03:00
["", "", "", "", ""],
["", "", "", "", ""],
["", "", "", "", ""],
["", "", "", "", ""],
["", "", "", "", ""],
["", "", "", "", ""],
2024-03-21 01:30:47 +03:00
["", OBS, "", OBS, ""],
2024-03-11 22:37:13 +03:00
["", "", "", "", ""],
2024-03-21 01:30:47 +03:00
["", OBS, OBS, OBS, ""],
["", OBS, OBS, OBS, OBS],
2024-03-11 22:37:13 +03:00
];
2024-03-21 01:30:47 +03:00
fn get_characters_for_kana(k: &Kanas) -> KanaCharacters {
2024-03-11 22:37:13 +03:00
match k {
2024-03-21 01:30:47 +03:00
Kanas::Hiragana => HIRAGANA,
Kanas::Katakana => KATAKANA,
Kanas::Romaji => ROMAJI,
}
}
2024-03-26 13:37:19 +03:00
pub fn get_kana_pair_for_hieroglyph(k: &Kanas, h: &Hieroglyph) -> Hieroglyph {
2024-03-21 01:30:47 +03:00
Hieroglyph {
syllable: get_characters_for_kana(&k)[h.row][h.column].to_string(),
row: h.row,
column: h.column,
2024-03-11 22:37:13 +03:00
}
}
pub fn generate_random_hieroglyph(k: Kanas) -> Hieroglyph {
2024-03-21 01:30:47 +03:00
let row: usize = rand::thread_rng().gen_range(0..CONSONANTS);
let column: usize = rand::thread_rng().gen_range(0..VOWELS);
2024-03-11 22:37:13 +03:00
2024-03-21 01:30:47 +03:00
let syllable: String = get_characters_for_kana(&k)[row][column].to_string();
2024-03-13 01:05:02 +03:00
2024-03-21 01:30:47 +03:00
if syllable == OBS {
2024-03-11 22:37:13 +03:00
return generate_random_hieroglyph(k);
}
2024-03-21 01:30:47 +03:00
Hieroglyph {
syllable,
row,
column,
}
}