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;
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;
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.
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 ( unsignedint 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...
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.
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/