From c0cd718e2d4ca24b427175ffaa602452a36c117c Mon Sep 17 00:00:00 2001 From: andromeda Date: Tue, 25 Aug 2026 03:32:45 +0200 Subject: init --- src/lib.rs | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 src/lib.rs (limited to 'src/lib.rs') 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); + +impl Stack { + pub fn new(cards: Vec) -> 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::(..) % (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 { + self.0.iter().next().cloned(); + } + fn take_cards(&mut self, cards: &mut Vec) { + cards.append(&mut self.0.clone()); + self.0.clear(); + } +} -- cgit v1.3.1