From aed804863b14984f735cb2c5f3bc9d947fea5390 Mon Sep 17 00:00:00 2001 From: Elijah Voigt Date: Sat, 7 Dec 2024 22:37:44 -0800 Subject: [PATCH] Simple deck shuffling --- src/deck.rs | 26 +++++++++++++++++++++++--- src/setup.rs | 2 +- todo.txt | 8 ++++++++ 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/deck.rs b/src/deck.rs index d14b230..9bc328a 100644 --- a/src/deck.rs +++ b/src/deck.rs @@ -1,4 +1,7 @@ -use bevy::{prelude::*, utils::HashMap}; +use bevy::{ + prelude::*, + utils::{HashMap, RandomState}, +}; /// Deck and Cards pub struct DeckPlugin; @@ -16,7 +19,7 @@ pub(crate) struct Deck { } impl Deck { - pub(crate) fn iter_cards() -> impl Iterator { + pub(crate) fn cards() -> Vec { let mut v = Vec::new(); [ItemColor::Red, ItemColor::Green, ItemColor::Purple] @@ -41,8 +44,25 @@ impl Deck { }); }); }); + v + } + + pub(crate) fn iter_cards() -> impl Iterator { + Self::cards().into_iter() + } - v.into_iter() + pub(crate) fn shuffled() -> impl Iterator { + let rs = RandomState::new(); + let mut base = Self::cards(); + let len = base.len(); + (1..len).into_iter().for_each(|i| { + let a = rs.hash_one(base[i]) % (len as u64); + let b = rs.hash_one(base[i - 1]) % (len as u64); + if a > b { + base.swap(a as usize, b as usize); + } + }); + base.into_iter() } } diff --git a/src/setup.rs b/src/setup.rs index 1fa3873..7cc3618 100644 --- a/src/setup.rs +++ b/src/setup.rs @@ -21,7 +21,7 @@ impl Plugin for SetupPlugin { /// Setup drawing our cards on the screen pub(crate) fn setup_cards(mut commands: Commands, deck: Res) { let size = 75.0; - Deck::iter_cards().enumerate().for_each(|(i, this_card)| { + Deck::shuffled().enumerate().for_each(|(i, this_card)| { let this = deck .cards .get(&this_card) diff --git a/todo.txt b/todo.txt index f179baf..2188e0b 100644 --- a/todo.txt +++ b/todo.txt @@ -4,3 +4,11 @@ TODO: * Shuffle deck * Serve out 12 cards * Deal out additional cards after set captured +* Reset game (click new game button) +* Track score (number of sets) +* Make button(s) look pretty +* Make buttons react to hover/click! +* Music! +* Background! +* Print "no set"/"too many cards" messages +* Add "quit game" button