From 9d44c5600a4656958a3c633e45e9b6181e66d6e2 Mon Sep 17 00:00:00 2001 From: kirbylife Date: Tue, 3 Sep 2024 13:57:53 -0600 Subject: [PATCH] Add valid letters checker --- src/gameslot.rs | 26 +++++++++++++++++++++----- src/main.rs | 10 +++------- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/src/gameslot.rs b/src/gameslot.rs index 9ca8a0d..fc59e04 100644 --- a/src/gameslot.rs +++ b/src/gameslot.rs @@ -1,7 +1,7 @@ use crate::attempt::Attempt; use crate::status::Status; use crate::words::get_word_of_the_day; -use std::collections::{HashMap, VecDeque}; +use std::collections::{HashMap, HashSet, VecDeque}; use uuid::Uuid; pub type Board = [Option; 6]; @@ -10,6 +10,7 @@ pub type Board = [Option; 6]; pub enum InsertionStatus { Ok, LengthError, + InvalidChar, GameOver, } @@ -44,11 +45,25 @@ impl Game { pub fn add_attempt(&mut self, word: &String) -> InsertionStatus { let word = word.to_uppercase(); + let valid_letters = { + let mut output: HashSet = ('A'..='Z').collect(); + output.insert('Ñ'); + output + }; let win = word.eq(&self.answer); + let has_valid_chars = word + .chars() + .collect::>() + .is_subset(&valid_letters); - let insertion_status = match (self.attempt_index, word.chars().count(), &self.status) { - (0..=5, 5, &GameStatus::Playing) => { + let insertion_status = match ( + self.attempt_index, + word.chars().count(), + &self.status, + has_valid_chars, + ) { + (0..=5, 5, &GameStatus::Playing, true) => { let new_attempt = Attempt::from_string(&word, &self.answer); self.board[self.attempt_index] = Some(new_attempt); self.attempt_index += 1; @@ -65,8 +80,9 @@ impl Game { (_, _) => InsertionStatus::Ok, } } - (0..=5, _, &GameStatus::Playing) => InsertionStatus::LengthError, - (_, _, &GameStatus::Win(_) | &GameStatus::Fail) => InsertionStatus::GameOver, + (0..=5, _, &GameStatus::Playing, true) => InsertionStatus::LengthError, + (0..=5, 5, &GameStatus::Playing, false) => InsertionStatus::InvalidChar, + (_, _, &GameStatus::Win(_) | &GameStatus::Fail, _) => InsertionStatus::GameOver, _ => todo!(), }; self.last_insertion_status = insertion_status.clone(); diff --git a/src/main.rs b/src/main.rs index 93a5990..a619bf5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -85,12 +85,6 @@ async fn main() -> Result<(), Box> { .mount("/", move |_| async { let game = new_game().await; windmark::response::Response::temporary_redirect(format!("/{}", game)) - // let mut context = tera::Context::new(); - - // let board: Board = Default::default(); - // context.insert("board", &board); - - // windmark::response::Response::success(render("index", &context).await.unwrap()) }) .mount("/:uuid", move |request| async move { let mut context = tera::Context::new(); @@ -109,6 +103,9 @@ async fn main() -> Result<(), Box> { GameStatus::Playing => { error = match game.last_insertion_status { InsertionStatus::Ok | InsertionStatus::GameOver => None, + InsertionStatus::InvalidChar => { + Some("Utiliza solo letras de la A la la Z incluyendo la Ñ".to_string()) + } InsertionStatus::LengthError => { Some("La palabra debe de ser de 5 letras".to_string()) } @@ -147,7 +144,6 @@ async fn main() -> Result<(), Box> { context.insert("board", &board); context.insert("error", &error); context.insert("result_pattern", &result_pattern); - // context.insert("error", "Partida no encontrada"); windmark::response::Response::success(render("index", &context).await.unwrap()) }) .mount("/:uuid/:attempt", move |request| async move {