use std::fmt::{Display, Formatter, Result};
use std::ops::AddAssign;
use super::*;
pub struct Hand {
pub cards: Vec<Card>,
}
impl Display for Hand {
fn fmt(&self, f: &mut Formatter) -> Result {
let mut result = String::new();
for (i, card) in self.cards.iter().enumerate() {
result.push_str(&card.to_str());
if i < self.cards.len() - 1 {
result.push(',');
}
}
write!(f, "{}", result)
}
}
impl Clone for Hand {
fn clone(&self) -> Hand {
return Hand {
cards: self.cards.clone()
}
}
}
impl<'a> AddAssign<&'a Hand> for Hand {
fn add_assign(&mut self, rhs: &Hand) {
self.push_hand(rhs);
}
}
impl AddAssign<Card> for Hand {
fn add_assign(&mut self, rhs: Card) {
self.push_card(rhs);
}
}
impl Cards for Hand {
fn cards(&self) -> &[Card] {
self.cards.as_slice()
}
fn mut_cards(&mut self) -> &mut [Card] {
self.cards.as_mut_slice()
}
}
impl Hand {
pub fn new() -> Hand {
Hand { cards: Vec::new() }
}
pub fn from_hand(hand: &Hand) -> Hand {
Hand::from_cards(hand.cards())
}
pub fn from_cards(cards: &[Card]) -> Hand {
Hand { cards: Vec::from(cards) }
}
pub fn from_strings(card_slice: &[&str]) -> Hand {
let mut cards: Vec<Card> = Vec::with_capacity(card_slice.len());
for s in card_slice {
let card = card!(s);
cards.push(card);
}
Hand { cards: cards }
}
pub fn push_card(&mut self, card: Card) {
self.cards.push(card);
}
pub fn push_cards(&mut self, cards: &[Card]) {
self.cards.extend(cards);
}
pub fn push_hand(&mut self, other: &Hand) {
self.cards.extend(other.cards());
}
pub fn len(&self) -> usize {
self.cards.len()
}
pub fn clear(&mut self) {
self.cards.clear();
}
pub fn remove(&mut self, index: usize) -> Card {
self.cards.remove(index)
}
pub fn remove_cards(&mut self, cards: &[Card]) {
for c in cards {
self.remove_card(*c);
}
}
pub fn remove_card(&mut self, card: Card) {
let found = self.cards.iter().position(|c| *c == card);
if found.is_some() {
self.cards.remove(found.unwrap());
}
}
pub fn cards_of_rank(&self, rank: Rank) -> Vec<Card> {
cards_of_rank(&self.cards, rank)
}
pub fn cards_of_suit(&self, suit: Suit) -> Vec<Card> {
cards_of_suit(&self.cards, suit)
}
}