learn-hieroglyphs/src/game.rs

99 lines
3.0 KiB
Rust
Raw Normal View History

2024-03-11 22:37:13 +03:00
use rand::Rng;
pub enum Kanas {
Romaji,
Hiragana,
Katakana
}
2024-03-13 01:05:02 +03:00
2024-03-11 22:37:13 +03:00
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 {
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: [[&str; 5]; 11] = [
["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", "0", "yu", "0", "yo"],
["ra", "ri", "ru", "re", "ro"],
["wa", "0", "0", "0", "wo"],
["n", "0", "0", "0", "0"],
];
const HIRAGANA: [[&str; 5]; 11] = [
["", "", "", "", ""],
["", "", "", "", ""],
["", "", "", "", ""],
["", "", "", "", ""],
["", "", "", "", ""],
["", "", "", "", ""],
["", "", "", "", ""],
["", "0", "", "0", ""],
["", "", "", "", ""],
["", "0", "0", "0", ""],
["", "0", "0", "0", "0"],
];
const KATAKANA: [[&str; 5]; 11] = [
["", "", "", "", ""],
["", "", "", "", ""],
["", "", "", "", ""],
["", "", "", "", ""],
["", "", "", "", ""],
["", "", "", "", ""],
["", "", "", "", ""],
["", "0", "", "0", ""],
["", "", "", "", ""],
["", "0" , "0", "0", ""],
["", "0", "0", "0", "0"],
];
pub fn get_kana_pair_for_hieroglyph(k: Kanas, h: &Hieroglyph) -> Hieroglyph {
match k {
2024-03-13 01:05:02 +03:00
Kanas::Hiragana => Hieroglyph {
syllable: HIRAGANA[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
},
2024-03-11 22:37:13 +03:00
}
}
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);
2024-03-13 01:05:02 +03:00
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()
2024-03-11 22:37:13 +03:00
};
2024-03-13 01:05:02 +03:00
2024-03-11 22:37:13 +03:00
if generated == "0" {
return generate_random_hieroglyph(k);
}
Hieroglyph { syllable: generated, row, column}
}