-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUNOPlayer.cpp
176 lines (141 loc) · 4.5 KB
/
UNOPlayer.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
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
#include "UNOPlayer.h"
UNOPlayer::UNOPlayer()
{
numberOfCards = 0;
hand = NULL;
}
void UNOPlayer::addCard(UNOCard card)
{
addCardToLL(card);
}
void UNOPlayer::addCardToLL(UNOCard card)
{
if (hand == NULL)
{
#ifdef DEBUG
std::cout << "DEBUG: This player currently has no cards, adding a card...\n";
#endif
/*
The hand is currently empty. This means we need to initialize a node.
*/
// hand will now point to a new node after this line.
hand = new Node;
// once this new node is created, set the previous to null, and the
// next one to null. There is only one card, after all.
(*hand).prevNode = NULL;
(*hand).nextNode = NULL;
(*hand).card = card;
// Finally, we'll increment the number of cards in this hand.
numberOfCards++;
}
else
{
#ifdef DEBUG
std::cout << "DEBUG: This player already has cards, adding a card...\n";
#endif
/*
There is at least one card already in the deck. We'll add to the beginning,
for the sake of simplicity.
*/
// *temp is going to put to what the hand currently represents.
Node * temp = hand;
// Then, we're going to create a new node.
hand = new Node;
(*hand).prevNode = NULL;
(*hand).nextNode = temp;
(*hand).card = card;
// Then, we'll update the temp so that it points to the new hand.
(*temp).prevNode = hand;
// don't touch the nextNode.
// don't touch the card.
// Finally, we'll increment the number of cards in this hand.
}
}
UNOCard UNOPlayer::removeCardFromLL(int indexToRemove)
{
int currentIndex = 0;
UNOCard cardToReturn;
Node * temp = hand;
// The first thing we'll do is check and see if there's a card in the hand at all.
if (temp == NULL)
{
// Uh-oh. That's not good. We're trying to remove from an empty hand.
// I'm going to throw an exception to the program.
#ifdef DEBUG
std::cout << "DEBUG: Removing from an empty deck... error!\n";
#endif
// This is the "empty" UNOCARD.
return UNOCard();
}
#ifdef DEBUG
std::cout << "DEBUG: Searching the linked list for the cards...\n";
#endif
// If we've reached this point, then there is at least one card.
while (temp != NULL)
{
#ifdef DEBUG
if (temp != NULL)
{
std::cout << (*temp).card.toString() << "\n";
}
#endif
if (currentIndex == indexToRemove)
{
// Adjust the pointers to the next one to be null.
cardToReturn = (*temp).card;
// Now, we're going to handle the case of which there's a previous node.
if ( (*temp).prevNode != NULL )
{
// There is a previous node, so we'll take that previous node value
// (even if it is NULL) and set it to the new value.
(*(*temp).prevNode).nextNode = (*temp).nextNode;
}
else
{
hand = (*temp).nextNode;
}
// Now, we're going to handle the case of which there's a next node.
if ( (*temp).nextNode != NULL )
{
(*(*temp).nextNode).prevNode = (*temp).prevNode;
}
numberOfCards--;
return cardToReturn;
}
temp = (*temp).nextNode;
currentIndex++;
};
#ifdef DEBUG
std::cout << "DEBUG: Could not find the card... error!\n";
#endif
// Returns the "empty" card as a signal that the card could not be found.
return UNOCard();
}
UNOCard UNOPlayer::removeCard(int indexToRemove)
{
return removeCardFromLL(indexToRemove);
}
string UNOPlayer::printHand()
{
int counter = 0;
string output = "";
Node * temp = hand;
if (temp == NULL)
{
return NULL;
}
while (temp != NULL)
{
// Append the number of the card.
output.append(std::to_string(counter) + ": ");
output.append((*temp).card.toString());
temp = (*temp).nextNode;
if (temp != NULL)
{
output.append(", ");
}
counter++;
}
output.append("\n");
return output;
}