Black Jack

My home work is to write a blackjack program the part i am having trouble with is drawing a random card out of the set my professor is having us use.
#include <iostream>
#include <cstdlib>
using namespace std;

typedef enum
{
VALUE,
JACK,
QUEEN,
KING,
ACE
} FACE;

typedef struct
{
int value;
FACE f;
} CARD;

void print_card(CARD c)
{
if (c.f == ACE)
cout << "ACE (11)";
else if (c.f == KING)
cout << "KING (10)";
else if (c.f == QUEEN)
cout << "QUEEN (10)";
else if (c.f == JACK)
cout << "JACK (10)";
else if (c.f == VALUE)
{
if (c.value == 10)
cout << "TEN (10)";
else if (c.value == 9)
cout << "NINE (9)";
else if (c.value == 8)
cout << "EIGHT (8)";
else if (c.value == 7)
cout << "SEVEN (7)";
else if (c.value == 6)
cout << "SIX (6)";
else if (c.value == 5)
cout << "FIVE (5)";
else if (c.value == 4)
cout << "FOUR (4)";
else if (c.value == 3)
cout << "THREE (3)";
else if (c.value == 2)
cout << "TWO (2)";
else
cout << "Value not recognized";
}
else
cout << "Face not recognized";
}

void init_deck(CARD deck[])
{
for (int index = 0; index < 4; index++)
{
int seg = index * 13;

deck[seg + 0].f = VALUE;
deck[seg + 0].value = 2;

deck[seg + 1].f = VALUE;
deck[seg + 1].value = 3;

deck[seg + 2].f = VALUE;
deck[seg + 2].value = 4;

deck[seg + 3].f = VALUE;
deck[seg + 3].value = 5;

deck[seg + 4].f = VALUE;
deck[seg + 4].value = 6;

deck[seg + 5].f = VALUE;
deck[seg + 5].value = 7;

deck[seg + 6].f = VALUE;
deck[seg + 6].value = 8;

deck[seg + 7].f = VALUE;
deck[seg + 7].value = 9;

deck[seg + 8].f = VALUE;
deck[seg + 8].value = 10;

deck[seg + 9].f = JACK;
deck[seg + 9].value = 10;

deck[seg + 10].f = QUEEN;
deck[seg + 10].value = 10;

deck[seg + 11].f = KING;
deck[seg + 11].value = 10;

deck[seg + 12].f = ACE;
deck[seg + 12].value = 11;
}
}

void print_deck(CARD deck[])
{
for (int x = 0; x < 52; x++)
{
print_card(deck[x]);
cout << endl;
}
}

void shuffle_deck(CARD deck[])
{
for (int x = 0; x < 52; x++)
{
int new_index = rand() % 52;
CARD temp = deck[x];
deck[x] = deck[new_index];
deck[new_index] = temp;
}
}

int main()
{
srand(5);
CARD deck[52];
init_deck(deck);
int funds;
int bet;

cout << "*****BlackJack*****" <<endl;
cout << "Please enter starting amount of funds:" << endl;
cin >> funds;
cout << "Place first bet: " << endl;

int x = rand() % 52;
cout << "dealer first card: " << print_card() << endl;
//shuffle_deck(deck);
//print_deck(deck);

}

i want to use the print card function and get a random card then add it to a total? any help please, or use another of the declared functions, to print a card and use the value of that card.
Last edited on
please any help?
First of all, please use code tags for your code - it is very hard to read when it's not formatted.

So, you have x - the random number of a card. So you can access that card as deck[x]. You can then send it to print_card function to print it.

Or you can use your shuffle_deck function first, and then go through the deck in sequential order (starting from x = 0;).

To add to total, access the deck in the same way:
1
2
int dealer_total = 0;
dealer_total += deck[x].value;
When I use print_card(deck[x]) it gives me an error could I be coding wrong?
Your code looks like the same code that my teacher gave me. I am currently trying to do my code as well. Here is the code that I have for my dealer and player for initial hand.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
        int next_index = 0;
        CARD c1 = deck[next_index++];
        CARD c2 = deck[next_index++];
        CARD c3 = deck[next_index++];
        CARD c4 = deck[next_index++];
        CARD c5 = deck[next_index++];



        int total_value = c1.value + c2.value;

        cout << "Current Hand: ";
        print_card(c1);
        cout << " ";
        print_card(c2);
        cout << ". " << "Player has: " << total_value << endl;

        int dealer_value = c3.value + c4.value;

        cout << "Dealer's hand: ";
        print_card(c3);
        cout << " ";
        print_card(c4);
        cout << ". " << "Dealer has: " << dealer_value << endl;
what is the error
Yea broncs we are in the same class lol I have been seeing your posts on here you have a lot of te same problems as I do lol.

With Your next hand code what if it goes past 5 cards say player gets a2 and a4 and wants to hit? I thought we needed to code for that? That's why I want to be able to pull a random card out of deck[] and add it to a total till you say hit 21 or over..

I don't have the code on hand for the error I will post it when I get home from work
Yeah, sorry that code was just what I used for the initial two cards to be dealt. I can give you code for the loops I have for the cards to be pulled but for some reason the cards are being drawn in the wrong order and I can't figure it out. For example they hit and draw a 5 and then a 2 instead of a 2 and then a 5. That's the only problem that I have left to solve.
Last edited on
Yea i saw your post about that im at a loss for how to change it like you said changeing the lines from ++ -- didnt change it >.<
thank you so much for all your help BroncoAG, looking at your loops and code helped me get mine together =D
No problem, if you get yours working today or tomorrow after noon let me know. Mine is due tomorrow. Hopefully I can figure it out or I'll just leave it as it and hope I get some credit for it.
yea mines due tomorrow night i think we are in the same class lol. I have no clue why its switching the cards lol
what I hate/love about the class is he gives you what he wants most the outputs to be on hypergrade whats output to the screen like "Your broke, get out of the casino." lol
I figured out the card problem and passed 7/8 of the test but I still am getting the wrong cards on the multiple game on.

to fix the wrong cards I had to remove the
CARD c5 = deck[next_index++]
from the beginning were I had them all in a row and the in the player hit section I left that and changed the -- to ++ and the in the dealer section I changed it to CARD c6 = deck[next_index++] along with the values to c6. Also make sure if you used the int init_count = 3 you need to bump that up to 6 to pass hypergrade.
yea the problem with mine seems to be when i go through the while loop the first time it does not remember it has gone through it so it starts back at the value it was at before, instead of going to the next value in the array. i think lol.
I just finished mine and it passed. I can pm it to you if you want to look at it for guide to see if it will help.
yeah that would be great thanks man
i enabled pm if you want to send it, man this was a crazy problem
Topic archived. No new replies allowed.