#![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(); } }