rust_cards/hand.rs
1#![allow(dead_code)]
2use rand::thread_rng;
3use rand::seq::SliceRandom;
4
5//use crate::cards;
6use crate::cards::Card;
7use crate::cards::Suit;
8use crate::cards::Value;
9
10#[derive(Clone)]
11pub struct Hand {
12 pub vec: Vec<Card>
13}
14impl Hand {
15 /// returns a hand from a Vec of Card
16 pub fn from(vec: Vec<Card>) -> Hand {
17 Hand {
18 vec: vec
19 }
20 }
21
22 /// returns a Hand containing a full deck
23 ///
24 /// ordered 2, 3, 4, ... J, Q, K, A
25 /// Suits ordered Club(♣), Diamond(♦), Heart(♥), Spade(♠)
26 pub fn full_deck() -> Hand {
27 let mut h = vec![];
28
29 for suit_index in 0..4 {
30 for value_index in 0..13 {
31 h.push(Card::new(
32 Value::item_from_index(value_index),
33 Suit::item_from_index(suit_index)
34 ))
35 }
36 }
37
38 Hand::from(h)
39 }
40
41 ///Randomizes the order of the hand
42 ///
43 /// returns a new value; does not mutate the original value
44 pub fn shuffle(&self) -> Hand {
45 let mut rng = thread_rng();
46
47 let mut clone = self.vec.clone();
48 clone.shuffle(&mut rng);
49
50 Hand::from(
51 clone
52 )
53 }
54
55 /// reverses the hand
56 /// i.e. the first index becomes the last index, the second index becomes the second-last index, etc.Hand
57 ///
58 /// returns a new value; does not mutate the original value
59 pub fn reverse(&self) -> Hand {
60 let mut clone = self.vec.clone();
61 clone.reverse();
62
63 Hand::from(
64 clone
65 )
66 }
67
68 /// removes the first indexed card in the hand and returns its value
69 ///
70 /// mutates the hand; doesn't return a new hand
71 ///
72 /// ```
73 /// let mut h = Hand::from(
74 /// vec![
75 /// Card::new(Value::Ace, Suit::Spade)
76 /// Card::new(Value::Two, Suit::Spade)
77 /// Card::new(Value::Three, Suit::Spade)
78 /// ]
79 /// );
80 /// // A♠, 2♠, 3♠
81 ///
82 /// let popped = h.pop();
83 /// // h == 2♠, 3♠
84 /// // popped == A♠
85 /// ```
86 ///
87 pub fn pop_top(&mut self) -> Card {
88 self.vec.reverse();
89 let first_card = self.vec.pop().unwrap();
90 self.vec.reverse();
91 first_card
92 }
93
94 /// removes the last indexed card in the hand and returns its value
95 ///
96 /// mutates the hand; doesn't return a new hand
97 ///
98 /// ```
99 /// let mut h = Hand::from(
100 /// vec![
101 /// Card::new(Value::Ace, Suit::Spade)
102 /// Card::new(Value::Two, Suit::Spade)
103 /// Card::new(Value::Three, Suit::Spade)
104 /// ]
105 /// );
106 /// // A♠, 2♠, 3♠
107 ///
108 /// let popped = h.pop();
109 /// // h == A♠, 2♠
110 /// // popped == 3♠
111 /// ```
112 ///
113 pub fn pop_bottom(&mut self) -> Card {
114 self.vec.pop().unwrap()
115 }
116
117 /// Pushes a card to the top of the hand
118 /// ```
119 /// let mut h = Hand::from(
120 /// vec![
121 /// Card::new(Value::Ace, Suit::Spade)
122 /// Card::new(Value::Two, Suit::Spade)
123 /// Card::new(Value::Three, Suit::Spade)
124 /// ]
125 /// );
126 /// // A♠, 2♠, 3♠
127 ///
128 /// h = h.push(Card::new(Value::King, Suit::Spade))
129 /// // K♠, A♠, 2♠, 3♠
130 /// ```
131 pub fn push_top(&self, card: Card) -> Hand {
132 self.reverse().push_bottom(card).reverse()
133 }
134
135 /// Pushes a card to the bottom of the hand
136 /// ```
137 /// let mut h = Hand::from(
138 /// vec![
139 /// Card::new(Value::Ace, Suit::Spade)
140 /// Card::new(Value::Two, Suit::Spade)
141 /// Card::new(Value::Three, Suit::Spade)
142 /// ]
143 /// );
144 /// // A♠, 2♠, 3♠
145 ///
146 /// h = h.push(Card::new(Value::Four, Suit::Spade))
147 /// // A♠, 2♠, 3♠, 4♠
148 /// ```
149 pub fn push_bottom(&self, card: Card) -> Hand {
150 let mut clone = self.vec.clone();
151 clone.push(card);
152 Hand::from(
153 clone
154 )
155 }
156
157 pub fn insert(&self, index: usize, card: Card) -> Hand {
158 let mut clone = self.vec.clone();
159 clone.insert(index, card);
160
161 Hand::from(
162 clone
163 )
164 }
165
166 /// Stacks a hand on top of another Hand
167 ///
168 /// ```
169 ///
170 /// ```
171 pub fn stack_top(&self, hand: Hand) -> Hand {
172 let mut copy = self.clone();
173 for card in hand.reverse().vec {
174 copy = copy.push_top(card);
175 }
176
177 copy
178 }
179 pub fn stack_bottom(&self, hand: Hand) -> Hand {
180 let mut copy = self.clone();
181
182 for card in hand.vec {
183 copy = copy.push_bottom(card);
184 }
185
186 copy
187 }
188
189}