first working version

This commit is contained in:
leca 2024-03-26 13:37:19 +03:00
parent 1ad28db03c
commit 41ec61d529
2 changed files with 58 additions and 22 deletions

View File

@ -5,13 +5,23 @@ const CONSONANTS: usize = 11;
const OBS: &str = ""; const OBS: &str = "";
type KanaCharacters<'a> = [[&'a str; VOWELS]; CONSONANTS]; type KanaCharacters<'a> = [[&'a str; VOWELS]; CONSONANTS];
#[derive(Clone)]
pub enum Kanas { pub enum Kanas {
Romaji, Romaji,
Hiragana, Hiragana,
Katakana, Katakana,
} }
impl std::fmt::Display for Kanas{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", match self {
Kanas::Romaji => "Romaji",
Kanas::Hiragana => "Hiragana",
Kanas::Katakana => "Katakana"
})
}
}
pub struct Hieroglyph { pub struct Hieroglyph {
syllable: String, syllable: String,
row: usize, row: usize,
@ -109,7 +119,7 @@ fn get_characters_for_kana(k: &Kanas) -> KanaCharacters {
} }
} }
pub fn get_kana_pair_for_hieroglyph(k: Kanas, h: &Hieroglyph) -> Hieroglyph { pub fn get_kana_pair_for_hieroglyph(k: &Kanas, h: &Hieroglyph) -> Hieroglyph {
Hieroglyph { Hieroglyph {
syllable: get_characters_for_kana(&k)[h.row][h.column].to_string(), syllable: get_characters_for_kana(&k)[h.row][h.column].to_string(),
row: h.row, row: h.row,

View File

@ -3,9 +3,9 @@ mod labled_switch;
use gio::Settings; use gio::Settings;
use std::cell::{Cell, RefCell}; use std::cell::{Cell, RefCell};
use gtk::glib::{clone, closure_local}; use std::ops::Deref;
use gtk::{gio, prelude::*, Button, Entry, Label}; use gtk::{gio, prelude::*, Button, Entry, Label};
use gtk::{glib, Application, ApplicationWindow, Box}; use gtk::{glib::clone, glib, Application, ApplicationWindow, Box};
use crate::game::*; use crate::game::*;
use rand::prelude::*; use rand::prelude::*;
@ -14,12 +14,6 @@ const APP_ID: &str = "org.foxarmy.learn-hieroglyph";
fn main() -> glib::ExitCode { fn main() -> glib::ExitCode {
let app = Application::builder().application_id(APP_ID).build(); let app = Application::builder().application_id(APP_ID).build();
let syllable = generate_random_hieroglyph(Kanas::Romaji);
let hiragana = get_kana_pair_for_hieroglyph(Kanas::Hiragana, &syllable);
let katakana = get_kana_pair_for_hieroglyph(Kanas::Katakana, &syllable);
println!("{syllable} = {hiragana} = {katakana}");
app.connect_activate(build_ui); app.connect_activate(build_ui);
app.run() app.run()
@ -150,7 +144,7 @@ fn generate_question_text(question: & Hieroglyph, exact_kana: &Kanas) -> String
} }
fn build_game(w: &RefCell<ApplicationWindow>) -> Box { fn build_game(_w: &RefCell<ApplicationWindow>) -> Box {
let game: Box = Box::builder() let game: Box = Box::builder()
.orientation(gtk::Orientation::Vertical) .orientation(gtk::Orientation::Vertical)
.build(); .build();
@ -160,33 +154,65 @@ fn build_game(w: &RefCell<ApplicationWindow>) -> Box {
let entry: Entry = Entry::builder() let entry: Entry = Entry::builder()
.placeholder_text("Answer") .placeholder_text("Answer")
.build(); .build();
let mut counter_label: RefCell<Label> = RefCell::new(Label::builder()
.label("Correct|incorrect: 0|0")
.build()
);
let stats: RefCell<(u32, u32)> = RefCell::new((0, 0));
let settings: Settings = Settings::new(APP_ID); let settings: Settings = Settings::new(APP_ID);
let ktr: bool = settings.boolean("is-ktr-enabled"); let ktr: bool = settings.boolean("is-ktr-enabled");
let rtk: bool = settings.boolean("is-rtk-enabled"); let rtk: bool = settings.boolean("is-rtk-enabled");
let exact_kana: Kanas = if rand::thread_rng().gen_bool(0.5) { Kanas::Hiragana } else { Kanas:: Katakana };
let hiragana: bool = settings.boolean("is-hiragana-enabled"); let hiragana: bool = settings.boolean("is-hiragana-enabled");
let katakana: bool = settings.boolean("is-katakana-enabled"); let katakana: bool = settings.boolean("is-katakana-enabled");
let question: Hieroglyph = ask(ktr, rtk, hiragana, katakana); let question: RefCell<Hieroglyph> = RefCell::new(ask(ktr, rtk, hiragana, katakana));
let exact_kana: RefCell<Kanas> = RefCell::new(if rand::thread_rng().gen_bool(0.5) { Kanas::Hiragana } else { Kanas:: Katakana });
let correct_answer: RefCell<Hieroglyph> = RefCell::new(get_kana_pair_for_hieroglyph(
match question.borrow().get_kana() {
Kanas::Romaji => match exact_kana.borrow().deref() {
Kanas::Hiragana => &Kanas::Hiragana,
Kanas::Katakana => &Kanas::Katakana,
_ => panic!("HOW DID YOU GET HERE?!")
},
_ => &Kanas::Romaji
},
&question.borrow()
));
let binding = generate_question_text(&question, &exact_kana); let binding = generate_question_text(&question.borrow(), exact_kana.borrow().deref());
let text: &str = &(binding.as_str());
question_label.get_mut().set_label(text);
entry.connect_activate(clone!( @strong question_label => move |entry: &Entry| {
let question: Hieroglyph = ask(ktr, rtk, hiragana, katakana);
let exact_kana: Kanas = if rand::thread_rng().gen_bool(0.5) { Kanas::Hiragana } else { Kanas:: Katakana };
let binding = generate_question_text(&question, &exact_kana);
let text: &str = &(binding.as_str()); let text: &str = &(binding.as_str());
question_label.borrow_mut().set_label(text); question_label.borrow_mut().set_label(text);
}));
let iteration = clone!( @strong question_label, @strong counter_label, @strong stats, @strong exact_kana => move |entry: &Entry| {
let answer: &String = &entry.text().to_string();
println!("{} <-> {}? = {}", answer, correct_answer.borrow().to_string(), *answer == correct_answer.borrow().to_string());
*stats.borrow_mut() = if *answer == correct_answer.borrow().to_string() { (stats.borrow().0 + 1, stats.borrow().1) } else {(stats.borrow().0, stats.borrow().1 + 1)};
counter_label.borrow_mut().set_label(format!("Correct|Incorrect: {}|{}", stats.borrow().0, stats.borrow().1).as_str());
*question.borrow_mut() = ask(ktr, rtk, hiragana, katakana);
*correct_answer.borrow_mut() = get_kana_pair_for_hieroglyph(
match question.borrow().get_kana() {
Kanas::Romaji => match exact_kana.borrow().deref() {
Kanas::Hiragana => &Kanas::Hiragana,
Kanas::Katakana => &Kanas::Katakana,
_ => panic!("HOW DID YOU GET HERE?!")
},
_ => &Kanas::Romaji
},
&question.borrow()
);
let binding = generate_question_text(&question.borrow(), exact_kana.borrow().deref());
let text: &str = &(binding.as_str());
question_label.borrow_mut().set_label(text);
entry.set_text("");
});
entry.connect_activate(iteration);
game.append(question_label.get_mut()); game.append(question_label.get_mut());
game.append(&entry); game.append(&entry);
game.append(counter_label.get_mut());
game game
} }