-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUNOCardPile.cpp
103 lines (91 loc) · 2.04 KB
/
UNOCardPile.cpp
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
#include <iostream>
#include <string>
#include <cstdlib>
#include <vector>
// So this knows what an UNOCard is.
#include "UNOCard.h"
using std::string;
// Before we define anything within an UNOCardPile....
#include "UNOCardPile.h"
UNOCardPile::UNOCardPile()
{
UNOCardPile(108);
}
UNOCardPile::UNOCardPile(int size)
{
UNOCardPile::cardArray.resize(size);
UNOCardPile::cardArraySize = size;
UNOCardPile::numberOfCards = 0;
// Make sure the random seed is set.
srand(time(NULL));
}
void UNOCardPile::shuffle( void )
{
// This method will eventually shuffle the deck.
// For now, we actually need to create an UNO deck.
UNOCard temp;
for (int i = 0; i < numberOfCards; i++)
{
swap(UNOCardPile::cardArray[i], UNOCardPile::cardArray[ rand() % numberOfCards ]);
}
}
/*
The following method will add a card to the card pile,
and return a 0 if adding the card was successful, or 1
if there was an error.
*/
int UNOCardPile::addCard(UNOCard card)
{
if (UNOCardPile::numberOfCards < UNOCardPile::cardArraySize)
{
UNOCardPile::cardArray[numberOfCards++] = card;
return 0; // success
}
else
{
return 1; // failure
}
}
UNOCard UNOCardPile::removeCard()
{
if ( (numberOfCards-1) <= 0)
{
/*
Nothing to return. We'll have to return the "empty" card.
*/
return UNOCard();
}
else
{
return cardArray[(numberOfCards--)-1];
}
}
string UNOCardPile::toString()
{
string output = "";
for (int i = 0; i < UNOCardPile::numberOfCards; i++)
{
// padding
if (i / 10 == 0)
{
output.append(" ");
}
if (i / 100 == 0)
{
output.append(" ");
}
// end padding
output.append(std::to_string(i) + ": " + UNOCardPile::cardArray[i].toString() + "\n");
}
return output;
}
/*
PRIVATE METHODS:
----------------
*/
void UNOCardPile::swap(UNOCard &x, UNOCard &y)
{
UNOCard temp = x;
x = y;
y = temp;
}