Thread: Blackjack!

  1. #1
    Registered User Dr. Bebop's Avatar
    Join Date
    Sep 2002
    Posts
    96

    Blackjack!

    I was never really into games, but here's a blackjack game that I wrote today for practice. It's not meant to be really high tech or fully featured, just a little game for fun. What do you guys think and where can I improve it?
    Code:
    //
    // BLACKJACK!
    //		by Dr.Bebop
    //
    // Rules: Each player gets at most 5 cards. If the total
    //        value of the cards is over 21 the player busts.
    //        If a player gets a total of 21 with the first
    //        two cards then they get a Blackjack, the highest
    //        score in the game. Otherwise the winner is the
    //        one with the highest total less than or equal to
    //        21. In the event of a tie, the dealer wins the
    //        hand.
    //
    #include <algorithm> // For random_shuffle().
    #include <iostream>  // For cout and cin.
    #include <cstdlib>   // For srand()
    #include <cctype>    // For tolower().
    #include <ctime>     // For time().
    using namespace std;
    
    const int N_CARDS = 52;  // Total number of cards.
    int curr_card = 0;       // Current card in the deck, from 0 to 51
    
    int deck[N_CARDS] = {    // Card values. Suit is ignored.
    	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,
    };
    
    struct Part {                // Stuff for each player, number of cards,
    	int n;               // the running total, and the individial
    	int total;           // card values (for program upgrades).
    	int cards[5];
    };
    
    // Take the deck of card values and mix them up randomly.
    //
    void shuffle_deck();
    // Deal the first two cards.
    //
    Part first_deal( Part player );
    // Deal out one card from the top of the deck (determined by curr_card)
    //
    int deal_one();
    // After the first two cards are dealt, add another if the player wants.
    //
    Part hit_player( Part player );
    // Test each player's hand and print who wins.
    //
    void check_score( Part player, Part dealer );
    
    int main() {
    	char play_again = 'y';
    	char hit = 0;
    	
    	// Seed the random number generator.
    	srand( (unsigned int)time( (time_t *)NULL ) );
    
    	while( tolower( play_again ) == 'y' ) {
    		Part dealer = {0}, player = {0};
    
    		cout<< "Shuffling..." <<endl;
    		shuffle_deck();
    		cout<< "First deal..." <<endl;
    
    		// Deal to players.
    		player = first_deal( player );
    		dealer = first_deal( dealer );
    
    		// Begin play.
    		while( true ) {
    			cout<< "Your total is: " << player.total <<endl;
    			cout<< "Hit? (y/n): "<<flush;
    			cin.get( hit ).ignore();
    
    			// Dealer AI. Very simple, based on casino rules.
    			while( dealer.total < 16 ) {
    				dealer = hit_player( dealer );
    				if( dealer.n > 4 )
    					break;
    			}
    
    			// Player options.
    			hit = tolower( hit );
    			if( hit == 'y' ) {
    				player = hit_player( player );
    				if( player.total > 21 || player.n > 4 )
    					break;
    			}
    			else if ( hit == 'n' )
    				break;
    			else
    				cerr<< "Unknown selection" <<endl;
    		}
    		// Print the winner.
    		check_score( player, dealer );
    
    		cout<< "Play again? (y/n): "<<flush;
    		cin.get( play_again ).ignore();
    	}
    	return 0;
    }
    
    void shuffle_deck() {
    	random_shuffle( deck, (deck + N_CARDS) );
    	// Reset curr_card to the top of the deck.
    	curr_card = 0;
    }
    
    Part first_deal( Part player )
    {
    	player.cards[player.n++] = deal_one();
    	player.cards[player.n++] = deal_one();
    	// n = 2 after the previous two lines. It makes for easier blackjack tests.
    	player.total += (player.cards[0] + player.cards[1]);
    
    	return player;
    }
    
    int deal_one() {
    	if( curr_card == 52 ) // If there are no more cards.
    		shuffle_deck();   // Reshuffle the deck, start over at the top.
    	return deck[curr_card++];
    }
    
    Part hit_player( Part player )
    {
    	int card = deal_one();
    
    	if( card == 11 && (player.total + card) > 21 ) // Check for an ace.
    		card = 1;
    	player.cards[player.n++] = card;
    	player.total += card;
    
    	return player;
    }
    
    void check_score( Part player, Part dealer )
    {
    	if( dealer.total == 21 && dealer.n == 2 )      // Dealer blackjack.
    		cout<< "Dealer blackjack. Dealer wins." <<endl;
    	else if( player.total == 21 && player.n == 2 ) // Player blackjack.
    		cout<< "BLACKJACK! You win!" <<endl;
    	else if( player.total > 21 )                   // Player bust.
    		cout<< "You busted. Dealer wins." <<endl;
    	else if( dealer.total > 21 )                   // Dealer bust.
    		cout<< "Dealer busts. You win!" <<endl;
    	else if( dealer.total < player.total )         // Player high value.
    		cout<< "You win!" <<endl;
    	else if( player.total < dealer.total )         // Dealer high value.
    		cout<< "Dealer wins." <<endl;
    	else                                           // House wins in a tie.
    		cout<< "Tie. Dealer wins." <<endl;
    }
    Bebop
    Processing error: Stupidity detected.
    ------------------------------
    Dr. Bebop
    Windows XP Professional Ed.
    Microsoft Visual Studio 6

  2. #2
    Registered User Kirdra's Avatar
    Join Date
    Aug 2002
    Posts
    105
    I think you've done a good job

    Maybe add a clear screen and score board to it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple Blackjack Program
    By saber1357 in forum C Programming
    Replies: 1
    Last Post: 03-28-2009, 03:19 PM
  2. Help with Blackjack program
    By sugie in forum C++ Programming
    Replies: 1
    Last Post: 04-30-2005, 12:30 AM
  3. BlackJack
    By gsoft in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 01-04-2005, 01:09 PM
  4. Blackjack
    By the_head in forum C Programming
    Replies: 1
    Last Post: 08-03-2002, 08:57 AM
  5. BlackJack Program...
    By 67stangman in forum C++ Programming
    Replies: 3
    Last Post: 05-06-2002, 10:44 PM