learn-hieroglyphs/src/game.rs

99 lines
3.3 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;
pub enum Kanas {
Romaji,
Hiragana,
Katakana
}
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 {
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
},
}
}
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 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()
}
};
if generated == "0" {
return generate_random_hieroglyph(k);
}
Hieroglyph { syllable: generated, row, column}
}