been working all day and heres my issue....

Ok, been working all day on this. im on my deal function and im getting this same problem over and over again. i cant figure out 1) why my deal keeps giving 2 hands to the player and 2 to the dealer, and 2) why does it keep dealing only numbers 0, 5, 2, 3, 7. it seems to be completely ignoring the rest of the deck....


// BlackJack.cpp : Defines the entry point for the console application.
//

#include <stdafx.h>
#include <cstdlib> //needed for rand() and srand()
#include <iostream>
#include <string.h>
#include <ctime>
#include <cmath>
#include <algorithm>
using namespace std;

char cards[52]= { '2','2','2','2','3','3','3','3','4','4','4','4','5','5','5','5',
'6','6','6','6','7','7','7','7','8','8','8','8','9','9','9','9','T','T','T','T','J','J','J','J',
'Q','Q','Q','Q','K','K','K','K','A','A','A','A' };



int main() {

int cardsdelt;
int handlength;
int Dealer[2];
int Player1[2];
int h;

cout<<"Welcome to Justin's BlackJack table. Ill be your dealer!" <<endl;
cout<< "We will be playing 1 hand at a time with one deck of cards. I hope you will enjoy your stay. " <<endl;
srand (time(NULL));
std::random_shuffle(cards, cards + 52);

cout << "the random shuffle has produced the following output: " << cards << endl;

for (int h=0; h<2; h++)

{
Player1[h] = cards[h];
Dealer[h] = cards[h];

cout<< "Player 1 has: "<< Player1[h]<<endl;
cout<< "Dealer has: " << Dealer[h]<<endl;


}

}
2) why does it keep dealing only numbers 0, 5, 2, 3, 7. it seems to be completely ignoring the rest of the deck....
Look at the std::random_shuffle function again, you are missing something.

1) why my deal keeps giving 2 hands to the player and 2 to the dealer,
Look at your for loop again, it is dealing the same hand to both the player and the dealer. Also, where you put braces is very important.
There is nothing wrong with his use of random_shuffle().

Your loop is in error just as smilodon said...


The reason you are only getting certain numbers for output is that you are printing the ASCII number associated with the characters in your deck. The Dealer and Player should both be arrays of char:
1
2
char Dealer[2];
char Player1[2];

By the way, it is possible to have more than two cards in your "hand" in Blackjack.

I found the Wikipedia article exceedingly helpful when I wrote my Blackjack game:
http://en.wikipedia.org/wiki/Blackjack

Also, this thread:
http://www.cplusplus.com/forum/lounge/2783/

which gave me this link:
http://www.blackjackinfo.com/blackjack-rules.php

Hope this helps.
Just a tiny little aside here, you should cast time( NULL ) to unsigned when you call srand(). This is because the declaration of srand is void srand ( unsigned int seed ). Just a tip, avoiding warnings could save you some marks further in your education, and makes your code look more clean and stable.
@Duoas: When I looked at randon_shuffle(), I noticed that it was overloaded with a third option, you could pass it a random number generator, I assumed the first option would generate the same set over and over again.
I dont know much about casino BJ, but i think you need to (except all the rest) declare a boolean array and check if a card was already picked up. Of course if we use only 1 deck...

Something like this (kinda pseudocode)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
char deck[52] = {'2','3'....'A','2'.....'A'};
char player[2];
char dealer[2];
bool validation[52]; //array[i] = false; 0<=i<=51;
/*static*/ int number;   //random card picker

for(int i=0;i<player lenght;i++)  // here player lenght = 2
{
    number = // get random number here , between 0 and 51;
    if(validation[number]==false)
    {
         player[i] = deck[i]
         validation[i] = true;
    }
     else
    {
         do{ number = /*randomize*/ }while(validation[number]==true); //pick the random number all the time unless it wasnt picked in the previous draw
          player[i] = deck[i]
         validation[i] = true;
    }
}


Then you do the same for dealer and compare their "results"
You can also assign an array of "points" - imo its better than bunch of IFs something like this:
1
2
char deck[5] = {'T','J','Q','K','A'};
int points[5] = {10,1,2,3,11};


and while picking those 2 cards for player/dealer you pick simultanously 2 values from 'points' array and sum it for the score.

Something like this:
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
int scoreForPlayer = 0;
int scoreForDealer = 0;
char deck[52] = {'2','3'....'A','2'.....'A'};
int points[52] = {2,3,4,....11,2,....11};
char player[2];
char dealer[2];
bool validation[52]; //array[i] = false; 0<=i<=51;
/*static*/ int number;   //random card picker

for(int i=0;i<player lenght;i++)  // here player lenght = 2
{
    number = // get random number here , between 0 and 51;
    if(validation[number]==false)
    {
         player[i] = deck[i];
         scoreForPlayer += points[number];  // automatically getting the score
         validation[i] = true;
    }
     else
    {
         do{ number = /*randomize*/ }while(validation[number]==true); //pick the random number all the time unless it wasnt picked in the previous draw
          player[i] = deck[i];
         scoreForPlayer += points[number];  // automatically getting the score
         validation[i] = true;
    }
}


Hope this helps
Regards
JK


BTW
I used that alghoritm to generate bridge hands in my bridge generator - of course it has nothing to do with BJ, but the deck shuffle alghoritm is 100% humanlike simulation. I've tested it for 2kk different combinations(took only 20 minutes) - scores were the same (+/- 0,1%) More on http://www.cplusplus.com/forum/windows/12287/
Last edited on
Topic archived. No new replies allowed.