Help with a Blackjack Project

Hey Guys! I was hoping to get some help a project that is due soon. Basically I just need to turn convert these 2 classes into linked lists. I'm short on time so if someones able to do one can you show me what you changed so that I can do the other. Thanks Guys!

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
Deck.h

  class deck                                                    //creates class deck
{
public:                                                       //lines 11 - 21 create public variables that can be used in other files
	deck();
	~deck();
	int getPlaceInDeck();

	void setPlaceInDeck(int aPlace);

	void getDeck(card aDeck[], int deckSize);
	void showDeck();
	void shuffleDeck();
	card dealCard();


private:                                                     //lines 24 - 27 create private variables that can only be used within the header file
	const int numberOfCards = 52;
	card cardDeck[52];
	int placeInDeck = 0;
};

Deck.cpp
deck::deck()
{
	getDeck(cardDeck, numberOfCards);                                      // constuctor for getDeck to include card deck and number of cards before launching
}

deck::~deck()
{

}

int deck::getPlaceInDeck()                                               // sets code to return placeInDeck when getPlaceInDeck is used                                       
{
	return placeInDeck;
}

void deck::setPlaceInDeck(int aPlace)                                   // makes placeInDeck equal to aPlace and is what getPlaceInDeck will return
{
	placeInDeck = aPlace;
}

void deck::getDeck(card aDeck[], int deckSize)                         // lines 25 - 62 set up the card deck to include faces, suits, and values, as well as code to move forward through the deck
{
	string suits[] = { "Hearts", "Diamonds", "Clubs", "Spades" };
	int numberSuits = 4;
	string faces[] = { "Ace", "Deuce", "Trip", "Quad", "Fin", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" };
	int numFaces = 13;
	int aSuit = 0;
	int aFace = 0;

	for (int i = 0; i < deckSize; i++)
	{
		aDeck[i].setFace(faces[aFace]);
		aDeck[i].setSuit(suits[aSuit]);

		if (aFace < 10)
		{
			aDeck[i].setValue(aFace + 1);
		}
		else
		{
			aDeck[i].setValue(+10);
		}

		aSuit++;
		if (aSuit > 3)
		{
			aSuit = 0;
			aFace++;
		}
		if (aSuit > 12)
		{
			aFace = 0;
		}


	}//for

}

void deck::showDeck()                                                       // lines 64 - 70 show the deck so we can see that previous code functions properly and includes all cards
{
	for (int i = 0; i < 52; i++)
	{
		cout << cardDeck[i].getFace() << " of " << cardDeck[i].getSuit() << endl;
	}
}

void deck::shuffleDeck()                                                 // lines 72 - 83 shuffle the deck
{
	srand(time(0));

	cout << endl << "Shuffling....." << endl << endl << endl;

	for (int i = 0; i < 52; i++)
	{
		int r = i + (rand() % (52 - i));
		swap(cardDeck[i], cardDeck[r]);
	}
}

card deck::dealCard()                                                   // lines 85- 90 deal card and uses code from Card.h
{
	card aCard = cardDeck[placeInDeck];
	placeInDeck++;

	return aCard;
}

Player.h

using namespace std;

class player                                           // sets up class for player, lines 10 - 23 set public variables, lines 26 - 31 set private variables
{
public:
	player();
	~player();

	void setName(string aString);

	int getNumberOfCardsInHand();
	int getValue();
	string getName();

	void getCard(card aCard);
	void showYourHand();
	int getScore();
	//void setAce(card aCard);


private:

	string name = "";
	card hand[10];
	int numberOfCardsInHand = 0;
	int value = 0;



};

Player.cpp
player::player()
{

}
player::~player()
{

}

int player::getNumberOfCardsInHand()              //lines 15 - 35 getters and setters for player variables
{

	return numberOfCardsInHand;

}

int player::getValue()
{
	return value;
}

void player::setName(string aString)
{
	name = "";
}

string player::getName()
{
	return name;
}

void player::getCard(card aCard)                                      // gives player card and increments the number of cards in hand
{
	cout << aCard.getFace() << " of " << aCard.getSuit() << endl;
	hand[numberOfCardsInHand] = aCard;
	numberOfCardsInHand++;

}
void player::showYourHand()                                            // moves through players and shows what they have in hand
{
	for (int i = 0; i < numberOfCardsInHand; i++)
	{
		cout << hand[i].getFace() << " of " << hand[i].getSuit() << endl;
	}
}

int player::getScore()                                                 // determines scores of individual players
{
	int handScore = 0;
	for (int i = 0; i < numberOfCardsInHand; i++)
	{
		handScore += hand[i].getValue();
	}

	return handScore;
}

a linked list looks like this:

class data
{
int i;
double d; //whatever data you need.
card c;
data *next; //this is the linked list part.
data() {next = nullptr;} //self defense ...
};

and you need methods that allocate and connect the pointers.
eg insert starts somewhere in the chain, and adds a new one. If order is ignored, its easy to add to the top: tmp = new data; tmp.c = somecard; tmp.next = head; head = tmp;
head is assumed to be the variable at the 'top' of the list.
so it says... get memory for a new data (class) instance, copy information into it from somewhere (somecard variable, here), make the new item point to head, which is your current list, then make head point to the new item. That makes the new item the new top of the list, and the rest are chained together. The final next is always nullptr, which indicates the end of the list.

from here it is just making sure all your methods work. most of the time you need a delete one item method, delete all items method, insert one item method, find item method, ... sometimes you need insert in middle or far end, often insert on top is sufficient (its the easiest way).

if you had time and know-how I would say make a templated linked list class, but that takes some extra skill and you are low on time, then you could reuse it for both classes. The c++ list is templated anyway, so making such a potent tool by hand is a lot of work for not much real code gained (its good for the knowledge).

The easy way to delete them all is recursion, call self until next == nullptr, then delete the current one backing out.

if you had extra time you can juggle it a bit to make head work inside the class cleanly. One way to do that is to make it static, so every node knows the entire current list's head. But again, you are low on time. This trick only works if you only have a sort of singleton, though... don't let me confuse you with 'nice' extras or clever messes :) ... static won't work if you have 2 lists from the same LL class, it works nicely if you have just one list though, so its kind of a bad approach for reusable code.
Last edited on
Topic archived. No new replies allowed.