Jump to content
  • Advertisement
Sign in to follow this  
ultigma

New programmer having problems

This topic is 3794 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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);
}

Share this post


Link to post
Share on other sites
Advertisement
I don't see anything very wrong with your shuffling mechanism. However, it doesn't generate a uniformly random permutation. If you want uniformity, you can do this:
for(int i=0;i<51;++i) {
int random = i + rand()%(52-i);
swap(deck, deck[random]);
}




You can also use std::random_shuffle, which does pretty much the same thing.

Share this post


Link to post
Share on other sites
Thanks for the reply alvaro.

The actual shuffling works, it's what happens to the values.

for example, in the console window, once you stand, you may end up with (2 of spades and 7 of clubs) which should evaluate to 9. But it does not output that result, it could be 14 or even 9. So the values do not correspond with the card.

And i can't see why this is happening.

the array 'deck' is given the integers 0 - 51 (0 being 2 of hearts, 51 being Ace of clubs) so ValueCard[deck], assuming the first integer in the deck is ... 11 (King of hearts) ValueCard[deck] should have a value of 10

But this is not happening.

Hope this makes sense. =)

Share this post


Link to post
Share on other sites
hey dude,

i didnt find anything wrong with your code.
and the only output in the end is the sum of the values of the DEALERS(!) cards,
not yours. (at least this is what i get from the last line from playgame()).

below is some modified code ,with some additional output for debugging, i hope you will find it useful


//===========================
//=========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;
//cout<< "sad";

while ( quit == false )
{
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] << "(" << CardValues[cards] << ")"<< " ";

cout << endl;
}
else
{
cout << "Dealers cards: ";
for (int i = 0; i < cards.size(); ++i)
cout << CardNames[cards] << "(" << CardValues[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

// PrintCards(deck, false);



//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 << "you have: " << SumCardValues(playersCards) << "\n";
cout << "dealer has: " << SumCardValues(dealersCards) << "\n";

}



PS: why dont you try use STL map for storing cards (their name and value). see here for info

Share this post


Link to post
Share on other sites
Thanks DpakoH,

I didn't realise it was the dealers cards i was finding the value of ><

Thanks for pointing it out XD
It was driving me crazy.

Working fine now =)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!