Better random for quiz
This commit is contained in:
+1
-1
@@ -181,7 +181,7 @@ impl KanaSet {
|
|||||||
|
|
||||||
pub fn next(&mut self) -> (String, String) {
|
pub fn next(&mut self) -> (String, String) {
|
||||||
let current_set = self.list();
|
let current_set = self.list();
|
||||||
|
|
||||||
let mut rand = rand::rng();
|
let mut rand = rand::rng();
|
||||||
let index: u32 = rand.random();
|
let index: u32 = rand.random();
|
||||||
current_set[index as usize % current_set.len()].clone()
|
current_set[index as usize % current_set.len()].clone()
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
#![windows_subsystem = "windows"]
|
||||||
mod lang;
|
mod lang;
|
||||||
mod quiz;
|
mod quiz;
|
||||||
mod selector;
|
mod selector;
|
||||||
|
|||||||
+14
-1
@@ -3,6 +3,7 @@ use crate::Page::Back;
|
|||||||
use crate::{NavigatedPage, Page};
|
use crate::{NavigatedPage, Page};
|
||||||
use iced::widget::*;
|
use iced::widget::*;
|
||||||
use iced::{alignment, Element, Fill, Font};
|
use iced::{alignment, Element, Fill, Font};
|
||||||
|
use rand::seq::SliceRandom;
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct QuizState {
|
pub struct QuizState {
|
||||||
@@ -10,6 +11,7 @@ pub struct QuizState {
|
|||||||
current_roman: String,
|
current_roman: String,
|
||||||
correct_roman: String,
|
correct_roman: String,
|
||||||
pub(crate) set: KanaSet,
|
pub(crate) set: KanaSet,
|
||||||
|
queue: Vec<(String, String)>,
|
||||||
score: Score,
|
score: Score,
|
||||||
is_help: bool,
|
is_help: bool,
|
||||||
}
|
}
|
||||||
@@ -32,6 +34,7 @@ impl QuizState {
|
|||||||
is_help: false,
|
is_help: false,
|
||||||
current_roman: "".to_string(),
|
current_roman: "".to_string(),
|
||||||
set: KanaSet::hiragana(),
|
set: KanaSet::hiragana(),
|
||||||
|
queue: Vec::new(),
|
||||||
score: Score {
|
score: Score {
|
||||||
total: 0,
|
total: 0,
|
||||||
correct: 0,
|
correct: 0,
|
||||||
@@ -75,7 +78,17 @@ impl QuizState {
|
|||||||
|
|
||||||
fn update_showed(&mut self) {
|
fn update_showed(&mut self) {
|
||||||
self.current_roman = String::new();
|
self.current_roman = String::new();
|
||||||
let pair = self.set.next();
|
let queue = &mut self.queue;
|
||||||
|
|
||||||
|
if queue.is_empty() {
|
||||||
|
for pair in self.set.list() {
|
||||||
|
queue.push(pair);
|
||||||
|
}
|
||||||
|
|
||||||
|
queue.shuffle(&mut rand::rng());
|
||||||
|
}
|
||||||
|
|
||||||
|
let pair = self.queue.pop().unwrap();
|
||||||
self.kana = pair.0;
|
self.kana = pair.0;
|
||||||
self.correct_roman = pair.1;
|
self.correct_roman = pair.1;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user