I have a Blackjack Assignment I need some help on. The Full requirements, what the .exe should look like, and what I have done so far is in this picasa link:

https://picasaweb.google.com/zaidalmas/November172011?authuser=0&authkey=Gv1sRgCNXrltTFwZnYNQ&feat=directlink

Sorry I couldn't get the insert link to work right.

What I have done: I have shuffled the cards and dealed 2 cards to each player besides the dealer.

Problem: I need an array to hold the cards and an array to calculate the total

It doesn't seem that tough but I was instructed to only use the switch(num) statement and shuffle code once in the program.

Refer back to the link I included with this post, Colored in Green Above.

I am really having a tough time starting this project, I had two other projects this year I aced but this is something else.

#include <iostream>
#include <iomanip>
#include <ctime>
#include <windows.h>
#include <stdio.h>
using namespace std;

void Header();
void Shuffle();
void Dealer();
void Deal();
void Another_Card();
void Fin_Dealer();
void WLT();

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()
{	char restart;

	Header();
	Dealer();
	Deal();
	WLT();
	gotoxy(0,19);
	return 0;
}
void Header()
{
	cout <<"\t\t\tWelcome to BlackJack!\n\n";
	gotoxy(3,3);
	cout <<"Dealer Player1 Player2 Player3 Player4 Player5 Player6 Player7 "<<endl;
	return;
}
void Shuffle(int num, char suit)
{
	return;
}
void Dealer()
{
	return;
}
void Deal()
{
	int cards[52], dup[52];
	int i = 0, card, num, rows;
	char suit;
	
	srand(time(NULL));

	for(i =0; i <52; i++)
		dup[i] = 0;

	for (int i = 1; i <8; i++)
	{
		for(rows = 4; rows <6; rows++)
		{
			gotoxy(i*8,rows);
			card = rand() % 52;

			 while(dup[card])
				card = rand() % 52;

			dup[card] = 1;
			suit = char(card/13 + 3); //display suit
	
			num = card %13;
			switch(num)
			{
			case 0: cout<<setw(6)<<right<<" A"<<suit;
				break;
			case 1:
			case 2:
			case 3:
			case 4:
			case 5:
			case 6:
			case 7:
			case 8:
			case 9: cout<<setw(6)<<right<<" "<<num + 1<<suit;
				break;
			case 10: cout<<setw(6)<<right<<" J"<<suit;
				break;
			case 11:cout<<setw(6)<<right<<" Q"<<suit;
				break;
			case 12:cout<<setw(6)<<right<<" K"<<suit;
				break;
			default:cout<<setw(6)<<right<<"Error";
				break;
			}
		}
	}
	return;
}
void Another_Card()
{
	return;
}
void Fin_Dealer()
{
	return;
}
void WLT()
{
	return;
}

Recommended Answers

All 8 Replies

Problem: I need an array to hold the cards and an array to calculate the total

So what's the problem? What is it you don't understand?

It doesn't seem that tough but I was instructed to only use the switch(num) statement and shuffle code once in the program.

It really isn't. That's why we need to know what the problem you're having is.

Refer back to the link I included with this post, Colored in Green Above.

Rather not. Post the part you're having trouble with if you really think we need it and you can't explain it yourself.

In the Deal() function it shuffles the cards and prints out the first two cards.

1.If I'm supposed to have an array to hold the cards for each player. How do I add the first two cards I printed added to each of the 7 arrays.

2.Another_Card() function is supposed to ask the player if they want another card. if they say yes another card is added to that player's array. How do I do this if I cannot use the switch statement more than once?


I also uploaded the .exe that my program is supposed to look like

1.If I'm supposed to have an array to hold the cards for each player. How do I add the first two cards I printed added to each of the 7 arrays.

1) If you need the first element of array[10] to be a 3, how would you do it? Write the statement: ____________________
2) What is the first thing you printed out after the shuffle? Write the thing: _________
3) Why can't you simply change the 3 above to the thing?

2.Another_Card() function is supposed to ask the player if they want another card. if they say yes another card is added to that player's array. How do I do this if I cannot use the switch statement more than once?

What makes you think a switch() is necessary?
Have you heard of IF statements?

I also uploaded the .exe that my program is supposed to look like

Don't care. Not going to run .exe files I don't know about. Too much can go wrong. Don't want no gotcha's

First off, permit me to recommend a different representation of the cards, one which would almost certainly be easier to work with:

enum SUITS {HEARTS, CLUBS, DIAMONDS, SPADES};

struct Card 
{
    int face;
    SUITS suit;
};

Card deck[52];

Of course, your coursework may not have covered structures and enumerations yet, in which case this wouldn't help much.

That having been said, the real issue is that you are tangling up the representation of the cards, the process of displaying the cards, the process of shuffling the cards, and the process of dealing the cards, all in a single function. You'll want to separate these four issues as best as you can, preferably with separate functions.

Among other things, you'll want to make deck[] available to multiple functions, either by a) making it a global variable, b) passing it to the functions explicitly, or c) writing a Deck class and having the functions all be methods of the class. The first of these options is easiest, but also presents the most potential problems; the second is better, but a bit if a hassle. The third option is probably best, but I am assuming here that you haven't covered classes yet, so it isn't one you could use right now. Let's take approach b - paing the deck[] variable as a function argument - as our solution for the moment.

Backing up a bit, you'll want to have a way of displaying a given card, without having to repeat the code for it time and again. Something like this should do the trick:

char get_suit(int card)
{
    char SUITS[] = {'H', 'C', 'D', 'S'};
    return SUITS[ card / 13];
}

string get_value(int card)
{
    string FACES = {" A", " 1", " 2", " 3", " 4", " 5", " 6",
                    " 7", " 8", " 9", "10", " J", " Q", " K"};

    return FACES[card % 13];
}


void display_card(int card)
{
    cout << setw(6) << right << get_value(card) << get_suit(card);
}

(WARNING: this is untested code, you may need to make further corrections to it later.)

Note that this draws the card wherever the cursor is currently pointing; you'll need to move the cursor to the desired coordinates first before calling this. I did it this way because there may be times when you want to show two or more cards in a row (i.e., a hand), which would make having to set the coordinates for each and every card redundant.

This is just the start of the project; you'll need to work out the details of the deck shuffle, the draw, and so on. But this should get you around the immediate problem.

OK well I didn't have much time to work on this but I did start splitting the program into parts. When I compile this it gives an error "Shuffle: ambiguous call to overloaded function" but I can't find whats wrong after tampering with it.

Have you heard of IF statements?

Yes I have I prefer Switch() statements in this case and either way I would only be able to use that statement once.

#include <iostream>
#include <iomanip>
#include <ctime>
#include <windows.h>
#include <stdio.h>
using namespace std;

void Header();
void Shuffle(int&, char&);
void Dealer();
void Deal();
void Another_Card();
void Fin_Dealer();
void WLT();

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()
{	char restart;

	Header();
	Dealer();
	Deal();
	WLT();
	gotoxy(0,19);
	return 0;
}
void Header()
{
	cout <<"\t\t\tWelcome to BlackJack!\n\n";
	gotoxy(3,3);
	cout <<"Dealer Player1 Player2 Player3 Player4 Player5 Player6 Player7 "<<endl;
	return;
}
void Shuffle(int num, char suit)
{
	int cards[52], dup[52];
	int card;
	srand(time(NULL));

	for(int i =0; i <52; i++)
		dup[i] = 0;

		card = rand() % 52;

		 while(dup[card])
			card = rand() % 52;

		dup[card] = 1;
		suit = char(card/13 + 3); //display suit
		num = card %13;
	return;
}
void Dealer()
{
	return;
}
void Deal()
{	
	int i = 0, num;
	char suit;
	for(i = 0; i <10; i++)
	{
	 Shuffle(num, suit);
	 switch(num)
			{
			case 0: cout<<setw(6)<<right<<" A"<<suit;
				break;
			case 1:
			case 2:
			case 3:
			case 4:
			case 5:
			case 6:
			case 7:
			case 8:
			case 9: cout<<setw(6)<<right<<" "<<num + 1<<suit;
				break;
			case 10: cout<<setw(6)<<right<<" J"<<suit;
				break;
			case 11:cout<<setw(6)<<right<<" Q"<<suit;
				break;
			case 12:cout<<setw(6)<<right<<" K"<<suit;
				break;
			default:cout<<setw(6)<<right<<"Error";
				break;
			}
	}
	return;
}
void Another_Card()
{
	return;
}
void Fin_Dealer()
{
	return;
}
void WLT()
{
	return;
}

The signature of the function prototype for Shuffle() doesn't match that of the actual function definition. The prototype has the two parameters as reference types,

void Shuffle(int& num, char& suit);

but the function just has

void Shuffle(int num, char suit)

These are seen as being two different function signatures by the compiler, which assumes you meant to overload the function name Shuffle() but cannot disambiguate calls for the two forms.

(BTW, it is advisable to always give the full function signature - including the names of the parameters - in the prototype, rather than just the type signature, even though it isn't required. With good parameter naming, the full prototype is much more readable and tells you more about the function.)

Yeah I realized right after posting

I feel there is a problem with the Shuffle() function itself.

When I run without debugging it will give me and output like (A of Spades) twice
but I put an array so it would not duplicate a card.

However when I do debug it and get into every line of code the output is would give me two non-duplicated cards.

I need it to not duplicate and I have no idea why it does this

Couple things I see:
srand() should be called once only -- when the program starts.
You call shuffle() for every card dealt. In a real game this means:
1) Shuffle the cards
2) Deal one card
3) Take the card back
4) Shuffle again
5) Deal one card again (which could be the same card)
6) Take the card back
7) Shuffle again
8) continue...

Rethink your deal...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.