Console Blackjack (A new problem)

Problem with dealer hitting

Page 1 of 1

3 Replies - 1430 Views - Last Post: 14 July 2009 - 11:22 AM Rate Topic: -----

#1 chuck981996   User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 119
  • Joined: 03-July 08

Console Blackjack (A new problem)

Posted 12 July 2009 - 09:57 PM

The story:
Hi all! I am *trying* to make a console version of Blackjack (A.K.A. 21, Pontoon). I have so far done most of it, except being able to play again (simple do-while loop) and the options such as split, double down and surrender (A bit harder ;) ).

The Problem:
The problem with my code is not that it doesn't compile, but that the dealer NEVER hits. I think it's a problem with the loop beginning on line #219 (I have marked it with a comment (if you want to search the code, search "^&%4589", without the quotes)). This line is located in the main function, in the second do-while loop. As I siad I THINK it is this loop, but I am not sure. Also, I know that ATM there are a few more problems with my code, but I am working on this first :D

The Code:

#include <iostream>
#include <string.h>

#define DECK_SIZE 208
#define MAX_TYPE 16
 
using namespace std;

void createDeck(int * deck)
{
	int count = 0;
	int card = 2;

	for(int i = 0; i < DECK_SIZE; i++)
	{
		if(count == MAX_TYPE)
		{
			card++;
			count = 0;
		}
		deck[i] = card;
		count++;
	}
}

void shuffleDeck(int * deck)
{
	int r;
	int buffer;

	srand (time(NULL));

	//begin shuffle loop
	for(int x = 0; x < 1000; x++)
	{
		for(int i = 0; i < DECK_SIZE; i++)
		{
			r = (rand() % DECK_SIZE);

			//begin swap
			buffer = deck[i];
			deck[i] = deck[r];
			deck[r] = buffer;
			//end swap
		}
	} //end shuffle loop
}

void cardsGone(int x, int * deck) //x = amount of cards
{
	int buffer[x];
	for(int a = 0; a<x; a++)
		buffer[a] = deck[(x-a-1)];

	for(int i = 0; i<DECK_SIZE; i++)
		deck[i] = deck[(i + x)];

	for(int b = 0; b<x; b++)
		deck[(DECK_SIZE-(b+1))] = buffer[b];
}

void printDeck(int * deck, bool newLine) //Testing function. newLine = true if newlines are wanted
{
	for(int i = 0; i < DECK_SIZE; i++)
		cout << deck[i] << ' ';

	if(newLine)
		cout << "\n\n";
}

void printHand(int * hand, int handSize) //hand = desired hand to print; handSize = cards in hand
{
	for(int a = 0; a < handSize; a++)
	{
		switch (hand[a])
		{
			case 11:
				cout << "Jack ";
				break;

			case 12:
				cout << "Queen ";
				break;

			case 13:
				cout << "King ";
				break;

			case 14:
				cout << "Ace ";
				break;

			default:
				cout << hand[a] << ' ';
				break;
		}
	}
}

int checkBust(int * hand, int handSize, bool * bust)
{
	bool ace = false;
	int amount;
	int worth;

	for(int i = 0; i < handSize; i++)
	{
		worth = hand[i];
		switch(worth)
		{
			case 11:
			case 12:
			case 13:
				worth = 10;
				break;

			case 14:
				ace = true;
				worth = 11;
				break;
		}
		amount += worth;
	}

	amount -= 1;

	if((amount > 21) && ace)
		amount -= 10;

	if(amount > 21)
		*bust = true;
	else *bust = false;

	return amount;
}

int main()
{
	int deck[DECK_SIZE]; //The deck

	int playerHandSize = 0; //Amount of cards in player's hand
	int playerHand[10]; //Player's hand
	int playerAmount; //Value of player's hand
	bool playerBust = false; //Is the player bust?
	bool playerStand = false; //Has the player stood?

	int dealerHandSize = 0; //Amount of cards in dealer's hand
	int dealerHand[10]; //Dealer's hand
	int dealerAmount; //Value of dealer's hand
	bool dealerBust = false; //Is the dealer bust?
	bool dealerStand = false; //Has the dealer stood?
	bool dealerAce = false; //Does the dealer an ace?

	int chips;
	int bet;
	//bool replay; //For later

	string input; //Either Hit or Stand

	cout << "Welcome to Blackjack! The deck has " << (DECK_SIZE / 52) << " packs of cards in it (" << DECK_SIZE << " cards)\n";
	cout << "How many chips would you like to start with? ";
	cin >> chips;
	cout << "\n";

	createDeck(deck); //Create a deck
	shuffleDeck(deck); //Shuff;e deck

	cout << "Place your bet...";
	cin >> bet;
	chips -= bet;

	for(int i = 0; i < 2; i++) //Deal cards
	{
		dealerHand[i] = deck[i];
		dealerHandSize++;
		playerHand[i] = deck[(i+1)];
		playerHandSize++;
	}
	cardsGone(4, deck); //Take dealt cards from deck

	do
	{
		cout << "The dealer's cards are: "; //Show (one of) dealer's cards
		printHand(dealerHand, 1);
		for(int q = 0; q < (dealerHandSize - 1); q++)
			cout << "? ";

		cout << "\nYour cards are: "; //Show player's cards
		printHand(playerHand, playerHandSize);
		cout << "\n";

		cout << "Would you like to hit, or stand? ";
		cin >> input;
		cout << "\n";

		if((input == "hit") || (input == "Hit") || (input == "HIT"))
		{
			playerHand[playerHandSize] = deck[0]; //Deal player 1 card
			playerHandSize++;
			cardsGone(1, deck);
		}
		else if((input == "stand") || (input == "Stand") || (input == "STAND"))
			playerStand = true;
		else
			cout << "That command isn't recognised, please input either 'Hit' or 'Stand'";

		playerAmount = checkBust(playerHand, playerHandSize, &playerBust); //Calculate if player is bust
	} while(!playerBust && !playerStand); //Do while player isn't bust & player hasn't stood

	do
	{
		dealerAmount = checkBust(dealerHand, dealerHandSize, &dealerBust);
		for(int u = 0; u < dealerHandSize; u++) //In case dealer has a soft 17
		{
			if(dealerHand[u] == 14)
				dealerAce = true;
		}

		//I think the problem is with this if - else loop ^&%4589
		if((dealerAmount >= 17) && !dealerAce) //If dealer has a hard 17, stand
			dealerStand = true;
		else //Otherwise, hit
		{
			dealerHand[dealerHandSize] = deck[0];
			dealerHandSize++;
			cardsGone(1, deck);
		}
		printHand(dealerHand, dealerHandSize); //TEST
	} while(!dealerStand); //Do while the dealer hasn't stood

	if(playerBust)
	{
		cout <<"You are BUST!" << " You went over 21 by " << (playerAmount - 21);
		cout <<"\nYour final cards were: ";
		printHand(playerHand, playerHandSize);
		cout << "\nThe dealer's final cards were: ";
		printHand(dealerHand, dealerHandSize);
		cout << "\n";
	}

	return 0;
}



Any help would be appreciated :)
Thankyou :wub:

Is This A Good Question/Topic? 0
  • +

Replies To: Console Blackjack (A new problem)

#2 frostyraver   User is offline

  • D.I.C Head
  • member icon

Reputation: 13
  • View blog
  • Posts: 195
  • Joined: 21-April 09

Re: Console Blackjack (A new problem)

Posted 13 July 2009 - 08:50 PM

Set a break point, within the hit loop, and tell me what you get, if it stops then the program is reading the loop.

This post has been edited by frostyraver: 13 July 2009 - 08:52 PM

Was This Post Helpful? 0
  • +
  • -

#3 chuck981996   User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 119
  • Joined: 03-July 08

Re: Console Blackjack (A new problem)

Posted 14 July 2009 - 03:35 AM

View Postfrostyraver, on 13 Jul, 2009 - 07:50 PM, said:

Set a break point, within the hit loop, and tell me what you get, if it stops then the program is reading the loop.


OK, I tried what you said, even if the dealer has under 17, it doesn't hit...
Was This Post Helpful? 0
  • +
  • -

#4 wildgoose   User is offline

  • D.I.C Regular
  • member icon

Reputation: 67
  • View blog
  • Posts: 468
  • Joined: 29-June 09

Re: Console Blackjack (A new problem)

Posted 14 July 2009 - 11:22 AM

Just looking at your modified code.

By the way, you don't need to shuffle your deck 1000 times! Once was sufficient! Granted you're using rnd(), which is a terrible RNG.
You do realize that ace is worth 1 or 11, not just 11 ?

*** Does not appear you initialize {dealerStand}! You set it for 17+ if no Ace but nothing otherwise, unless its hiding in your code. So it would have garbage on some compilers or a zero on others.

Be consistent in your function calling. You mostly use cardstack pointer, # of cards but...

void cardsGone(int x, int * deck)

...is contrary to that!

MORE COMMENTS IN YOUR CODE!!!!

This post has been edited by wildgoose: 14 July 2009 - 11:30 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1