Any idea why

Having a problem with the following code the array tens should be printing out either jack,king,queen,jack1 but rather it is printing out 10, i don't seem to see where i have messed up. Updated excuse the lack of commenting

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
#include <iostream>
#include <stdlib.h>
#include <ctime>
#include <string>
using namespace std;

//calling decleration of welcome screen
void Welcome_Screen ();

//decleration of random values function
void User(int& a, int& b);
void Special_VALUES(int& c, int& d);
void ValueOfTen(int& e, int& f);
void SingleCard_Pickup(int& g, int& h);
//size of array being declared and arrys

int Array_Cards [] = {2,3,4,5,6,7,8,9,10,10,10,10,11,2,3,4,5,6,7,8,9,10,10,10,10,11,2,3,4,5,6,7,8,9,10,10,10,10,11,2,3,4,5,6,7,8,9,10,10,10,10,11};
string Suits [] = {"Clubs","Spades","Diamonds","Hearts"};
string Tens [] =  {"King","Queen","jack","jack1"};
string choice ;
int main(){

srand(time(NULL)) ;

int values = 1;
int w,x,y,z,u,v,t,s;
int total;
int cardNo = 0 ;



Welcome_Screen();

cout << "Select enter key to begin"<<endl;




User(w,x);
Special_VALUES(y,z);
ValueOfTen(u,v);
SingleCard_Pickup(t,s);

cout <<"You pick up two cards"<<endl;
cout << "Your Values are "<<endl;

if (w == 10 && x != 10){
    cout << "Card 1: " <<Tens[u] << " of " <<Suits[y] <<endl ;
    cout << "Card 2: " <<Array_Cards[x] << " of " <<Suits[z] <<endl ;
}

else if (x == 10 && w != 10){
    cout << "Card 1 : " <<Tens[v] << " of " <<Suits[z] <<endl ;
    cout << "Card 2 : " <<Array_Cards[w] << " of " <<Suits[y] <<endl ;
    }

 else if (x == 10 && w == 10){
    cout << "Card 1 : " <<Tens[u] << " of " <<Suits[z] <<endl ;
    cout << "Card 2 : " <<Tens[v] << " of " <<Suits[y] <<endl ;
}

 else if (x != 10 && w != 10) {
    cout << "Card 1 : " <<Array_Cards[w] << " of " <<Suits[y] <<endl ;
    cout << "Card 2 : " <<Array_Cards[x] << " of " <<Suits[z] <<endl ;
}

cardNo = 2;

total = Array_Cards[w] + Array_Cards[x];

cout << "Your total so far is " << total;

cout <<" Do you want to hold or hit? "<<endl;
cout <<"(Please enter either hold or hit) " <<endl;


cin >> choice ;

if (choice == "hit"){

    cardNo ++;
    cout << "You draw ";
    cout<<"Card" <<cardNo <<" : " <<Array_Cards[t]<<" of " <<Suits[s] <<endl ;

    total = total + Array_Cards[t];

    cout << "Your total so far is " << total <<endl;

    if(total <=21){

    cardNo ++;

    cout <<" Do you want to hold or hit? "<<endl;
    cout <<"(Please enter either hold or hit) " <<endl;
    cin >> choice;

    if(choice == "hit"){
    SingleCard_Pickup(t,s);
    Special_VALUES(y,z);

    cout <<"You draw" <<endl;
    cout <<"Card" <<cardNo <<" : " << Array_Cards[t] <<" of "<<Suits[y] <<endl;
    total = total + Array_Cards[t];
    cout << "Your total so far is " << total <<endl;
    }

    }

    else if (total > 21){

    cout << "You have exceeded 21, computers turn" <<endl;
    }
}


return 0;
}



 void User(int& a, int& b){

a = rand() %51 ;
b = rand() %51 ;
}

void Welcome_Screen(){
cout <<"\t\t\t******************************\t\t\t\t"<<endl;
cout <<"\t\t\t****                      ****\t\t\t\t"<<endl;
cout <<"\t\t\t*****Welcome to Blackjack*****\t\t\t\t"<<endl;
cout <<"\t\t\t****  Powered by Rnjesus  ****\t\t\t\t"<<endl;
cout <<"\t\t\t******************************\t\t\t\t"<<endl;
}

void Special_VALUES(int& c,int& d){

c  = rand()%3;
d  = rand()%3;
}

void ValueOfTen(int& e, int& f){
e  = rand()% 3;
f  = rand()% 3;
}

void SingleCard_Pickup(int& g, int& h){

g = rand() %51 ;
h = rand() %3;

}


Last edited on
closed account (zNASE3v7)
I am not seeing enough of your code to completely interpret the problem, but the first question in my mind is how your assignments are made for the variables you are passing to your arrays. obviously I see they are u-z.

I think it is too easy to overlook the obvious in this, I looked at it for a little while and I even loaded it into xcode to play with for a bit, built and cleaned up most of the errors, but I think the problem must exist with your tests, if you are testing for 10 then the program should print 10.
but if your program tests for a jack then it should print jack.
and likewise for queen king etc...
I think my test might read something like this:
While (Array Cards[])
if (Tens[] == j1) else if Tens[] == j, etc... ==q, ==k
if (Suits[] == "heart",(Suits[] == "spade" etc...
just set up your Suits Array with a string return type, and if your still confused I'm sure we can get help on this forum to get your problem solved.
This one intrigued me so I hope this helps and you got my curiosity...
Last edited on
would you like to see the whole code, this is just a self given xmas project i wanted to pursue any help would be great
Even your definitions of w, x, and all of your arrays might give us a small chance.
updated with all code
Your first issue is you have not included this:
#include <string>

I'm not sure how your code was even compiling without that?
Last edited on
the other issues?
Line 16: Why is Card_Deck 51? A standard deck of cards has 52 (4x13) cards as does Array_Cards[].

 
int Array_Cards[Card_Deck] = ...  // Specifying the array size would have detected that the array has too many elements. 


Having a problem with the following code the array tens should be printing out either jack,king,queen,jack1 but rather it is printing out 10,

You have a lot of repeated code code displaying cards. You should consolidate your code for display a card into a single function.

Since your code for displaying cards is rather convoluted, it is hard to tell where you might be displaying from the wrong array.

Likewise, you also have a lot of repeated for for drawing cards. That should also be consolidated into a single function.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.


Last edited on
in regards of the const that was at the start of development and was removed but not saved afterwards hence why it is there but with no purpose, good point on the functions all being in one i really didnt consider that, the reason for the repeated calling of the function for the user was so that i may receive two new random values on draws after card two.
Last edited on
closed account (zNASE3v7)
Good Morning and Merry Christmas to ALL!
I like your game, first of all because I like Blackjack! But there are a variety of Blackjack games available. That said I have thought about this code and realized there are also some varieties of choices to make:
As AbstactionAnon Noted: there are 52 card in a regular deck, but that is minimal in your code.
I think if you want to roll the rand several times during the user choices that is ok, but I also thought about it, and the way that some Blackjack decks go there are also more than one decks being used at a time, so the other part of that is that you could have say: Jack of Clubs and Jack of Clubs, in one players hand, or whatever they happen to end up with...My point is simple, once rand is run the deck is shuffled, and the dealer - the computer: has set the deck and pulls off the top...If you like these ideas I just offer them as Chaste,
To the code: I see you have chosen the option of placing a deck into one assignment, and therefore if rand pulls a card then it has one array to choose from. That is a good idea for simplicity, and it fits with rest of the code, as well as I can tell...
I also thought that you might also be able to make 4 simple arrays as something like the following:
<string Clubs [cSIZE] = {"Ace","Two","Three","Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
string Hearts[hSIZE] = {"Ace","Two","Three","Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
string Spades[sSIZE] = {"Ace","Two","Three","Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
string Diamonds[dSIZE] = {"Ace","Two","Three","Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};>
Now there is also an alternative to this and you could use a character array:
<char Array_Cards [a_cSIZE] = {'A','2', '3', '4','5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'}; //or maybe like this and you keep the most of your code you already wrote
char Array_Cards [] = {'A','2', '3', '4','5', '6', '7', '8', '9', '10'};>

Now all this is probably a little confusing so I will further set another thought into this jibberish, and that is that I would run rand thru a nest of for loops and rand each array, <if(cin.get(choice == "hit")) { ...>
but I am not seeing an input key for hold...
For hold ("stand", "check", "pass", , or similar labels depending on the version)
:you can do something similar to "hit"...< if (choice == "Hold"){ cin << ('1');>
Next I would also mention that I might add a "fold" or redeal....LOL
Anyway this is stimulating for my brain, so I hope you can use some of this and also thank you for your game here!!! I look forward to seeing the success! and it looks pretty close!!!
Oh yeah I forgot to mention you could also throw in in some wild cards like using the Jokers!!!
Then later on you can create some objects and learn programming in classes and begin creating objects...

Last edited on
Thank you very much for your input as i said about the constant and all its gone was simply there at the first point of the program, your input was amazing bookmarking this right now for further reference seriously this was an eye opener.. i was hoping on implementing the wild cards later on once the rough outline (this code) is complete really thank you cant say it enough made me feel positive about this and it turns out this issue has been solved actually but none the less great insight thank you merry xmas hope you enjoy your holidays.

:D
Last edited on
I think you need to step back and figure out exactly how you want to represent the cards. Right now it looks like the same card could be drawn multiple times. Figure out what you need to do with cards and then work from there. Looking at this, I decided to represent the cards in a deck with the numbers 0-51.

Now to get the suit and value of a card (maybe "rank" would be better than value here:
1
2
int suit(int card) { return card / 13; }
int value(int card) { return card % 13; }


And we need the names of the suits and values:
1
2
3
4
5
6
7
// Convert suit and values to names
const char *suitName[] = {"Clubs","Spades","Diamonds","Hearts"};
const char *valueName[] = {
    "Two", "Three", "Four", "Five",
    "Six", "Seven", "Eight", "Nine",
    "Ten", "Jack", "Queen", "King", "Ace"
};


We can use these to easily create a string representation of the card:
1
2
3
4
5
6
7
8
9
10
11
// Return the name of a card. Eg "Ace of Hearts"
string cardName(int card)
{
    string result = valueName[value(card)];
    result += " of ";
    result += suitName[suit(card)];
    char buf[20];
    sprintf(buf, " (%d)", card);
    result += buf;
    return result;
}


Now lets think about the deck. As others have pointed out, in blackjack, there are often multiple decks that are shuffled together into a shoe. So I chose a vector of int for the shoe and kept in mind that it can be any size. To start, you need a way to initialize the shoe with the card values. This just jams cards into the shoe in order. If the shoe doesn't contain an even number of cards then a partial deck will fill the rest.
1
2
3
4
5
6
7
// Initialize a deck or shoe of cards
void initDeck(vector<int> &deck)
{
    for (int i=0; i < deck.size(); ++i) {
        deck[i] = i % 52;
    }
}

Then you need to shuffle a deck:
1
2
3
4
5
6
7
8
9
10
11
// Shuffle bunch of cards
void shuffle(vector<int> &deck)
{
    // To shuffle a deck, pick a random card and swap it with
    // the first. Then pick a random remaining card and swap with
    // the second etc.
    for (int i=0; i < deck.size(); ++i) {
        int pos = i + rand() % (deck.size()-i);
        swap(deck[i], deck[pos]);
    }
}


And inside main, keep a variable that represents the current position in the deck. Again, be very specific with what this means. I chose it to represent the position of the next card to deal:
1
2
3
4
5
    vector<int> shoe(52);       // the shoe that cards are drawn from
    int pos = 0;                // position of next card to draw from shoe
    srand(time(NULL)) ;
    initDeck(shoe);
    shuffle(shoe);


Now what about the user's hand? How many cards are there? How big can it get? How many times can you draw? With a large enough shoe, the user could draw 21 aces for a value of 21. For all these reasons, I chose to make the hand a vector<int> also.

Now to compute the value of a hand. We have to do some arithmetic to convert the rank values to card values, but it gets harder since an ace can be worth 1 or 11. To handle this, I chose to always count aces as 11 but to count the number in the hand along the way. If the hand busts with that calculation, then subtract 10 for each ace (converting their value from 11 to 1) until you no longer bust or you run out of aces:
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
// Given a hand, compute the total value.
int handValue(const vector<int> &hand)
{
    int result = 0;
    int numAces=0;
    for (int i=0; i < hand.size(); ++i) {
        int val = value(hand[i]);
        if (val <= 8) {
            result += val + 2;  // two-ten
        } else if (val <= 11) {
            result += 10;       // jack, queen, king
        } else {
            result += 11;
            ++numAces;
        }
    }

    // If you busted counting aces as 11, then count then as 1's
    // until you no longer bust
    while (result > 21 && numAces--) {
        result -= 10;
    }

    return result;
}


Here is the final code. Notice how simple the main program becomes.
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
#include <iostream>
#include <stdlib.h>
#include <cstdio>
#include <ctime>
#include <string>
#include <vector>
using namespace std;

//calling decleration of welcome screen
void Welcome_Screen ();

// Cards in a deck are numbered 0-51. The first 13 are clubs,
// then spades, diamonds, and hearts.

// Given a card, return the number for it's suit and value
int suit(int card) { return card / 13; }
int value(int card) { return card % 13; }

// Convert suit and values to names
const char *suitName[] = {"Clubs","Spades","Diamonds","Hearts"};
const char *valueName[] = {
    "Two", "Three", "Four", "Five",
    "Six", "Seven", "Eight", "Nine",
    "Ten", "Jack", "Queen", "King", "Ace"
};

// Initialize a deck or shoe of cards
void initDeck(vector<int> &deck)
{
    for (int i=0; i < deck.size(); ++i) {
        deck[i] = i % 52;
    }
}


// Shuffle bunch of cards
void shuffle(vector<int> &deck)
{
    // To shuffle a deck, pick a random card and swap it with
    // the first. Then pick a random remaining card and swap with
    // the second etc.
    for (int i=0; i < deck.size(); ++i) {
        int pos = i + rand() % (deck.size()-i);
        swap(deck[i], deck[pos]);
    }
}

// Return the name of a card. Eg "Ace of Hearts"
string cardName(int card)
{
    string result = valueName[value(card)];
    result += " of ";
    result += suitName[suit(card)];
    return result;
}


// Given a hand, compute the total value.
int handValue(const vector<int> &hand)
{
    int result = 0;
    int numAces=0;
    for (int i=0; i < hand.size(); ++i) {
        int val = value(hand[i]);
        if (val <= 8) {
            result += val + 2;  // two-ten
        } else if (val <= 11) {
            result += 10;       // jack, queen, king
        } else {
            result += 11;
            ++numAces;
        }
    }

    // If you busted counting aces as 11, then count then as 1's
    // until you no longer bust
    while (result > 21 && numAces--) {
        result -= 10;
    }

    return result;
}



int main(){
    vector<int> shoe(52);       // the shoe that cards are drawn from
    int pos = 0;                // position of next card to draw from shoe

    vector<int> hand;           // user's current hand

    srand(time(NULL)) ;
    initDeck(shoe);
    shuffle(shoe);

    Welcome_Screen();

    cout << "Select enter key to begin"<<endl;

    hand.push_back(shoe[pos++]);
    hand.push_back(shoe[pos++]);

    cout <<"You pick up two cards"<<endl;
    cout << "Your Values are "<<endl;

    cout << "Card 1: " << cardName(hand[0]) << '\n';
    cout << "Card 2: " << cardName(hand[1]) << '\n';


    int total = handValue(hand);
    cout << "Your total so far is " << total;

    while (true) {

        cout <<" Do you want to hold or hit? "<<endl;
        cout <<"(Please enter either hold or hit) " <<endl;


        string choice;
        cin >> choice ;

        if (choice == "hit"){

            hand.push_back(shoe[pos++]);
            cout << "You draw " << cardName(hand.back());
            total = handValue(hand);
            cout << " Your total so far is " << total <<endl;

            if (total > 21){
                cout << "You have exceeded 21, computers turn" <<endl;
                break;
            }
        } else {
            cout << "You hold. Computer's turn\n";
            break;
        }
    }


    return 0;
}

void Welcome_Screen(){
    cout <<"\t\t\t******************************\t\t\t\t"<<endl;
    cout <<"\t\t\t****                      ****\t\t\t\t"<<endl;
    cout <<"\t\t\t*****Welcome to Blackjack*****\t\t\t\t"<<endl;
    cout <<"\t\t\t****  Powered by Rnjesus  ****\t\t\t\t"<<endl;
    cout <<"\t\t\t******************************\t\t\t\t"<<endl;
}


closed account (zNASE3v7)
Thank you for your support and the rest of the program! I'm not sure if I could have solved the swap sequence as quickly as you put all of this together! Awesome new design!
Topic archived. No new replies allowed.