learn-hieroglyphs/src/game.rs

153 lines
3.9 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

use rand::Rng;
const VOWELS: usize = 5;
const CONSONANTS: usize = 11;
const OBS: &str = "";
type KanaCharacters<'a> = [[&'a str; VOWELS]; CONSONANTS];
#[derive(Clone)]
pub enum Kanas {
Romaji,
Hiragana,
Katakana,
}
impl std::fmt::Display for Kanas {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}",
match self {
Kanas::Romaji => "Romaji",
Kanas::Hiragana => "Hiragana",
Kanas::Katakana => "Katakana",
}
)
}
}
pub struct Hieroglyph {
syllable: String,
row: usize,
column: usize,
}
pub trait Useful {
// fn get_syllable (&self) -> String;
fn get_kana(&self) -> Kanas;
}
impl Useful for Hieroglyph {
// fn get_syllable (&self) -> String {
// self.syllable.to_string()
// }
fn get_kana(&self) -> Kanas {
for row in ROMAJI {
if row.contains(&(self.syllable).as_str()) {
return Kanas::Romaji;
}
}
for row in HIRAGANA {
if row.contains(&(self.syllable).as_str()) {
return Kanas::Hiragana;
}
}
for row in KATAKANA {
if row.contains(&(self.syllable).as_str()) {
return Kanas::Katakana;
}
}
Kanas::Romaji // /shrug
}
}
impl std::fmt::Display for Hieroglyph {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.syllable)
}
}
impl std::fmt::Debug for Hieroglyph {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"Hieroglyph: {} => ({}, {})",
self.syllable, self.row, self.column
)
}
}
const ROMAJI: KanaCharacters = [
["a", "i", "u", "e", "o"],
["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"],
["ya", OBS, "yu", OBS, "yo"],
["ra", "ri", "ru", "re", "ro"],
["wa", OBS, OBS, OBS, "wo"],
["n", OBS, OBS, OBS, OBS],
];
const HIRAGANA: KanaCharacters = [
["", "", "", "", ""],
["", "", "", "", ""],
["", "", "", "", ""],
["", "", "", "", ""],
["", "", "", "", ""],
["", "", "", "", ""],
["", "", "", "", ""],
["", OBS, "", OBS, ""],
["", "", "", "", ""],
["", OBS, OBS, OBS, ""],
["", OBS, OBS, OBS, OBS],
];
const KATAKANA: KanaCharacters = [
["", "", "", "", ""],
["", "", "", "", ""],
["", "", "", "", ""],
["", "", "", "", ""],
["", "", "", "", ""],
["", "", "", "", ""],
["", "", "", "", ""],
["", OBS, "", OBS, ""],
["", "", "", "", ""],
["", OBS, OBS, OBS, ""],
["", OBS, OBS, OBS, OBS],
];
fn get_characters_for_kana(k: &Kanas) -> KanaCharacters {
match k {
Kanas::Hiragana => HIRAGANA,
Kanas::Katakana => KATAKANA,
Kanas::Romaji => ROMAJI,
}
}
pub fn get_kana_pair_for_hieroglyph(k: &Kanas, h: &Hieroglyph) -> Hieroglyph {
Hieroglyph {
syllable: get_characters_for_kana(&k)[h.row][h.column].to_string(),
row: h.row,
column: h.column,
}
}
pub fn generate_random_hieroglyph(k: Kanas) -> Hieroglyph {
let row: usize = rand::thread_rng().gen_range(0..CONSONANTS);
let column: usize = rand::thread_rng().gen_range(0..VOWELS);
let syllable: String = get_characters_for_kana(&k)[row][column].to_string();
if syllable == OBS {
return generate_random_hieroglyph(k);
}
Hieroglyph {
syllable,
row,
column,
}
}