use std::ops::{Index, RangeTo, RangeFrom, RangeFull};
use core::card::Card;
use core::deck::Deck;
extern crate rand;
use self::rand::{thread_rng, sample, Rng};
#[derive(Debug)]
pub struct FlatDeck {
cards: Vec<Card>,
}
impl FlatDeck {
pub fn len(&self) -> usize {
self.cards.len()
}
pub fn is_empty(&self) -> bool {
self.cards.is_empty()
}
pub fn sample(&self, n: usize) -> Vec<Card> {
let mut rng = thread_rng();
sample(&mut rng, &self.cards[..], n)
.iter()
.map(|c| *(*c))
.collect()
}
pub fn shuffle(&mut self) {
let mut rng = thread_rng();
rng.shuffle(&mut self.cards)
}
pub fn deal(&mut self) -> Option<Card> {
self.cards.pop()
}
}
impl Index<usize> for FlatDeck {
type Output = Card;
fn index(&self, index: usize) -> &Card {
&self.cards[index]
}
}
impl Index<RangeTo<usize>> for FlatDeck {
type Output = [Card];
fn index(&self, index: RangeTo<usize>) -> &[Card] {
&self.cards[index]
}
}
impl Index<RangeFrom<usize>> for FlatDeck {
type Output = [Card];
fn index(&self, index: RangeFrom<usize>) -> &[Card] {
&self.cards[index]
}
}
impl Index<RangeFull> for FlatDeck {
type Output = [Card];
fn index(&self, index: RangeFull) -> &[Card] {
&self.cards[index]
}
}
pub trait Flattenable {
fn flatten(self) -> FlatDeck;
}
impl Flattenable for Deck {
fn flatten(self) -> FlatDeck {
FlatDeck { cards: self.into_iter().collect() }
}
}
impl Into<FlatDeck> for Deck {
fn into(self) -> FlatDeck {
self.flatten()
}
}