player cannot ask for a card he dont have

This commit is contained in:
Victor Martinez Montane 2021-08-28 19:15:01 +02:00
commit 43d49ff71f
2 changed files with 6 additions and 4 deletions

View file

@ -105,7 +105,7 @@ impl Game {
if !self.can_ask(index) { if !self.can_ask(index) {
return Err(CannotAsk(player_id.clone()).into()); return Err(CannotAsk(player_id.clone()).into());
} }
if !self.is_valid_question(to, card) { if !self.is_valid_question(index, to, card) {
return Err(InvalidQuestion(to, card).into()); return Err(InvalidQuestion(to, card).into());
} }
let cards = self.take_cards_from(to, card); let cards = self.take_cards_from(to, card);
@ -211,8 +211,10 @@ impl Game {
self.state == GameState::Asking(index) self.state == GameState::Asking(index)
} }
fn is_valid_question(&self, to: usize, card: u8) -> bool { fn is_valid_question(&self, from: usize, to: usize, card: u8) -> bool {
self.is_valid_player_index(to) && Deck::valid_card(card) self.is_valid_player_index(to)
&& Deck::valid_card(card)
&& self.players[from].cards.iter().any(|&c| c == card)
} }
fn is_valid_player_index(&self, to: usize) -> bool { fn is_valid_player_index(&self, to: usize) -> bool {

View file

@ -2,7 +2,7 @@ use thiserror::Error;
#[derive(Error, Debug)] #[derive(Error, Debug)]
pub enum ActionError { pub enum ActionError {
#[error("Invalid question, check the option you chose or the card number. \n Option: {0}, Card: {1}")] #[error("Invalid question, check the option you chose or the card number.\n\nOption: {0}, Card: {1}\n\nRemember: You can't ask for a card that you don't have!🤥")]
InvalidQuestion(usize, u8), InvalidQuestion(usize, u8),
#[error("There is no player with id {0}")] #[error("There is no player with id {0}")]
InvalidPlayerId(String), InvalidPlayerId(String),