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;
const VOWELS: usize = 5;
const CONSONANTS: usize = 11;
const OBS: &str = "";
type KanaCharacters<'a> = [[&'a str; VOWELS]; CONSONANTS];
pub enum Kanas {
Romaji,
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 {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
@ -14,13 +24,17 @@ 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 {
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"],
["ka", "ki", "ku", "ke", "ko"],
["sa", "si", "su", "se", "so"],
@ -28,13 +42,13 @@ const ROMAJI: [[&str; 5]; 11] = [
["na", "ni", "nu", "ne", "no"],
["ha", "hi", "hu", "he", "ho"],
["ma", "mi", "mu", "me", "mo"],
["ya", "0", "yu", "0", "yo"],
["ya", OBS, "yu", OBS, "yo"],
["ra", "ri", "ru", "re", "ro"],
["wa", "0", "0", "0", "wo"],
["n", "0", "0", "0", "0"],
["wa", OBS, OBS, OBS, "wo"],
["n", OBS, OBS, OBS, OBS],
];
const HIRAGANA: [[&str; 5]; 11] = [
const HIRAGANA: KanaCharacters = [
["", "", "", "", ""],
["", "", "", "", ""],
["", "", "", "", ""],
@ -42,13 +56,13 @@ const HIRAGANA: [[&str; 5]; 11] = [
["", "", "", "", ""],
["", "", "", "", ""],
["", "", "", "", ""],
["", "0", "", "0", ""],
["", OBS, "", OBS, ""],
["", "", "", "", ""],
["", "0", "0", "0", ""],
["", "0", "0", "0", "0"],
["", OBS, OBS, OBS, ""],
["", OBS, OBS, OBS, OBS],
];
const KATAKANA: [[&str; 5]; 11] = [
const KATAKANA: KanaCharacters = [
["", "", "", "", ""],
["", "", "", "", ""],
["", "", "", "", ""],
@ -56,44 +70,40 @@ const KATAKANA: [[&str; 5]; 11] = [
["", "", "", "", ""],
["", "", "", "", ""],
["", "", "", "", ""],
["", "0", "", "0", ""],
["", OBS, "", OBS, ""],
["", "", "", "", ""],
["", "0" , "0", "0", ""],
["", "0", "0", "0", "0"],
["", OBS, OBS, OBS, ""],
["", 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 {
Kanas::Hiragana => Hieroglyph {
syllable: HIRAGANA[h.row][h.column].to_string(),
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
},
Kanas::Katakana => Hieroglyph {
syllable: KATAKANA[h.row][h.column].to_string(),
row: h.row,
column: h.column
},
Kanas::Romaji => Hieroglyph {
syllable: ROMAJI[h.row][h.column].to_string(),
row: h.row,
column: h.column
},
column: h.column,
}
}
pub fn generate_random_hieroglyph(k: Kanas) -> Hieroglyph {
let row: usize = rand::thread_rng().gen_range(0..ROMAJI.len()-1);
let column: usize = rand::thread_rng().gen_range(0..ROMAJI[0].len()-1);
let row: usize = rand::thread_rng().gen_range(0..CONSONANTS);
let column: usize = rand::thread_rng().gen_range(0..VOWELS);
let generated: String = match k {
Kanas::Romaji => ROMAJI[row][column].to_string(),
Kanas::Hiragana => HIRAGANA[row][column].to_string(),
Kanas::Katakana => KATAKANA[row][column].to_string()
};
let syllable: String = get_characters_for_kana(&k)[row][column].to_string();
if generated == "0" {
if syllable == OBS {
return generate_random_hieroglyph(k);
}
Hieroglyph { syllable: generated, row, column}
Hieroglyph {
syllable,
row,
column,
}
}