small improvements

This commit is contained in:
leca 2024-03-21 01:30:47 +03:00
parent a42744434d
commit 2854f41437
1 changed files with 55 additions and 45 deletions

View File

@ -1,12 +1,22 @@
use rand::Rng; use rand::Rng;
const VOWELS: usize = 5;
const CONSONANTS: usize = 11;
const OBS: &str = "";
type KanaCharacters<'a> = [[&'a str; VOWELS]; CONSONANTS];
pub enum Kanas { pub enum Kanas {
Romaji, Romaji,
Hiragana, Hiragana,
Katakana Katakana,
} }
pub struct Hieroglyph {syllable: String, row: usize, column: usize} pub struct Hieroglyph {
syllable: String,
row: usize,
column: usize,
}
impl std::fmt::Display for Hieroglyph { impl std::fmt::Display for Hieroglyph {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
@ -14,86 +24,86 @@ impl std::fmt::Display for Hieroglyph {
} }
} }
impl std::fmt::Debug for Hieroglyph{ impl std::fmt::Debug for Hieroglyph {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Hieroglyph: {} => ({}, {})", self.syllable, self.row, self.column) write!(
f,
"Hieroglyph: {} => ({}, {})",
self.syllable, self.row, self.column
)
} }
} }
const ROMAJI: [[&str; 5]; 11] = [ const ROMAJI: KanaCharacters = [
["a", "i", "u", "e", "o"], ["a", "i", "u", "e", "o"],
["ka", "ki", "ku", "ke", "ko"], ["ka", "ki", "ku", "ke", "ko"],
["sa", "si", "su", "se", "so"], ["sa", "si", "su", "se", "so"],
["ta", "ti", "tu", "te", "to"], ["ta", "ti", "tu", "te", "to"],
["na", "ni", "nu", "ne", "no"], ["na", "ni", "nu", "ne", "no"],
["ha", "hi", "hu", "he", "ho"], ["ha", "hi", "hu", "he", "ho"],
["ma", "mi", "mu", "me", "mo"], ["ma", "mi", "mu", "me", "mo"],
["ya", "0", "yu", "0", "yo"], ["ya", OBS, "yu", OBS, "yo"],
["ra", "ri", "ru", "re", "ro"], ["ra", "ri", "ru", "re", "ro"],
["wa", "0", "0", "0", "wo"], ["wa", OBS, OBS, OBS, "wo"],
["n", "0", "0", "0", "0"], ["n", OBS, OBS, OBS, OBS],
]; ];
const HIRAGANA: [[&str; 5]; 11] = [ const HIRAGANA: KanaCharacters = [
["", "", "", "", ""], ["", "", "", "", ""],
["", "", "", "", ""], ["", "", "", "", ""],
["", "", "", "", ""], ["", "", "", "", ""],
["", "", "", "", ""], ["", "", "", "", ""],
["", "", "", "", ""], ["", "", "", "", ""],
["", "", "", "", ""], ["", "", "", "", ""],
["", "", "", "", ""], ["", "", "", "", ""],
["", "0", "", "0", ""], ["", OBS, "", OBS, ""],
["", "", "", "", ""], ["", "", "", "", ""],
["", "0", "0", "0", ""], ["", OBS, OBS, OBS, ""],
["", "0", "0", "0", "0"], ["", OBS, OBS, OBS, OBS],
]; ];
const KATAKANA: [[&str; 5]; 11] = [ const KATAKANA: KanaCharacters = [
["", "", "", "", ""], ["", "", "", "", ""],
["", "", "", "", ""], ["", "", "", "", ""],
["", "", "", "", ""], ["", "", "", "", ""],
["", "", "", "", ""], ["", "", "", "", ""],
["", "", "", "", ""], ["", "", "", "", ""],
["", "", "", "", ""], ["", "", "", "", ""],
["", "", "", "", ""], ["", "", "", "", ""],
["", "0", "", "0", ""], ["", OBS, "", OBS, ""],
["", "", "", "", ""], ["", "", "", "", ""],
["", "0" , "0", "0", ""], ["", OBS, OBS, OBS, ""],
["", "0", "0", "0", "0"], ["", OBS, OBS, OBS, OBS],
]; ];
pub fn get_kana_pair_for_hieroglyph(k: Kanas, h: &Hieroglyph) -> Hieroglyph { fn get_characters_for_kana(k: &Kanas) -> KanaCharacters {
match k { match k {
Kanas::Hiragana => Hieroglyph { Kanas::Hiragana => HIRAGANA,
syllable: HIRAGANA[h.row][h.column].to_string(), Kanas::Katakana => KATAKANA,
row: h.row, Kanas::Romaji => ROMAJI,
column: h.column }
}, }
Kanas::Katakana => Hieroglyph {
syllable: KATAKANA[h.row][h.column].to_string(), pub fn get_kana_pair_for_hieroglyph(k: Kanas, h: &Hieroglyph) -> Hieroglyph {
row: h.row, Hieroglyph {
column: h.column syllable: get_characters_for_kana(&k)[h.row][h.column].to_string(),
}, row: h.row,
Kanas::Romaji => Hieroglyph { column: h.column,
syllable: ROMAJI[h.row][h.column].to_string(),
row: h.row,
column: h.column
},
} }
} }
pub fn generate_random_hieroglyph(k: Kanas) -> Hieroglyph { pub fn generate_random_hieroglyph(k: Kanas) -> Hieroglyph {
let row: usize = rand::thread_rng().gen_range(0..ROMAJI.len()-1); let row: usize = rand::thread_rng().gen_range(0..CONSONANTS);
let column: usize = rand::thread_rng().gen_range(0..ROMAJI[0].len()-1); let column: usize = rand::thread_rng().gen_range(0..VOWELS);
let generated: String = match k { let syllable: String = get_characters_for_kana(&k)[row][column].to_string();
Kanas::Romaji => ROMAJI[row][column].to_string(),
Kanas::Hiragana => HIRAGANA[row][column].to_string(),
Kanas::Katakana => KATAKANA[row][column].to_string()
};
if generated == "0" { if syllable == OBS {
return generate_random_hieroglyph(k); return generate_random_hieroglyph(k);
} }
Hieroglyph { syllable: generated, row, column} Hieroglyph {
} syllable,
row,
column,
}
}