From 2f310f54829cb368f407373eb95cd4512992ba9e Mon Sep 17 00:00:00 2001 From: Kjetil Orbekk Date: Sun, 4 Sep 2022 16:36:35 -0400 Subject: Extract card logic into a separate file --- Cargo.lock | 93 +++++++++++++++++++++++++++++++++++++++++++ webapp/Cargo.toml | 3 ++ webapp/src/card.rs | 113 +++++++++++++++++++++++++++++++++++++++++++++++++++++ webapp/src/main.rs | 95 ++++++-------------------------------------- 4 files changed, 222 insertions(+), 82 deletions(-) create mode 100644 webapp/src/card.rs diff --git a/Cargo.lock b/Cargo.lock index 4fbc561..0d60727 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -36,6 +36,17 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "getrandom" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4eb1a864a501629691edf6c15a593b7a51eebaa1e8468e9ddc623de7c9b58ec6" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + [[package]] name = "gloo" version = "0.4.2" @@ -151,6 +162,12 @@ version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" +[[package]] +name = "heck" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9" + [[package]] name = "indexmap" version = "1.9.1" @@ -182,6 +199,12 @@ version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" +[[package]] +name = "libc" +version = "0.2.132" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8371e4e5341c3a96db127eb2465ac681ced4c433e01dd0e938adbef26ba93ba5" + [[package]] name = "log" version = "0.4.17" @@ -197,6 +220,12 @@ version = "1.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2f7254b99e31cad77da24b08ebf628882739a608578bb1bcdfc1f9c21260d7c0" +[[package]] +name = "ppv-lite86" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872" + [[package]] name = "proc-macro-error" version = "1.0.4" @@ -239,6 +268,42 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d34f1408f55294453790c48b2f1ebbb1c5b4b7563eb1f418bcfcfdbb06ebb4e7" +dependencies = [ + "getrandom", +] + +[[package]] +name = "rustversion" +version = "1.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97477e48b4cf8603ad5f7aaf897467cf42ab4218a38ef76fb14c2d6773a6d6a8" + [[package]] name = "ryu" version = "1.0.0" @@ -291,6 +356,25 @@ dependencies = [ "autocfg", ] +[[package]] +name = "strum" +version = "0.24.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "063e6045c0e62079840579a7e47a355ae92f60eb74daaf156fb1e84ba164e63f" + +[[package]] +name = "strum_macros" +version = "0.24.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e385be0d24f186b4ce2f9982191e7101bb737312ad61c1f2f984f34bcf85d59" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "rustversion", + "syn", +] + [[package]] name = "syn" version = "1.0.99" @@ -334,6 +418,12 @@ version = "0.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + [[package]] name = "wasm-bindgen" version = "0.2.82" @@ -427,6 +517,9 @@ version = "0.1.0" dependencies = [ "console_error_panic_hook", "log", + "rand", + "strum", + "strum_macros", "wasm-logger", "yew", ] diff --git a/webapp/Cargo.toml b/webapp/Cargo.toml index 9abb154..c179533 100644 --- a/webapp/Cargo.toml +++ b/webapp/Cargo.toml @@ -8,3 +8,6 @@ yew = "0.19" console_error_panic_hook = "0.1" wasm-logger = "0.2" log = "0.4" +strum = "0.24" +strum_macros = "0.24" +rand = "0.8" diff --git a/webapp/src/card.rs b/webapp/src/card.rs new file mode 100644 index 0000000..071f2d1 --- /dev/null +++ b/webapp/src/card.rs @@ -0,0 +1,113 @@ +use rand::prelude::SliceRandom; +use rand::Rng; +use std::fmt; +use strum::IntoEnumIterator; +use strum_macros::EnumIter; + +#[derive(PartialOrd, Ord, PartialEq, Eq, Clone, Copy, EnumIter)] +pub enum Suit { + Club, + Diamond, + Heart, + Spade, +} + +#[derive(PartialOrd, Ord, PartialEq, Eq, Clone, Copy, EnumIter)] +pub enum Rank { + Two = 2, + Three, + Four, + Five, + Six, + Seven, + Eight, + Nine, + Ten, + Jack, + Queen, + King, + Ace, +} + +impl fmt::Display for Suit { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> { + f.write_str(match self { + Suit::Club => "♣", + Suit::Diamond => "♢", + Suit::Heart => "♡", + Suit::Spade => "♠", + }) + } +} + +impl fmt::Debug for Suit { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> { + write!(f, "{}", self) + } +} + +impl fmt::Display for Rank { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> { + f.write_str(match self { + Rank::Ace => "A", + Rank::King => "K", + Rank::Queen => "Q", + Rank::Jack => "J", + Rank::Ten => "10", + Rank::Nine => "9", + Rank::Eight => "8", + Rank::Seven => "7", + Rank::Six => "6", + Rank::Five => "5", + Rank::Four => "4", + Rank::Three => "3", + Rank::Two => "2", + }) + } +} + +impl fmt::Debug for Rank { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> { + write!(f, "{}", self) + } +} + +#[derive(PartialEq, Eq, Clone, Copy)] +pub struct Card(Suit, Rank); + +impl fmt::Display for Card { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> { + let Card(suit, rank) = self; + write!(f, "{}{}", suit, rank) + } +} + +impl fmt::Debug for Card { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> { + write!(f, "{}", self) + } +} + +fn make_deck() -> Vec { + let mut result = vec![]; + for suit in Suit::iter() { + for rank in Rank::iter() { + result.push(Card(suit, rank)); + } + } + result +} + +pub fn shuffle_deck(rng: &mut R) -> (Vec, Vec, Vec, Vec) +where + R: Rng, +{ + let mut deck = make_deck(); + deck.shuffle(rng); + let mut deck = deck.iter(); + let n = deck.by_ref().take(13).cloned().collect(); + let w = deck.by_ref().take(13).cloned().collect(); + let s = deck.by_ref().take(13).cloned().collect(); + let e = deck.by_ref().take(13).cloned().collect(); + (n, w, s, e) +} diff --git a/webapp/src/main.rs b/webapp/src/main.rs index 2d01713..ed84935 100644 --- a/webapp/src/main.rs +++ b/webapp/src/main.rs @@ -1,7 +1,8 @@ +use crate::card::{Rank, Suit}; #[allow(unused_imports)] use log::{debug, error, info, warn}; -use std::fmt; use yew::prelude::*; +pub mod card; fn main() { std::panic::set_hook(Box::new(console_error_panic_hook::hook)); @@ -63,6 +64,15 @@ pub fn hand(props: &HandProps) -> Html { } } +pub fn suit_css_class(suit: Suit) -> &'static str { + match suit { + Suit::Club => "suit-club", + Suit::Diamond => "suit-diamond", + Suit::Heart => "suit-heart", + Suit::Spade => "suit-spade", + } +} + #[derive(Clone, Default, PartialEq, Properties)] pub struct HandProps { #[prop_or_default] @@ -70,10 +80,10 @@ pub struct HandProps { } #[function_component(Card)] -pub fn card(props: &CardProps) -> Html { +pub fn wcard(props: &CardProps) -> Html { html! {
-
+
{ props.rank }
@@ -85,82 +95,3 @@ pub struct CardProps { suit: Suit, rank: Rank, } - -#[derive(PartialOrd, Ord, PartialEq, Eq, Clone, Copy)] -pub enum Suit { - Club, - Diamond, - Heart, - Spade, -} - -#[derive(PartialOrd, Ord, PartialEq, Eq, Clone, Copy)] -pub enum Rank { - Two = 2, - Three, - Four, - Five, - Six, - Seven, - Eight, - Nine, - Ten, - Jack, - Queen, - King, - Ace, -} - -impl Suit { - pub fn css_class(&self) -> &'static str { - match self { - Suit::Club => "suit-club", - Suit::Diamond => "suit-diamond", - Suit::Heart => "suit-heart", - Suit::Spade => "suit-spade", - } - } -} - -impl fmt::Display for Suit { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> { - f.write_str(match self { - Suit::Club => "♣", - Suit::Diamond => "♢", - Suit::Heart => "♡", - Suit::Spade => "♠", - }) - } -} - -impl fmt::Debug for Suit { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> { - write!(f, "{}", self) - } -} - -impl fmt::Display for Rank { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> { - f.write_str(match self { - Rank::Ace => "A", - Rank::King => "K", - Rank::Queen => "Q", - Rank::Jack => "J", - Rank::Ten => "10", - Rank::Nine => "9", - Rank::Eight => "8", - Rank::Seven => "7", - Rank::Six => "6", - Rank::Five => "5", - Rank::Four => "4", - Rank::Three => "3", - Rank::Two => "2", - }) - } -} - -impl fmt::Debug for Rank { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> { - write!(f, "{}", self) - } -} -- cgit v1.2.3