Implemented basic hieroglyphs

This commit is contained in:
leca 2024-03-11 22:37:13 +03:00
parent 5fd9e1e10b
commit d65978c891
2 changed files with 100 additions and 0 deletions

View File

@ -7,3 +7,4 @@ edition = "2021"
[dependencies] [dependencies]
gtk = { version = "0.8.1", package = "gtk4", features = ["v4_12"] } gtk = { version = "0.8.1", package = "gtk4", features = ["v4_12"] }
rand = "0.8.5"

99
src/game.rs Normal file
View File

@ -0,0 +1,99 @@
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}
}