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