Simple Blackjack Program - Misaligned Output

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
                                                                                                             
#include<iostream>
#include<ctime>
#include<iomanip>
#include<string>
#include<windows.h>
#include<stdio.h>
 using namespace std;
 
 void shuffleCards(int[]);
 void printHeader();
 int dealCards(int[], int[], int, int);
 void printCards(int, int, int);
 char suit(int);
 int cardValue(int);
 char cardFace(int);
 int offerCard(int[], int[], int, int, int);
 void myPrint(string);


void gotoxy(int h, int w)
{
	HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
	
	if (INVALID_HANDLE_VALUE != hConsole)
	{
		COORD pos = {h,w};
		SetConsoleCursorPosition (hConsole, pos);
	}
	
	return;
}
 
 int main()
 {
	 int k, cards[52];
	 int Player[8] = {0,0,0,0,0,0,0};
	 int i = 0, j=0, currentCard;
	 char reply;
	 
	srand(time(NULL));

	 for(i=0; i<52; i++) //Set deck == 0-51
		 cards[i]=i;

	 shuffleCards(cards);

	 printHeader();

	 for(i=0; i<16; i++)
		i = dealCards(Player, cards, i, j); 
	 cout << endl;
	
	 k = i;
	 offerCard(Player, cards, i, j, k);

	 cout << endl << endl;

	 system("pause");
	 return 0;

 }

 
void shuffleCards(int cards[])
{
	int i, k, picked[52], value;

	for(i = 0; i < 52; i++)  //setting array elements equal to 0
		picked[i] = 0;

	for(i = 0; i< 52; i++)
	{
		k = rand() % 52;

		while(picked[k])
			k = rand() % 52;

		picked[k] = 1;
		cards[i] = k;
	}

	return;
}

 void printHeader()
{  
	cout << "D      " << "P1     " << "P2     "<< "P3     " << "P4     "<< "P5     "<< "P6     "<< "P7     ";
	return;
}
 
	 
 int dealCards(int Player[], int cards[], int i, int l)
	 /*This function needs to:	
           1. Give cards 0 through 7 to players 0 through 7	
           2. Give cards 8 through 15 to players 0 through 7
           3. Update Player[] array with every players total    
           4. Print the cards received by calling on suit, value and number functions*/
 
 {
		 int j = i%8;  //j gives player at this juncture because cards are dealt one per player
		 int k = i;
		 Player[j] += cardValue(cards[i]);
		 printCards(cards[i], j, i); //j = player number
		 gotoxy(7*j, 17);
		 cout << Player[j];

	 return k;
 }

 void printCards(int i, int j, int k) //i is the card value, j is player number, k is former i
 {
	 if(k<8)
	    gotoxy(7*j, 1);	 
	 else if (k < 16)
	    gotoxy(7*j, 2);
	 else 
	    gotoxy(7*j, 3); 

	 if(i%13 == 0 || i%13 == 1 || i%13 == 11 || i%13 == 12)
		cout << cardFace(i) << suit(i);
	 else
		cout << cardValue(i) << suit(i);

	 return;
 }

 
int cardValue(int k)
{
	int value = 0;
	k = k % 13;

	if (k >=1 && k <=10)
		value = k;
	else 
		value = 10;
	return value;
}

 char suit (int k)
{
	int suit;
	char csuit;

	suit = k/13;

	switch(suit)
	{
		case 0: csuit = char(3);
			break;
		case 1: csuit = char(4);
			break;
		case 2: csuit = char(5);
			break;
		case 3: csuit = char(6);
			break;
	}
		
	return csuit;
}

 char cardFace(int value)
{
	char face = ' ';
	if (value%13 == 11)
		face = 'J';
	    else if (value%13 == 12)
		face = 'Q';
	    else if (value%13 == 0)
		face = 'K';
		else if (value%13 == 1)
		face = 'A';

	return face;
}

 int offerCard(int Player[], int cards[], int i, int j, int k)
 {
	 
   string s = "Another card for player ";
   string response;
	for(i=1; i<8; i++)
	 {
		 gotoxy(0, 18);
		 if (Player[i] <= 20)
			myPrint(s);
			cout << i << "? (Y or N)" << endl;
			cin >> response;
			if (response == "Y" || response == "y")
			{
				gotoxy(7*i,18);
				k += 8;
				dealCards(Player, cards, i, k);
				if (Player[i] <= 20)
					gotoxy(0, 18+i);
					myPrint(s);
					cout << i << "? (Y or N)" << endl;
			}
	 }

	 return k;
 } 
 
 void myPrint(string s)
{
	int length = s.length();

	for (int i=0;i<length;i++)
     {
		 cout << s[i];
		 Sleep(50);
     }
	return;
}


This is my code, I'm currently having a problem with my output. i'm not sure how to pass the right value(s) to control where my cout lines are printing. Also, I know the code isn't complete yet, I just can't move on until I get this to output correctly. Thanks in advance and any help is appreciated!

-m1ck3y
Last edited on

I'm currently having a problem with my output


Any part of this mess of code in particular? Don't just dump a portion of code and run and then expect to get help... It may not always happen...

printHeader() ?
printCards() ?
myPrint() ?

Also, please paste your code in the code tags!! you simply need but post code within [ code] and [ /code] (remove the spaces).


I also might suggest using memset() to zero a simple array. It's much faster than a loop you code yourself.

 
int dealCards(int Player[], int cards[], int i, int l)


Am I mistaken or is 'L' not used in that function at all? Either way it's a horribly ambiguous variable name.
Last edited on
Wow, I'm not really sure why you are being such a jerk. In the future if you feel as though you are too busy / accomplished to respond to a post you see, don't.

My apologies about not using code tags, I didn't realize that I should/could. This is my very first time programming, I'm in an introduction to programming course at my college. There really isn't any reason to use terms like "mess of code" and "horribly ambiguous" It just makes you sound like some elitist jerk. Maybe you ought to take a step back and realize that you are responding to posts often written by beginner programmers that feel as though they have no where else to turn. Imaging someone being so hurtful when you were first learning to program. Anyway, I thank you for the actual suggestions you have and I'll go figure this out myself.

Last edited on
Thanks for the brutally honest response, this isn't my first time around a programming forum. I think I just come off like a douche-bag sometimes--you're not the first person to tell me that. But, my intention *is* to help you.

So, could you maybe narrow down where the problem occurs? The idea is, I'm not entirely up to speed, and I don't want to decode your code...

Perhaps I should suggest some comments describing what the functions do, leave a note or two maybe?

Perhaps in your printHeader() function you could use the "setw()" stream manipulator?
Last edited on
Topic archived. No new replies allowed.