Surgery

Pages: 12
I was trying to change something after it has been written which is like removing a peice that has a couple connections quite diffiuclt:)

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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306

#include "cards.h"

//Card array
cards hand[52];

//Constants

const int PLAYERCOUNT = 7;
const int GAMELIMIT = 21;
const int DEALER = 16; 
const int TWOCARD = 2;

void Dealer(string dName[], int dHit[], int i)
{    
	
	
	dName[i] = "Dealer";
	dHit[i] = DEALER;
	
}


void display()
{
	cout << "BlackJack by Jordan Lillie" << endl;
	cout << "The shuffled deck is : " << endl;

	for(int i = 0; i < 52; i++)
	{
		if(i % 13 == 0)
		{
			cout << endl;
		}

		hand[i].display();
		cout <<' ' << ' '; 

	}
	cout << '\n' << endl;
}

void displayHit(string playersName[], int hitLim[])
{
	cout << "Player Name" << '\t' << "Hit Limit" << endl;
	
	
	for(int i = 0; i < PLAYERCOUNT-1; i++)
	{
		setfill(" ");
		cout << right << setw(11) << playersName[i] << " " 
		<< right << setw(9) << hitLim[i] << endl;
	}
		cout << endl;

}

void deal(string playersName[],int playerStat[])
{
	int counter = 0;
	cout << "Deal two cards to everyone" << endl;
	
	for(int i = 0; i < TWOCARD; i++)
	{
		for( int j = 0; j < PLAYERCOUNT; j++)
		{	
			playerStat[j] += hand[counter++].getFace();
		}

	}
	
	//Display
	for( int k = 0; k < PLAYERCOUNT; k++)
	{
		cout << "Hand for " << playersName[k] << " is : " ;
		hand[k].display();  
		cout << " and " ;
		hand[k + PLAYERCOUNT].display();
		cout << " Value for this hand is " << playerStat[k] << endl;
	}
	 cout << endl << endl;
}

void limits(string playersName[], int playersStat[], int hitLim[])
{

	int hitCount = PLAYERCOUNT * TWOCARD;
	int k = 0;

	for( k = 0; k < PLAYERCOUNT; k++)
	{
		while(playersStat[k] < hitLim[k])
		{
		playersStat[k] += hand[hitCount].getFace();
		cout << "Player " << playersName[k] << " getting another card ";
		hand[hitCount].display();
		cout << endl;
		hitCount++;
		}
	 
		cout << "Value of hand for " << playersName[k] << " is now  " << playersStat[k] << endl;
	}
	
}

void displayEndgame(int playerStat[], string playersName[])
{		
		cout << endl;
		cout << "Results of this game" << endl;

    for(int i = 0; i < PLAYERCOUNT - 1; i++)
	{
		  if(playerStat[PLAYERCOUNT-1] == GAMELIMIT)
	      {
		     for( int k = 0; k < PLAYERCOUNT - 1; k++)
		     {
			 cout << "The deal beats player" << playersName[k] << endl;
		     }
		     break;
	     }
	
	    if(playerStat[i] > GAMELIMIT)
	    {
		    cout << "Dealer beats player " << playersName[i] << endl;
	    }
	else 
	{
	if(playerStat[PLAYERCOUNT-1] <= GAMELIMIT)
    {
			if(playerStat[PLAYERCOUNT - 1] >= playerStat[i])
			{
				cout << "Dealer beats player" << playersName[i] << endl;
			}
			else 
			{
				cout << "Player " << playersName[i] << " beats the dealer" << endl;
			}
	 }
		   else
		   {
			cout << "Player " << playersName[i] << " beats the dealer" << endl;
		   }
	}
 }
	      cout << endl;
}

void game(string playersName[],int hitLim[],int i, 
int playerStat[])
{
	Dealer(playersName,hitLim,i);
	display();
	displayHit(playersName, hitLim);
	deal(playersName, playerStat);
	limits(playersName, playerStat, hitLim);
	displayEndgame(playerStat, playersName);
}

void shuffleCards(int counter)
{
	for(int s = 3; s <= 6; s++)
	{
		for (int f = 1; f <= 13; f++)
		{
			hand[counter] = cards(f, char (s));
			counter++;
		}

	}

	for(int i = 0; i < 52; i++)
	{
		hand[i].swap(hand[(rand() % 52)]);

	}
	cout << endl;
} 


class
 


 
#include <string>
#include <ctime>
#include <iomanip>

using namespace std;

class cards
{
  private:
	int face;
	char suit;
	
  public:
	  cards(int = 0, char = ' ');
	  void display();
	  void swap(cards&);
	  void setCards(int, char);
	 
	  //Getters
	  int getFace();
      char getSuit();

};


//Functions
cards::cards(int f, char s)
{
	face = f;
	suit = s;
}

//Swap
void cards::swap(cards& c)
{
	cards temp;

	temp.face = c.face;
	temp.suit = c.suit;
	c.face = face;   
    c.suit = suit;  
    face = temp.face;
    suit = temp.suit;
}

//Display
void cards::display()
{
	switch(face)
	{
	case 11: cout <<'J';
		break;
	case 12: cout << 'Q';
		break;
	case 13: cout << 'K';
	    break;
	case 1: cout << 'A' ;
	    break;
	default: cout << face;
	}
	
	cout << suit;
}

void cards::setCards(int f, char s)
{
	face = f;
	suit = s;
}

int cards::getFace()
{	
	return face;

}

char cards::getSuit()
{
	return suit;
} 

Main 




#include "functions2.h"




int main()
{
	int hitL[PLAYERCOUNT]= {0}, cardsT[PLAYERCOUNT] = {0}, 
	index = 0, i = 0;
	string playersname[PLAYERCOUNT];
	char limit = ' ' ;
	ifstream fin("input.txt");

	srand((unsigned)(1234));
	
	//Shuffle cards
	shuffleCards(index);
	
	//Input
	fin >> playersname[i] >> hitL[i];
	//Input
	while(!fin.eof())
	{   
		i++;
		fin >> playersname[i] >> hitL[i];
	}
	
	//Game function
	game(playersname,hitL,i,cardsT);

	system("pause");


return 0;

 

In the shuffle function I'm passing looping from I think 3 - 6 And using the constructor to make a card. What any way that I can't change my cards from the actual char to a letter like QH or JH instead of the char character? This is hard to think of .
Thanks
If I understand correctly, you are having issues shuffling your cards. When you call cards with hand[counter] = cards(f, char (s)); you're passing the character value of s. You don't need to cast it since an int is convertible. However, the value of s is somewhere from 3-6. I don't know what the actual characters of 3-6 are, but I know it's not going to give you H, S, C, or D like I believe you want. You have two options, you can either convert it before passing it, or you can let your class handle it. I believe the second method is best, so I'll show you how to fix that.

In Shuffle Cards, you need to change for(int s = 3; s <= 6; s++) to for(int s = 1; s <= 4; s++) and hand[counter] = cards(f, char (s)); to hand[counter] = cards(f, s);. This will simply do the same thing you had before, but force the values to be changed from 3-6 to 1-4. This makes it easier to understand.

Now, we have another thing to change. We're still just passing two ints to cards, but your member function wants an int and a character. That's a smart way to make a card function, but what about when we pass two ints? We need to overload that function.

In your class declaration, right under cards(int = 0, char = ' ');, add the line cards(int, int);. This will overload the cards function so that it can accept more than one set of parameters and handle them accordingly.

We now need to define it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void cards::setCards(int f, int s)
{
	face = f;
        switch(s) {
                case 1:
                        suit = 'S';
                        break;
                case 2:
                        suit = 'H';
                        break;
                case 3:
                        suit = 'C';
                        break;
                case 4:
                        suit = 'D';
                        break;
}


That will manually convert a number from 1-4 into the corresponding suit that you want. I hope this fixes your problem.
Last edited on
Dude I just got home from school and I haven't been able to try that yet. I will post later to let you know. I appreciate your help tremendously:) Even if it doesn't work that's a good response:)
Thanks
I will try tonight
Worked great dude thanks so much:)

Except that the value of JH QH for the first player according to my sample should be 20 and I get 23. I don't think this has anything to do with the help you gave me but do you have any idea why I would get the wrong value? I seed my random number generator to 1234 as suggested by instructions. My cards are spot on with the sample but the value of the card is not I"m not sure why.
Line 67:
playerStat[j] += hand[counter++].getFace();

The face value of Jacks, Queens, and Kings should always be 10...I'm unsure how you handle Aces if that's not done correctly.

The problem lies in the fact that getFace will return a number 1-13, I assume, and Jacks in your program equal 11 and Queens equal 12.

To fix this, you need to look at your function getFace and implement some if statements.
Dude once again thank you! I appreciate it so much.
Solution was
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int cards::getFace()
{	
	if(face > 10)
	{
		face = 10;
		return face;
	}
	
	else
	{
		return face;
	}
}


I did it ..ran it...it matches my output given from my hand out now. Whew. Thanks. Good move. Let me ask you a side note question did you take a into to C++ course in maybe college? If so how far in depth did your instructor go. As in amount of material covered?
I took C++ in high school, two years of it there, and studied at ITT for two years. I learned more in high school and on my own time than I was ever taught at ITT.

I see that you're covering classes now, typically your next step is class inheritance and/or lists. Each professor/teacher has their own strategy on how to teach, but that's the typical course and should complete your semester. If there is going to be more, it's going to really round everything up and start testing your logical skills.

I've been helping some students, one is on her third semester of C++ and it was a little challenging for me since it was a lot of stuff I'm not good with. But if you study and learn to think like a programmer, it becomes much easier. In my opinion, syntax is the easy part, the logical thinking is what will really test you.
I'm done this coming week :). I feel that the hardest part is like you say the logic.
Speaking of which I just noticed that because I implemented that if statement in get face that when I do my dealing of cards that say the dealer is short so gets another card in the sample it is KH in mine it is TH for ten of Hearts I don't even think that card exists. I don't know the issue with doing card games for me is I hate cards I don't ever pick them up. I had probability so I just learned that there were 52 ha no joke. :)
Lol, that's funny. But without some pretty major rewriting, there is a quick trick to use. You need to have two functions, one called get value, returns 1-10, and one called get face that returns a character of the face value. I hope that helps you.
Yes I get what your saying but I don't quite understand. So I can use getvalue in the place of getface here

playerStat[j] += hand[counter++].getFace();
will be

playerStat[j] += hand[counter++].getValue(); And that will be it ?
Well, anywhere that getFace needed to be an integer, you need to replace it with getValue(). Anywhere that you need a character, use getFace. getFace should return only Jack ('J'), Queen ('Q'), etc. if the card isn't a face card, then return getValue instead, if that makes sense.
OK this is how I fixed it but of course something else is being pushed out of place. So it appears the the cards are correct. Like KH for dealer now but his value is wrong.
This is how I did the getValue just the same as the other function when the values were right but the card was wrong :) haha oh jeeze

1
2
3
4
5
6
7
8
9
10
11
12
13
int cards::getValue()
{
	if(face > 10 )
	{
		face = 10;
		return face;
	}
	else
	{
		return face;
	}

}
Change face = 10; return face; to just return 10;
Done, when I do that player Bob is off by one he should be 26 and dealer should be 17. Instead they are 27 and 20 respectively.

The cards themselves are once again correct but only these two players are off so close.
The card dealt to Bob first gets QC 2H the value of this is correct it is 12
Then he gets 4H, then JS. The value is 27 (wrong).

The dealer gets AS and 6C the value is 7 this is correct. He then gets, KH and the value is 20.(wrong)
Like I said the cards dealt are all correct only the values in bold are wrong.
What is this telling me. How strange that all the others are correct except two values this seems the program especially because of the loops that if one is right all wold fall into line.
I think it might be with this function

This is the one that handles that part of the program. Possibly with getFace?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void limits(string playersName[], int playersStat[], int hitLim[])
{

	int hitCount = PLAYERCOUNT * TWOCARD;
	int k = 0;

	cout << "Play the game - who wants more cards? " << endl;
	cout << endl;
	for( k = 0; k < PLAYERCOUNT; k++)
	{
		while(playersStat[k] < hitLim[k])
		{
		playersStat[k] += hand[hitCount].getFace();
		cout << endl;
		cout << "Player " << playersName[k] << " getting another card ";
		hand[hitCount].display();
		cout << endl;
		hitCount++;
		}
		//cout << endl;
		cout << "Value of hand for " << playersName[k] << " is now  " << playersStat[k] << endl;
	}
	
}
BINGO GOT IT DUDE !!!!!! THANKS AGAIN :)
I even remember doing this program in class and I can't remember exactly what I did. If I'm not mistaken, I used an enum for this.

What's important is the getValue function which should return the card value, 1-10.

The problem is properly outputting the the face as a character if it's a j,q,k.

Let me look over the code again and see if I can narrow it down.

Edit: Can you share input.txt?

I'm glad you got it. And no problem.
Last edited on
I got it I got it ! Don't look over that haha. It was the function I suggested I didn't change it to get value.

The last thing is to use setw to make the code formatted to something like this. Where all the : are lined up. I'm having problems because of the array in the statement I can't get it because of this type of thing cout << "Hand for " << playersName[k] << " is : "; Ususally it is just cout << setw() << "blah blah << endl; not with the arrary in the middle this is dividing my code weird when I try to format.

Hand for Andy is : JH QH Val.......blah blah

1
2
3
4
5
6
7
8
9
10
11
//Display
	for( int k = 0; k < PLAYERCOUNT; k++)
	{
		 cout << "Hand for " <<  playersName[k] << " is : ";
		 hand[k].display(); cout << ' ';
		 hand[k + PLAYERCOUNT].display();
		 cout << " Value for this hand is " << 
		 playerStat[k] << endl;
	}
	 cout << endl << endl;
}
Do you mean something like:
    Hand for Dealer is : AS JS
Value for this hand is : 21

      Hand for Jack is : 10S 5S
Value for this hand is : 15
Last edited on
Yeah but because the array is there I can't quite figure out how to align it. I need the : to be all aligned. How are you supposed to do this with that damn array just sitting there? Because setw will end there.
Pages: 12