Original Post
So, I'm Coel, nice to meet you all. I'm new here to gamedev and studying C++ using GameInstitue however I dont like the fact they use timeblocks to post questions ect. So here I am ... My problem, I have a blackjack game that doesn't shuffle correctly. What seems to happen is it shuffles the picture cards, but not the values. Here is all the code that i think you will need
//===========================
//=========BlackJack=========
//===========================
#include <iostream>
#include <vector>
#include <ctime>
#include <string>
using namespace std;
typedef std::vector <int> CardVec; //CardVec = vector <int>
const int NUM_CARDS = 52;
const std::string CardNames[52] =
{
"2H", "3H", "4H", "5H", "6H", "7H", "8H", "9H", "10H", "JH", "QH", "KH", "AH",
"2D", "3D", "4D", "5D", "6D", "7D", "8D", "9D", "10D", "JD", "QD", "KD", "AD",
"2S", "3S", "4S", "5S", "6S", "7S", "8S", "9S", "10S", "JS", "QS", "KS", "AS",
"2C", "3C", "4C", "5C", "6C", "7C", "8C", "9C", "10C", "JC", "QC", "KC", "AC"
};
const int CardValues[52] =
{
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
};
void InitDeck(CardVec& deck);
void ShuffleDeck(CardVec& deck);
int SumCardValues(const CardVec &cards);
void PrintCards(const CardVec &cards, bool dealer);
void PlayGame();
int main()
{
srand((unsigned int)time(0));
int play = 0;
bool quit = false;
while ( !quit )
{
cout << "(1)Play Blackjack (2)Quit ";
cin >> play;
if (play == 1)
PlayGame();
else
{
cout << "Exiting Game." << endl;
quit = true;
}
}
return 0;
}
void InitDeck(CardVec& deck)
{
for(int i = 0; i < NUM_CARDS; ++i)
deck = i;
}
void ShuffleDeck(CardVec& deck)
{
int temp = 0;
for (int i = 0; i < 200; ++i) //shuffle 200 times
{
int random = rand() % 52; //Pick a random number from 1 to 52
temp = deck[random]; //store deck[random] into temp
int random2 = rand() % 52; //Pick another random number
deck[random] = deck[random2]; //store deck[random2] into deck[random]
deck[random2] = temp; //put temp back into deck[random2]
}
}
int SumCardValues(const CardVec &cards)
{
int sum = 0;
for (int i = 0; i < cards.size(); ++i)
sum += CardValues[cards];
return sum;
}
void PrintCards(const CardVec &cards, bool dealer)
{
if (dealer == false)
{
cout << "Your cards: ";
for (int i = 0; i < cards.size(); ++i)
cout << CardNames[cards] << " ";
cout << endl;
}
else
{
cout << "Dealers cards: ";
for (int i = 0; i < cards.size(); ++i)
cout << CardNames[cards] << " ";
cout << endl;
}
}
void PlayGame()
{
//create conditions
bool stand = false; //stand = true
int hit = 0;
//create and initialize the deck
CardVec deck(52); //vector <int> deck(52)
InitDeck(deck); //Function InitDeck
ShuffleDeck(deck); //Shuffle the deck
//Create a vector of cards for each player
CardVec playersCards;
CardVec dealersCards;
//Deal each player 2 cards and display the hands
playersCards.push_back( deck.back() ); //create a new element with the value of the last element
deck.pop_back(); //in deck and then remove it from deck
playersCards.push_back( deck.back() );
deck.pop_back();
PrintCards(playersCards, false);
dealersCards.push_back( deck.back() );
deck.pop_back();
PrintCards(dealersCards, true);
dealersCards.push_back( deck.back() );
deck.pop_back();
while ( !stand )
{
cout << "(1)Hit (2)Stand: ";
cin >> hit;
if (hit == 1)
{
playersCards.push_back( deck.back() );
deck.pop_back();
PrintCards(playersCards, false);
}
else
stand = true;
}
cout << SumCardValues(dealersCards);
}