diff options
| author | andromeda <andromeda@lenovo> | 2026-08-25 03:32:45 +0200 |
|---|---|---|
| committer | andromeda <andromeda@lenovo> | 2026-08-25 03:32:45 +0200 |
| commit | c0cd718e2d4ca24b427175ffaa602452a36c117c (patch) | |
| tree | 4b6f1bd4af4a6fc466f2b344a0daae8f100d75f5 /src | |
Diffstat (limited to 'src')
| -rw-r--r-- | src/lib.rs | 72 | ||||
| -rw-r--r-- | src/main.rs | 15 |
2 files changed, 87 insertions, 0 deletions
diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..e8f7511 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,72 @@ +#![feature(random)] +use std::random::random; + +#[derive(Debug, Clone)] +pub enum Suit { + Spades, + Hearts, + Clubs, + Diamonds, +} + +#[derive(Debug, Clone)] +pub enum Value { + Ace, + Two, + Three, + Four, + Five, + Six, + Seven, + Eight, + Nine, + Ten, + Jack, + Queen, + King, +} + +#[derive(Debug, Clone)] +pub struct Card { + pub suit: Suit, + pub value: Value, +} + +impl Card { + pub fn new(suit: Suit, value: Value) -> Self { + Card { + suit: suit, + value: value, + } + } +} + +#[derive(Debug)] +pub struct Stack(Vec<Card>); + +impl Stack { + pub fn new(cards: Vec<Card>) -> Self { + Stack(cards) + } + pub fn shuffle(&mut self) { + let n: usize = self.0.len(); + if n == 0 { + return; + } + for i in 0..(n - 1) { + let j = random::<usize>(..) % (n - i) + i; + self.0.swap(i, j); + } + } + pub fn shuffle_in(&mut self, stack: &mut Stack) { + stack.take_cards(&mut self.0); + self.shuffle(); + } + pub fn draw(&mut self) -> Option<Card> { + self.0.iter().next().cloned(); + } + fn take_cards(&mut self, cards: &mut Vec<Card>) { + cards.append(&mut self.0.clone()); + self.0.clear(); + } +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..36018c4 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,15 @@ +use cards::{Card, Stack, Suit, Value}; + +fn main() { + let card = Card::new(Suit::Spades, Value::Ace); + let card0 = Card::new(Suit::Hearts, Value::Ace); + let card1 = Card::new(Suit::Clubs, Value::Ace); + let card2 = Card::new(Suit::Diamonds, Value::Ace); + let mut stack = Stack::new(vec![card, card0, card1, card2]); + stack.shuffle(); + loop { + if let Ok(c) = stack.next() { + println!("{:?}", c); + } + } +} |
