-
Notifications
You must be signed in to change notification settings - Fork 0
/
Deck.h
273 lines (239 loc) · 7.04 KB
/
Deck.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: Deck.h
* Author: r2d2
*
* Created on July 1, 2016, 9:01 PM
*/
#ifndef DECK_H
#define DECK_H
#include <list>
// ANTES DE QUE ME RE CAGUES A PEDOS AGUSTIN: Cuando trabajo con tamplates no puedo separar en .cpp y .h
//http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file
template <class CARD_T>
class Deck {
public:
Deck();
//virtual ~Deck();
unsigned int size();
void addcard_end(const CARD_T& cardtoadd);
void addcard_front(const CARD_T& cardtoadd);
bool draw(CARD_T& card_taken);
bool get_ncard(unsigned int place, CARD_T& card_taken);
bool draw_ncard(unsigned int place, CARD_T& card_taken); //starts from 0
bool draw_rand_card(CARD_T& card_taken);
bool remove_ncard(unsigned int place);
bool remove_specific_card(CARD_T card_to_remove);
protected:
Deck(const Deck& orig); //Too lazy not gonna use it, if I need it along the line I'll probably be pissed...
typename std::list<CARD_T> deck;
typename std::list<CARD_T>::iterator it;
};
template <class CARD_T>
Deck<CARD_T>::Deck() {
}
/*
template <class CARD_T>
Deck<CARD_T>::~Deck(){
}*/
template <class CARD_T>
unsigned int Deck<CARD_T>::size(){
return deck.size();
}
/*
* This function adds a card to the end of the deck, the card is received by reference.
*
* Input:
* -CARD_T &cardtoadd: Reference to CARD_T which will be added to the end of the deck.
*
* Return:
* -void
*
*/
template <class CARD_T>
void Deck<CARD_T>::addcard_end(const CARD_T& cardtoadd){
deck.push_back(cardtoadd);
}
/*
* This function adds a card to the front of the deck, the card is received by reference.
*
* Input:
* -CARD_T &cardtoadd: Reference to CARD_T which will be added to the end of the deck.
*
* Return:
* -void
*
*/
template <class CARD_T>
void Deck<CARD_T>::addcard_front(const CARD_T& cardtoadd){
deck.push_front(cardtoadd);
}
/*
* This function removes a specific card from the deck. The success of the action is returned with a boolean value.
* NOTE: CARD_T must have the == operator overloaded.
*
* Input:
* -CARD_T card_to_remove: Copy of the card to be removed, only removes the first copy of the card found.
*
* Return:
* -bool: Returns true if the card in the place was successfully removed from the deck.
*
*/
template <class CARD_T>
bool Deck<CARD_T>::remove_specific_card(CARD_T card_to_remove)
{
bool card_found = false;
if( deck.size() )
for(it = deck.begin() ; !card_found && it!= deck.end(); it++ )
if( (*it) == card_to_remove)
{
card_found = true;
deck.erase(it++);
}
return card_found;
}
/*
* This function draws(THE CARD IS REMOVED FROM THE DECK) a card in a specific position from the deck,
* the card is stored by reference and the success of the action is returned with a boolean value.
*
* Input:
* -Unsigned int place: Place of the card which will be removed. Starts from 0 (zero being the first card)
* -CARD_T &card_taken: Reference to CARD_T in which the card taken will be stored.
*
* Return:
* -bool: Returns true if the card in the place was successfully drawn from the deck.
*
*/
template <class CARD_T>
bool Deck<CARD_T>::draw_ncard(unsigned int place, CARD_T& card_taken)
{
bool grabbed_card = false;
if( deck.size() > place)
{
it = deck.begin();
std::advance(it,place);
card_taken = *it;
grabbed_card = true;
it = deck.erase(it++);
}
return grabbed_card;
}
/*
* This function gets(THE CARD IS NOT REMOVED FROM THE DECK) a card in a specific position from the deck,
* the card is stored by reference and the success of the action is returned with a boolean value.
*
* Input:
* -Unsigned int place: Place of the card which will be get. Starts from 0 (zero being the first card)
* -CARD_T &card_taken: Reference to CARD_T in which the card taken will be stored.
*
* Return:
* -bool: Returns true if the card in the place was successfully drawn from the deck.
*
*/
template <class CARD_T>
bool Deck<CARD_T>::get_ncard(unsigned int place, CARD_T& card_taken)
{
bool grabbed_card = false;
if( deck.size() > place)
{
it = deck.begin();
std::advance(it,place);
card_taken = *it;
grabbed_card = true;
}
return grabbed_card;
}
#include <stdlib.h>
/*
* This function draws a random card from the deck, the card is stored by reference and the success of
* the action is returned with a boolean value.
* Note: Uses rand() and deck size to pull the random card, don't forget to seed rand with srand(time(NULL))
*
* Input:
* -CARD_T &card_taken: Reference to CARD_T in which the card taken will be stored.
*
* Return:
* -bool: Returns true if a random card was successfully drawn from the deck
*
*/
template <class CARD_T>
bool Deck<CARD_T>::draw_rand_card(CARD_T& card_taken)
{
bool able_to_draw = false;
if(deck.size())
able_to_draw = draw_ncard(rand()%deck.size(), card_taken);
return able_to_draw;
}
/*
* This function removes a card in a specific place from the deck. DOESN'T return in any way the card value, and
* the success of the action is returned with a boolean value.
*
* Input:
* -Unsigned int place: Place of the card which will be removed. Starts from 0 (zero being the first card)
*
* Return:
* -bool: Returns true if the card in the place was successfully removed from the deck.
*
*/
template <class CARD_T>
bool Deck<CARD_T>::remove_ncard(unsigned int place)
{
bool erased_card = false;
if(deck.size() > place)
{
it = deck.begin();
std::advance(it,place);
erased_card = true;
it = deck.erase(it++);
}
return erased_card;
}
/*
* This function draws a card from the deck(top of deck), the card is stored by reference and the success of
* the action is returned with a boolean value.
*
* Input:
* -CARD_T &card_taken: Reference to CARD_T in which the card taken will be stored.
*
* Return:
* -bool: Returns true if a card(the top card) was successfully drawn from the deck
*
*/
template <class CARD_T>
bool Deck<CARD_T>::draw(CARD_T& card_taken)
{
bool grabbed_card = false;
if( deck.size())
{
grabbed_card = true;
card_taken = * deck.begin();
deck.pop_front();
}
return grabbed_card;
}
/* Prueba de funcionamiento cards + deck
int main(int argc, char** argv) {
Deck hanabideck;
std::cout << hanabideck.size() << std::endl;
hanabideck.addcard(Card(Card::BLUE,Card::ONE));
std::cout << hanabideck.size() << std::endl;
hanabideck.addcard(Card(Card::BLUE,Card::TWO));
std::cout << hanabideck.size() << std::endl;
hanabideck.addcard(Card(Card::BLUE,Card::THREE));
std::cout << hanabideck.size() << std::endl;
Card card1,card2,card3;
hanabideck.removecard(1);
hanabideck.getcard(1,card1);
std::cout << hanabideck.size() << std::endl;
hanabideck.drawcard(1,card2);
std::cout << hanabideck.size() << std::endl;
if(!hanabideck.drawcard(1,card3))
std::cout << "Unable to get card " <<std::endl;
std::cout << hanabideck.size() << std::endl;
return 0;
} */
#endif /* DECK_H */