Simple deck shuffling

main
Elijah Voigt 2 years ago
parent 5afcb07dce
commit aed804863b

@ -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<Item = Card> {
pub(crate) fn cards() -> Vec<Card> {
let mut v = Vec::new();
[ItemColor::Red, ItemColor::Green, ItemColor::Purple]
@ -41,8 +44,25 @@ impl Deck {
});
});
});
v
}
v.into_iter()
pub(crate) fn iter_cards() -> impl Iterator<Item = Card> {
Self::cards().into_iter()
}
pub(crate) fn shuffled() -> impl Iterator<Item = Card> {
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()
}
}

@ -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<Deck>) {
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)

@ -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

Loading…
Cancel
Save