use core::card::*;
use std::ops::Index;
use std::ops::{RangeFull, RangeTo, RangeFrom};
use std::slice::Iter;
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub struct Hand {
cards: Vec<Card>,
}
impl Hand {
pub fn default() -> Hand {
Hand { cards: Vec::with_capacity(5) }
}
pub fn new_with_cards(cards: Vec<Card>) -> Hand {
Hand { cards: cards }
}
pub fn push(&mut self, c: Card) {
self.cards.push(c);
}
pub fn len(&self) -> usize {
self.cards.len()
}
pub fn is_empty(&self) -> bool {
self.cards.is_empty()
}
pub fn iter(&self) -> Iter<Card> {
self.cards.iter()
}
}
impl Index<usize> for Hand {
type Output = Card;
fn index(&self, index: usize) -> &Card {
&self.cards[index]
}
}
impl Index<RangeFull> for Hand {
type Output = [Card];
fn index(&self, range: RangeFull) -> &[Card] {
&self.cards[range]
}
}
impl Index<RangeTo<usize>> for Hand {
type Output = [Card];
fn index(&self, index: RangeTo<usize>) -> &[Card] {
&self.cards[index]
}
}
impl Index<RangeFrom<usize>> for Hand {
type Output = [Card];
fn index(&self, index: RangeFrom<usize>) -> &[Card] {
&self.cards[index]
}
}
#[cfg(test)]
mod tests {
use super::*;
use core::card::Card;
#[test]
fn test_add_card() {
assert!(true);
let mut h = Hand::default();
let c = Card {
value: Value::Three,
suit: Suit::Spade,
};
h.push(c);
assert_eq!(1, h.len());
}
#[test]
fn test_index() {
let mut h = Hand::default();
h.push(Card {
value: Value::Four,
suit: Suit::Spade,
});
assert_eq!(Card {
value: Value::Four,
suit: Suit::Spade,
},
h[0]);
}
}