Allow joining an existing room and validating tile placement
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
use std::error::Error;
|
||||
use crate::tile::Operator;
|
||||
use enum_map::EnumMap;
|
||||
use rand::{thread_rng, Rng};
|
||||
@@ -6,7 +7,14 @@ type DeckSize = u16;
|
||||
type DigitDeck = [DeckSize; 19];
|
||||
|
||||
/// When a deck is empty, new tiles cannot be retrieved.
|
||||
pub type EmptyDeckError = ();
|
||||
#[derive(Debug)]
|
||||
pub struct EmptyDeckError;
|
||||
impl Error for EmptyDeckError {}
|
||||
impl std::fmt::Display for EmptyDeckError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "Deck is empty")
|
||||
}
|
||||
}
|
||||
|
||||
/// A entire deck of tiles.
|
||||
#[derive(Debug, Clone, Default, PartialEq)]
|
||||
|
@@ -35,12 +35,20 @@ impl Hand {
|
||||
pub fn complete(&mut self, deck: &mut RngDeck) -> Result<(), EmptyDeckError> {
|
||||
for _ in 0..self.count_missing_operators() {
|
||||
self.tiles
|
||||
.push(Tile::Operator(deck.rand_operator().ok_or(())?));
|
||||
.push(Tile::Operator(deck.rand_operator().ok_or(EmptyDeckError.into())?));
|
||||
}
|
||||
for _ in 0..self.count_missing_numbers() {
|
||||
self.tiles
|
||||
.push(Tile::Digit(Digit::new(deck.rand_digit().ok_or(())?)));
|
||||
.push(Tile::Digit(Digit::new(deck.rand_digit().ok_or(EmptyDeckError.into())?)));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn remove(&mut self, idx: usize) -> Option<Tile> {
|
||||
if idx < self.tiles.len() {
|
||||
Some(self.tiles.remove(idx))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user