Add valid letters checker
parent
763d9507be
commit
9d44c5600a
|
@ -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<Attempt>; 6];
|
||||
|
@ -10,6 +10,7 @@ pub type Board = [Option<Attempt>; 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<char> = ('A'..='Z').collect();
|
||||
output.insert('Ñ');
|
||||
output
|
||||
};
|
||||
|
||||
let win = word.eq(&self.answer);
|
||||
let has_valid_chars = word
|
||||
.chars()
|
||||
.collect::<HashSet<char>>()
|
||||
.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();
|
||||
|
|
10
src/main.rs
10
src/main.rs
|
@ -85,12 +85,6 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||
.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<dyn std::error::Error>> {
|
|||
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<dyn std::error::Error>> {
|
|||
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 {
|
||||
|
|
Loading…
Reference in New Issue