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
|
|
|
|
|
2024-03-26 14:11:46 +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 {
|
2024-03-26 14:11:46 +03:00
|
|
|
|
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;
|
2024-03-26 14:11:46 +03:00
|
|
|
|
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()
|
|
|
|
|
// }
|
2024-03-26 14:11:46 +03:00
|
|
|
|
fn get_kana(&self) -> Kanas {
|
2024-03-25 05:20:18 +03:00
|
|
|
|
for row in ROMAJI {
|
2024-03-26 14:11:46 +03:00
|
|
|
|
if row.contains(&(self.syllable).as_str()) {
|
|
|
|
|
return Kanas::Romaji;
|
|
|
|
|
}
|
2024-03-25 05:20:18 +03:00
|
|
|
|
}
|
|
|
|
|
for row in HIRAGANA {
|
2024-03-26 14:11:46 +03:00
|
|
|
|
if row.contains(&(self.syllable).as_str()) {
|
|
|
|
|
return Kanas::Hiragana;
|
|
|
|
|
}
|
2024-03-25 05:20:18 +03:00
|
|
|
|
}
|
|
|
|
|
for row in KATAKANA {
|
2024-03-26 14:11:46 +03:00
|
|
|
|
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,
|
|
|
|
|
}
|
|
|
|
|
}
|