learn-hieroglyphs/src/game.rs

203 lines
5.4 KiB
Rust
Raw Normal View History

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
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 {
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-04-01 15:42:50 +03:00
impl Hieroglyph {
pub fn get_kana(&self) -> Kanas {
2024-03-25 05:20:18 +03:00
for row in ROMAJI {
if row.contains(&(self.syllable).as_str()) {
return Kanas::Romaji;
}
2024-03-25 05:20:18 +03:00
}
for row in HIRAGANA {
if row.contains(&(self.syllable).as_str()) {
return Kanas::Hiragana;
}
2024-03-25 05:20:18 +03:00
}
for row in KATAKANA {
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-04-02 15:32:43 +03:00
["", "", "", "", ""],
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,
}
}
2024-04-01 15:42:50 +03:00
pub fn random_kana(hiragana: bool, katakana: bool) -> Hieroglyph {
if hiragana && katakana {
generate_random_hieroglyph(if rand::thread_rng().gen_bool(0.5) {
Kanas::Katakana
} else {
Kanas::Hiragana
})
} else {
generate_random_hieroglyph(if hiragana {
Kanas::Hiragana
} else {
Kanas::Katakana
})
}
}
pub fn ask(ktr: bool, rtk: bool, hiragana: bool, katakana: bool) -> Hieroglyph {
if ktr && rtk {
return if rand::thread_rng().gen_bool(0.5) {
random_kana(hiragana, katakana)
} else {
generate_random_hieroglyph(Kanas::Romaji)
};
} else if ktr {
return random_kana(hiragana, katakana);
} else {
return generate_random_hieroglyph(Kanas::Romaji);
}
}
pub fn generate_question_text(question: &Hieroglyph, exact_kana: &Kanas) -> String {
return String::from(match question.get_kana() {
Kanas::Romaji => match exact_kana {
Kanas::Hiragana => {
format!("Name corresponding hiragana for {}", question.to_string())
}
Kanas::Katakana => {
format!("Name corresponding katakana for {}", question.to_string())
}
_ => {
panic!("HOW DID YOU GET HERE!?");
}
},
Kanas::Hiragana => {
format!(
"Name corresponding romaji for hiragana {}",
question.to_string()
)
}
Kanas::Katakana => {
format!(
"Name corresponding romaji for katakana {}",
question.to_string()
)
}
});
2024-04-02 15:32:43 +03:00
}