From 804c632c108bebc793674b3040e4e23878869901 Mon Sep 17 00:00:00 2001 From: Aryadev Chavali Date: Thu, 16 Apr 2026 21:15:29 +0100 Subject: [PATCH] game:mode: A simple enum to represent the different stages of a round Specifically at the stage of the game where players may start presenting Items (i.e. a round), this enum allows us to validate if an Item should be played at that round stage: this is called a mode. --- src/game/mod.rs | 1 + src/game/mode.rs | 53 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 src/game/mode.rs diff --git a/src/game/mod.rs b/src/game/mod.rs index 61e02e3..8f7ddd2 100644 --- a/src/game/mod.rs +++ b/src/game/mod.rs @@ -1,2 +1,3 @@ pub mod deck; +pub mod mode; pub mod playerbuilder; diff --git a/src/game/mode.rs b/src/game/mode.rs new file mode 100644 index 0000000..204188b --- /dev/null +++ b/src/game/mode.rs @@ -0,0 +1,53 @@ +use crate::{ + card::Card, + modes::item::{Item, ItemParseError}, +}; + +#[derive(Debug, Clone, PartialEq, Eq)] +/// All possible stages of a Round where an Item may be presented by a player, +/// distinguished by Mode (Single, Pair, Triples, etc). `Any` represents the +/// "decision" stage where a single player may play any Item of any of the +/// available Round Modes to start off the new round. +pub enum Mode { + Any, + Single, + Pair, + Triple, +} + +#[derive(Debug, Clone, PartialEq, Eq)] +/// Types of errors which may occur when validating some set of cards against a +/// Mode. +pub enum ModeValidateError { + ParseError(ItemParseError), + ModeMismatch, +} + +impl Mode { + /// Given mode `self`, validate if `cards` creates a valid Item for that + /// mode. + /// Returns `Ok(Item)` if so, otherwise return `Err(ModeValidateError)`. + pub fn validate_cards( + &self, + cards: &[Card], + ) -> Result { + let item = Item::parse(cards).map_err(ModeValidateError::ParseError)?; + if *self == Mode::Any || *self == item.mode() { + Ok(item) + } else { + Err(ModeValidateError::ModeMismatch) + } + } +} + +impl Item { + /// Return the Mode we'd expect this Item to be played in - trivial enum + /// conversion. + fn mode(&self) -> Mode { + match self { + Self::Single(_) => Mode::Single, + Self::Pair(_) => Mode::Pair, + Self::Triple(_) => Mode::Triple, + } + } +}