#include <iostream.h>
#include <stdlib.h>
#include <time.h>
#include <fstream.h>
#include <string.h>
#include <windows.h> // WinApi header
#include <ctype.h>
class game
{
public:
game(); //default constuctor
void blackjack();
protected:
void shuffle(); //prototype for shuffle prog.
void hit(); //deals one card at a time
void first_deal(); //deals 2 cards to each player
void display(); //displays cards, score, and money
void player_hit_stay(); //player chooses to hit or stay
void dealer_hit_stay(); //automated dealer hit/stay, hit if total <= 16
void player_hand_totaler(); //sums cards in player's hand
void dealer_hand_totaler(); //sums cards in dealer's hand
void ace_change(); //if total is > 12 then changes aces (11's) to 1's. reruns totaler everytime it changes an ace
void bust_check(); //checks if either player busts
void reshuffle(); //reruns shuffle if current card is 52
void clear_screen(); //couts 20 clear lines
void clear_hands(); // clears arrays for player and dealer hands, resets bust, winner, hand totals, and bet amount
void winner_check(); //checks for busts or (if no bust) greater hand total
void get_name(); //cin's the person's name
void intro(); //displays program picture and author info
void bet(); //allows for players to enter a bet. checks for too high bets and negative bets.
void win_money(); //if player wins, adds 2*bet to total, 3*bet if dealer busts
int deck[52], player_hand[15], dealer_hand[15]; //sets array for deck, and for each player's hand
int current_card, player_total, dealer_total; //current card # (in deck), and player/dealer hand total
int player_card_number, dealer_card_number; //card # in player/dealers's hand
int player; //sets 1 for player and 2 for dealer, affects turn
int bust; //if total is >21, sets bust=1 for player and =2 for dealer.
int winner; //set by winner_check, 1 for player, 2 for dealer, 3 for tie
int bet_amount; //amount the player bets. entered in bet function
char player_hit; //player enters y or n if they want to hit
public:
void display_header(); //file display
void display_record(); //display name and money from record
int money_total; //total money the player currently has
char player_name[50]; //player can enter their name, up to 49 characters long
};
game::game() //default consturctor
{
int i=0;
for(i=0; i<=51; i++) //initialzes deck
{
deck[i]=0;
}
for(i=0; i<=14; i++) //initializes player/dealer hand arrays
{
player_hand[i]=0;
dealer_hand[i]=0;
}
current_card = 0;
player_total = 0;
dealer_total = 0;
player_card_number = 0;
dealer_card_number = 0;
player = 1;
player_hit = ' ';
bust = 0;
winner = 0;
money_total = 100;
}
void game::intro()
{
cout << " ------------- -------------\n"
<< " | ----- J| | A|\n"
<< " | '<' | | A |\n"
<< " | - | | A A |\n"
<< " | / \\ | | A A |\n"
<< " | +-( J )-+ | | AAAAAAA |\n"
<< " | \\ / | | A A |\n"
<< " | | | | | A A |\n"
<< " |J - - | |A |\n"
<< " ------------- -------------\n"
<< " BLACKJACK 21!\n";
cout << "\n";
get_name();
}
void game::get_name()
{
cout << "Enter your name? ";
cin.getline(player_name, 50);
//if(player_name )
{
cout << "You must enter a name and not a digit!" << "\n""\n";
clear_screen();
cout << "Enter your name? ";
cin.getline(player_name, 50);
}
}
//player make bet
void game::bet()
{
cout << "\n\nyou have $" << money_total;
cout << "\nplease only bet in whole dollar amounts.";
cout << "\nhow much do want to you bet? ";
cin >> bet_amount;
//checks bet amount to be in range of money
while ((bet_amount > money_total)||(bet_amount < 0))
{
//if bet is over money in players bank
if (bet_amount > money_total)
{
cout << "\nyou bet more money than you have. no, you can't bet your car keys. fool.\n\n";
clear_screen();
cout << "\n\nyou have $" << money_total;
cout << "\nplease only bet in whole dollar amounts.";
cout << "\nhow much do want to you bet? ";
cin >> bet_amount;
}
//can't bet a negative amount
else if (bet_amount < 0)
{
cout << "\nyou can't bet negative money!\n\n";
clear_screen();
cout << "\n\nyou have $" << money_total;
cout << "\nplease only bet in whole dollar amounts.";
cout << "\nhow much do want to you bet? ";
cin >> bet_amount;
}
}
money_total = money_total - bet_amount;
}
void game::blackjack() //"main" part of program
{
intro();
char play_again = 'y';
shuffle();
do
{
bet();
clear_screen();
first_deal();
player_hit_stay();
if (bust == 1)
{
clear_screen();
cout << "you bust!\n\n";
//winner = 2;
}
else if (bust == 0)
{
dealer_hit_stay();
if (bust == 2)
{
clear_screen();
cout << "dealer busts!\n\n";
//winner = 1;
}
else if (bust == 0)
{
clear_screen();
cout << "Dealer stays.\n\n";
}
}
winner_check();//here winner_check will be called
if (money_total > 0)
{
cout << "\n\nanother round? (y for yes, n for no) ";
cin >> play_again;
while ((play_again != 'y')&&(play_again != 'Y')&&(play_again != 'n')&&(play_again != 'N'))
{
cout << "\n\nyou did not enter a y or n. please try again: ";
cin >> play_again;
}
clear_hands();
clear_screen();
}
} //keep playing game
while ((money_total > 0)&&((play_again == 'y')||(play_again == 'Y')));
clear_screen();
//to save player name and money win to record
// if((money_total > 0) && (play_again = 'n')&&(play_again = 'N'))
//{
// cout << "Save your name and winnings (Y for yes and N for n)";
// cin >> play_again;
//if((play_again = 'y')&&(play_again = 'Y'))
// {
// ofstream tofile; //save_name();
//}
//}
if (money_total <= 0)
{
cout << "\n\nsorry, you ran out of money. goodbye!\n";
}
clear_screen();
cout << "\n";
}
void game::shuffle()
{
int unshuffled[] = {2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 11, 12, 12, 12, 12, 13, 13, 13, 13, 14, 14, 14,14};
//sets the unshuffled deck.
int picked = 0; //number variable, used in function
srand(time(0));
for(int x=0; x<=51; x++)
{
picked = (1 + rand() % 52) ;
while (unshuffled[picked -1] == 0)
//tests if card picked was already picked (set to 0). if so, chooses another.
{
picked = (1 + rand() % 52) ;
}
deck[x] = unshuffled[picked - 1]; //sets card in deck used to card in unshuffled deck.
unshuffled[picked -1] = 0; //"deletes" card in unshuffled deck so it cannot be used again.
current_card=0;
}
}
void game::hit()
{
reshuffle();
if (player==1)
{
player_hand[player_card_number]=deck[current_card];
player_card_number++;
player_hand_totaler();
}
if (player==2)
{
dealer_hand[dealer_card_number]=deck[current_card];
dealer_card_number++;
dealer_hand_totaler();
}
current_card++;
ace_change();
bust_check();
}
void game::first_deal()
{
player=1;
hit();
hit();
player++;
hit();
hit();
}
void game::display()
{
int c=0;
if (winner == 0)
{
cout << "Dealer:\n";
}
else
{
cout << "Dealer: (total) " << dealer_total << "\n";
}
for (c=0; c<=14; c++)//draws top of card
{
if (dealer_hand[c] != 0)
{
cout << " +---+ ";
}
}
cout << endl;
for (c=0; c<=14; c++)//draws top of card
{
if (dealer_hand[c] != 0)
{
cout << " | | ";
}
}
cout << endl;
for (c=0; c<=14; c++)
{
//displays a J, Q, or K instead of card values 12, 13, and 14
//J-K is 12-14 so that the Ace can be 11 automatically
if (dealer_hand[c] != 0)
{
cout << " | ";
if ((winner == 0)&&(c == 0))
{
cout << "? ";
}
else if (dealer_hand[c]==10)
{
cout << "10";
}
else if ((dealer_hand[c]==11)||(dealer_hand[c]==1))
{
cout << "A ";
}
else if (dealer_hand[c]==12)
{
cout << "J ";
}
else if (dealer_hand[c]==13)
{
cout << "Q ";
}
else if (dealer_hand[c]==14)
{
cout << "K ";
}
else
{
cout << dealer_hand[c] << " ";
}
cout << "| ";
}
}
cout << endl;
for (c=0; c<=14; c++)//draws middle of card
{
if (dealer_hand[c] != 0)
{
cout << " | | ";
}
}
cout << endl;
for (c=0; c<=14; c++)//draws bottom of card
{
if (dealer_hand[c] != 0)
{
cout << " +---+ ";
}
}
//displays players name, total of hand and money
cout << "\n\n" << player_name << ": (total) " << player_total << ", total money: $" << money_total << "\n";
for (c=0; c<=14; c++)//draws top of card
{
if (player_hand[c] != 0)
{
cout << " +---+ ";
}
}
cout << endl;
for (c=0; c<=14; c++)//draws top of card
{
if (player_hand[c] != 0)
{
cout << " | | ";
}
}
cout << endl;
for (c=0; c<=14; c++)
{
//displays a J, Q, or K instead of card values 12, 13, and 14
//J-K is 12-14 so that the Ace can be 11 automatically
if (player_hand[c] != 0)
{
cout << " | ";
if (player_hand[c]==10)
{
cout << "10";
}
else if ((player_hand[c]==11)||(player_hand[c]==1))
{
cout << "A ";
}
else if (player_hand[c]==12)
{
cout << "J ";
}
else if (player_hand[c]==13)
{
cout << "Q ";
}
else if (player_hand[c]==14)
{
cout << "K ";
}
else
{
cout << player_hand[c] << " ";
}
cout << "| ";
}
}
cout << endl;
for (c=0; c<=14; c++)//draws middle of card
{
if (player_hand[c] != 0)
{
cout << " | | ";
}
}
cout << endl;
for (c=0; c<=14; c++)//draws bottom of card
{
if (player_hand[c] != 0)
{
cout << " +---+ ";
}
}
}
void game::player_hit_stay()
{
player=1;
player_hit = 'y';
while (((player_hit == 'y')||(player_hit == 'Y'))&&(bust == 0))
{
clear_screen();
display();
cout << "\n\nwould you like to hit? (y for yes or n for no) ";
cin >> player_hit;
while ((player_hit != 'y')&&(player_hit != 'Y')&&(player_hit != 'n')&&(player_hit != 'N'))
{
cout << "\n\nyou did not enter a y or n. please try again: ";
cin >> player_hit;
}
if ((player_hit == 'y')||(player_hit == 'Y'))
{
hit();
}
}
}
void game::dealer_hit_stay()
{
player = 2;
while ((dealer_total < 17)&&(bust != 2))
{
if (bust == 0)
{
hit();
clear_screen();
cout << "\nDealer hits!\n";
display();
}
}
}
//converts k,q,j to value of 10 for player hand
void game::player_hand_totaler()
{
int c=0;
player_total=0;
for(c=0; c<=14; c++) //totals player hand
{
//this if statement adds 10 for J, Q, and K instead of 12, 13, and 14
//J-K is 12-14 so that the Ace can be 11 automatically
if ((player_hand[c]==12)||(player_hand[c]==13)||(player_hand[c]==14))
{
player_total=player_total+10;
}
else // adds current card
{
player_total=player_total+player_hand[c];
}
}
//return;
}
//converts k,q,j to value of 10 for dealer hand
void game::dealer_hand_totaler()
{
int c=0;
dealer_total=0;
for(c=0; c<=14; c++) //totals player hand
{
//this if statement adds 10 for J, Q, and K instead of 12, 13, and 14
//J-K is 12-14 so that the Ace can be 11 automatically
if ((dealer_hand[c]==12)||(dealer_hand[c]==13)||(dealer_hand[c]==14))
{
dealer_total=dealer_total+10;
}
else //else adds current card
{
dealer_total=dealer_total+dealer_hand[c];
}
}
}
//clears the screen
void game::clear_screen()
{
system("cls");
}
//clears cards for new hand
void game::clear_hands()
{
int c=0;
player_card_number = 0;
dealer_card_number = 0;
player_total = 0;
dealer_total = 0;
bust = 0;
winner = 0;
bet_amount = 0;
for (c=0; c<=14; c++)
{
player_hand[c] = 0;
}
for (c=0; c<=14; c++)
{
dealer_hand[c] = 0;
}
}
//checks for ace to be 1 or 11
void game::ace_change()
{
int c=0;
if ((player_total >= 22)&&(player_total != 21))
{
for (c=0; c <=14; c++)
{
if (player_hand[c]==11)
{
player_hand[c]=1;
player_hand_totaler();
}
}
}
if ((dealer_total >= 22)&&(dealer_total != 21))
{
for (c=0; c <=14; c++)
{
if (dealer_hand[c]==11)
{
dealer_hand[c]=1;
dealer_hand_totaler();
}
}
}
player_hand_totaler();
dealer_hand_totaler();
}
//reshuffle cards for new hand
void game::reshuffle()
{
if (current_card == 52)
{
shuffle();
current_card=0;
}
}
//checks for bust
void game::bust_check()
{
if (player_total >= 22)
{
bust = 1;
}
if (dealer_total >= 22)
{
bust = 2;
}
}
//in this blackjack game, your $ is already subtracted. therefore,
//when you lose, no money is subtracted because your bet has already been subtracted.
void game::winner_check()
{
if (bust == 0)
{
if (player_total > dealer_total)
{
winner = 1;
win_money();
display();
cout << "\n\nyou win!\n\n";
}
else if (player_total < dealer_total)
{
winner = 2;
display();
cout << "\n\nyou lose!\n\n";
}
else if (player_total == dealer_total)
{
winner = 3;
money_total = money_total + bet_amount; //you dont lose anything for a tie.
display();
cout << "\n\ntie!\n\n";
}
}
else
{
if (bust == 1)
{
winner = 2;
display();
cout << "\n\nyou lose!\n\n";
}
if (bust == 2)
{
winner = 1;
win_money();
display();
cout << "\n\nyou win!\n\n";
}
}
}
//determines money win
void game::win_money()
{
if (bust == 2)//player gets twice his bet back if the dealer busts
{
money_total = money_total + (3 * bet_amount);
}
else
{
money_total = money_total + (2 * bet_amount);
}
}
//display header for record
void game::display_header()
{
cout << " Player Bank"<<"\n";
cout << "___________________________";
cout << "\n";
}
//display name and money amount
void game::display_record()
{
cout << player_name << "\t"<<"\t"<< money_total;
cout <<"\n";
}
int main()
{
//code to color text to blue
HANDLE hConsole;
int k = 11; // pick the colorattribute k you want
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, k);
game player;
player.blackjack();
ofstream tofile;
tofile.open("Blackjack.txt", ios::app); //write to record
if(tofile.fail())
{
cout << "Record failed to open";
exit(1);
}
if (player.money_total > 0)
{
tofile << player.player_name <<"\n"<< player.money_total<<"\n";
cout<<"\n";
}
tofile.close();
ifstream infile; //read from record
infile.open("Blackjack.txt");
player.game::display_header();
while(!infile.eof())
{
infile >> player.player_name;
infile >> player.money_total;
if(infile.eof() == 0 ) //to remove duplicacy
{
player.game::display_record();
}
}
infile.close();
return 0;
}