Implemented basic hieroglyphs
This commit is contained in:
		| @@ -7,3 +7,4 @@ edition = "2021" | ||||
|  | ||||
| [dependencies] | ||||
| gtk = { version = "0.8.1", package = "gtk4", features = ["v4_12"] } | ||||
| rand = "0.8.5" | ||||
|   | ||||
							
								
								
									
										99
									
								
								src/game.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										99
									
								
								src/game.rs
									
									
									
									
									
										Normal 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} | ||||
| } | ||||
		Reference in New Issue
	
	Block a user