diff options
author | Kjetil Orbekk <kj@orbekk.com> | 2022-09-04 17:22:36 -0400 |
---|---|---|
committer | Kjetil Orbekk <kj@orbekk.com> | 2022-09-04 17:22:36 -0400 |
commit | b7180143745c5ebe571936b0116cca403d082e8d (patch) | |
tree | f61ba100b46e7aca1f05d082706c81be95fb2990 /webapp/src/main.rs | |
parent | 2f310f54829cb368f407373eb95cd4512992ba9e (diff) |
Display a full deck of shuffled cards
Diffstat (limited to 'webapp/src/main.rs')
-rw-r--r-- | webapp/src/main.rs | 67 |
1 files changed, 45 insertions, 22 deletions
diff --git a/webapp/src/main.rs b/webapp/src/main.rs index ed84935..a99ea94 100644 --- a/webapp/src/main.rs +++ b/webapp/src/main.rs @@ -12,35 +12,41 @@ fn main() { #[function_component(App)] pub fn app() -> Html { - let hand = use_state(|| HandProps { - cards: vec![ - CardProps { - suit: Suit::Club, - rank: Rank::Two, - }, - CardProps { - suit: Suit::Club, - rank: Rank::Ace, - }, - ], - }); + let mut rng = rand::thread_rng(); + let (n, w, s, e) = card::shuffle_deck(&mut rng); + + let north = use_state(|| HandProps::from_iter(n.into_iter())); + let west = use_state(|| HandProps::from_iter(w.into_iter())); + let south = use_state(|| HandProps::from_iter(s.into_iter())); + let east = use_state(|| HandProps::from_iter(e.into_iter())); + + let shuffle = { + let north = north.clone(); + let west = west.clone(); + let south = south.clone(); + let east = east.clone(); - let onclick = { - let hand = hand.clone(); Callback::from(move |_| { - let mut cards = hand.cards.clone(); - cards.push(CardProps { - suit: Suit::Heart, - rank: Rank::Three, - }); - hand.set(HandProps { cards }) + let mut rng = rand::thread_rng(); + let (n, w, s, e) = card::shuffle_deck(&mut rng); + north.set(n.into_iter().collect()); + west.set(w.into_iter().collect()); + south.set(s.into_iter().collect()); + east.set(e.into_iter().collect()); }) }; html! { <> - <Hand ..(*hand).clone() /> - <button {onclick}>{ "Add card" }</button> + <p>{ "North" }</p> + <Hand ..(*north).clone() /> + <p>{ "West" }</p> + <Hand ..(*west).clone() /> + <p>{ "South" }</p> + <Hand ..(*south).clone() /> + <p>{ "East" }</p> + <Hand ..(*east).clone() /> + <button onclick={shuffle}>{ "Shuffle" }</button> </> } } @@ -79,6 +85,17 @@ pub struct HandProps { cards: Vec<CardProps>, } +impl<C: Into<CardProps>> FromIterator<C> for HandProps { + fn from_iter<Cards>(cards: Cards) -> Self + where + Cards: std::iter::IntoIterator<Item = C>, + { + HandProps { + cards: cards.into_iter().map(Into::into).collect(), + } + } +} + #[function_component(Card)] pub fn wcard(props: &CardProps) -> Html { html! { @@ -95,3 +112,9 @@ pub struct CardProps { suit: Suit, rank: Rank, } + +impl From<card::Card> for CardProps { + fn from(card::Card(suit, rank): card::Card) -> Self { + CardProps { suit, rank } + } +} |