Flock of Cats Rotating Header Image

BlackJack

I made a blackjack game in C++. You can click the link to download the game (probably only works on windows). You can see the source code after the jump.

#include <iostream>		//for basic input/output
#include <string>		//for strings
#include <cstdlib>		//for...I forgot...the random number?
#include <ctime>		//for seeding random number with clock
#include <windows.h>	//for colors
#include "colors.h"		//include color definitions

using namespace std;

#define HEARTS "H"		//define suits
#define DIAMONDS "D"
#define CLUBS "C"
#define SPADES "S"

void clear_screen ( void )	//Function to clear the screen
{
  DWORD n;                         /* Number of characters written */
  DWORD size;                      /* number of visible characters */
  COORD coord = {0};               /* Top left screen position */
  CONSOLE_SCREEN_BUFFER_INFO csbi;

  /* Get a handle to the console */
  HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );

  GetConsoleScreenBufferInfo ( h, &csbi );

  /* Find the number of characters to overwrite */
  size = csbi.dwSize.X * csbi.dwSize.Y;

  /* Overwrite the screen buffer with whitespace */
  FillConsoleOutputCharacter ( h, TEXT ( ' ' ), size, coord, &n );
  GetConsoleScreenBufferInfo ( h, &csbi );
  FillConsoleOutputAttribute ( h, csbi.wAttributes, size, coord, &n );

  /* Reset the cursor to the top left position */
  SetConsoleCursorPosition ( h, coord );
}

struct card {  //info for one card
string face;
string suit;
int value;

};

void make_deck(card deck[]) { //fill the deck with cards

card * deck_p;

deck_p=deck;
int counter=0;
for (int i=0; i<4; i++){
	for (int j=0; j<13; j++){

		if (j==0) { deck_p->value = 11; deck_p->face = "A";}
		if ((j>0) && (j<9)) {deck_p->value = j+1; deck_p->face = j+49;}
		if (j==9) { deck_p->value = 10; deck_p->face = "10";}
		if (j==10) { deck_p->value = 10; deck_p->face = "J";}
		if (j==11) { deck_p->value = 10; deck_p->face = "Q";}
		if (j==12) { deck_p->value = 10; deck_p->face = "K";}

		if (i==0) {deck_p->suit = HEARTS;}
		if (i==1) {deck_p->suit = DIAMONDS;}
		if (i==2) {deck_p->suit = CLUBS;}
		if (i==3) {deck_p->suit = SPADES;}
		deck_p++;
			}
		}
}

void shuffle_deck(card deck[]) { // exchange two cards (SHUFFLE number of times)

	card temp;
	const int SHUFFLE = 200;	//define number of shuffles
    int random1=0;
	int random2=0;
    int lowest=0, highest=51;
    int range=(highest-lowest)+1;

	for (int n=0; n< SHUFFLE; n++) {
		random1 = lowest+int(range*rand()/(RAND_MAX + 1.0));
		random2 = lowest+int(range*rand()/(RAND_MAX + 1.0));
	temp = deck[random1];
	deck[random1]=deck[random2];
	deck[random2]=temp;
	}
	cout <<endl << "New deck!! Shuffling!" <<endl<<endl;
	Sleep(1000);
}

class hand_c { //define class for manipulating player and dealer hands

	card hand[20];	//I tried to make this a dynamic array but gave up and just defined a constant array of 20, which is more than enough for a Blackjack hand
	int ace_count;	//For counting number of aces to adjust value to 1 or 11
	int hand_value;	//Total value of hand

public:
	int hand_count;	//Number of cards in hand
	bool blackjack;	//True if hand is Blackjack

int calc_hand_value () {  //function for calculating value of hand

	hand_value=0;
	ace_count=0;

	for (int j=0; j<hand_count; j++)
		hand_value+=hand[j].value;

	for (int k=0; k<hand_count; k++) {
		if (hand[k].face=="A")
			ace_count++; }
	for (int m=1; m<=ace_count; m++)
	{if (hand_value > 21) hand_value-=10;} //subtract 10 for each ace if hand value is over 21, thus taking ace as 1

	return (hand_value);

}

int hidden_hand_value () {	//calculate the value excluding first card

	hand_value=hand[1].value;

	return (hand_value);

}

void get_deal(card deck[], int &dealt_cards) {	//perform initial deal of two cards to player and dealer

	hand_count=0;
	hand_value=0;
	blackjack=false;

	if (dealt_cards == 52) {		//check that there are enough cards, if not, shuffle new deck
			make_deck(deck);
			shuffle_deck(deck);
			dealt_cards=0;
			cout <<endl << "...New deck!! Re-shuffling!" <<endl<< endl;
			Sleep(1000);}

	hand[0]=deck[dealt_cards];	//deal first card
	dealt_cards++;
	hand_count++;

	if (dealt_cards == 52) {
			make_deck(deck);
			shuffle_deck(deck);
			dealt_cards=0;
			cout <<endl << "...New deck!! Re-shuffling!" <<endl<<endl;
			Sleep(1000);}

	hand[1]=deck[dealt_cards];	//deal second card
	dealt_cards++;
	hand_count++;

	for (int j=0; j<hand_count; j++)
		hand_value+=hand[j].value;

	if (hand_value==21)		//check for blackjack
		blackjack=true;
	}

void print_hand() {

	 HANDLE hOut;								//shit for color
	 hOut = GetStdHandle(STD_OUTPUT_HANDLE);

for (int k=0; k<hand_count; k++)
{
	if ((hand[k].suit==HEARTS)||(hand[k].suit==DIAMONDS))  //print red face of card
	{
		cout <<"  ";
		SetConsoleTextAttribute (hOut, BWI | FRI);
		cout.width(2);
		cout<< hand[k].face <<"      "<<flush;
		SetConsoleTextAttribute(hOut, BNULL | FW);
		cout<< "  "<<flush;}
	else						//print black face of cards
	{
		cout <<"  ";
		SetConsoleTextAttribute (hOut, BWI | FNULL);
		cout.width(2);
		cout<< hand[k].face <<"      "<<flush;
		SetConsoleTextAttribute(hOut, BNULL | FW);
		cout<< "  "<<flush;}
	}

	cout << endl;
	for (int k=0; k<hand_count; k++)  //print white space
		{SetConsoleTextAttribute(hOut, BNULL | FW); cout << "  " << flush; SetConsoleTextAttribute (hOut, BWI | FRI); cout <<"        " << flush; SetConsoleTextAttribute(hOut, BNULL | FW); cout << "  " << flush;}
	cout << endl;
	for (int k=0; k<hand_count; k++)	//print white space
		{SetConsoleTextAttribute(hOut, BNULL | FW); cout << "  " << flush; SetConsoleTextAttribute (hOut, BWI | FRI); cout <<"        " << flush; SetConsoleTextAttribute(hOut, BNULL | FW); cout << "  " << flush;}
	cout << endl;

	for (int k=0; k<hand_count; k++) {
		if ((hand[k].suit==HEARTS)||(hand[k].suit==DIAMONDS)) { //print red suits
			cout <<"  ";
			SetConsoleTextAttribute (hOut, BWI | FRI);
			cout.width(7);
			cout<< hand[k].suit << " "<< flush;
			SetConsoleTextAttribute(hOut, BNULL | FW);
			cout<< "  "<<flush;}
		else {									//print black suits
			cout <<"  ";
			SetConsoleTextAttribute (hOut, BWI | FNULL);
			cout.width(7);
			cout<< hand[k].suit << " " << flush;
			SetConsoleTextAttribute(hOut, BNULL | FW);
			cout<< "  "<<flush;}
		}
		cout <<endl;

	}

void print_hidden_hand() {		//as above, but hide first card, print it blue

	 HANDLE hOut;
	 hOut = GetStdHandle(STD_OUTPUT_HANDLE);

for (int k=0; k<hand_count; k++)
{

	if (k==0)
	{
		cout <<"  ";
		SetConsoleTextAttribute (hOut, BB | FNULL);
		cout<< "        "<<flush;
		SetConsoleTextAttribute(hOut, BNULL | FW);
		cout<< "  "<<flush;}
	else if ((hand[k].suit==HEARTS)||(hand[k].suit==DIAMONDS))
	{
		cout <<"  ";
		SetConsoleTextAttribute (hOut, BWI | FRI);
		cout.width(2);
		cout<< hand[k].face <<"      "<<flush;
		SetConsoleTextAttribute(hOut, BNULL | FW);
		cout<< "  "<<flush;}
	else
	{
		cout <<"  ";
		SetConsoleTextAttribute (hOut, BWI | FNULL);
		cout.width(2);
		cout<< hand[k].face <<"      "<<flush;
		SetConsoleTextAttribute(hOut, BNULL | FW);
		cout<< "  "<<flush;}
	}

	cout << endl;

	for (int k=0; k<hand_count; k++){
		if (k==0)
			{SetConsoleTextAttribute(hOut, BNULL | FW); cout << "  " << flush; SetConsoleTextAttribute (hOut, BB | FB); cout <<"        " << flush; SetConsoleTextAttribute(hOut, BNULL | FW); cout << "  " << flush;}
		else
			{SetConsoleTextAttribute(hOut, BNULL | FW); cout << "  " << flush; SetConsoleTextAttribute (hOut, BWI | FRI); cout <<"        " << flush; SetConsoleTextAttribute(hOut, BNULL | FW); cout << "  " << flush;}
	}
	cout << endl;

	for (int k=0; k<hand_count; k++){
		if (k==0)
			{SetConsoleTextAttribute(hOut, BNULL | FW); cout << "  " << flush; SetConsoleTextAttribute (hOut, BB | FB); cout <<"        " << flush; SetConsoleTextAttribute(hOut, BNULL | FW); cout << "  " << flush;}
		else
			{SetConsoleTextAttribute(hOut, BNULL | FW); cout << "  " << flush; SetConsoleTextAttribute (hOut, BWI | FRI); cout <<"        " << flush; SetConsoleTextAttribute(hOut, BNULL | FW); cout << "  " << flush;}
	}
	cout << endl;

	for (int k=0; k<hand_count; k++){
		if (k==0){
			cout <<"  ";
			SetConsoleTextAttribute (hOut, BB | FB);
			cout << "        "<< flush;
			SetConsoleTextAttribute(hOut, BNULL | FW);
			cout<< "  "<<flush;}
		else if ((hand[k].suit==HEARTS)||(hand[k].suit==DIAMONDS)){
			cout <<"  ";
			SetConsoleTextAttribute (hOut, BWI | FRI);
			cout.width(7);
			cout<< hand[k].suit << " "<< flush;
			SetConsoleTextAttribute(hOut, BNULL | FW);
			cout<< "  "<<flush;}
		else {
			cout <<"  ";
			SetConsoleTextAttribute (hOut, BWI | FNULL);
			cout.width(7);
			cout<< hand[k].suit << " " << flush;
			SetConsoleTextAttribute(hOut, BNULL | FW);
			cout<< "  "<<flush;}
		}

	cout <<endl;

}

void hit (card deck[], int &dealt_cards) {	//add a card for hit

	if (dealt_cards == 52) {	//check there are enough cards to deal, else shuffle new deck
		make_deck(deck);
		shuffle_deck(deck);
		dealt_cards=0;
		cout <<endl << "...New deck!! Re-shuffling!" <<endl<<endl;
		Sleep(1000);}

	hand[hand_count]=deck[dealt_cards];
	dealt_cards++;
	hand_count++;

}

}player_hand, dealer_hand;  //objects(?) for player and dealer hands

void make_bet ( double &bet,  double &cash) {	//function to get bet

	HANDLE hOut;
	hOut = GetStdHandle(STD_OUTPUT_HANDLE);

	if (cash!=0) {
		cout << "You have ";
			SetConsoleTextAttribute(hOut, BNULL | FGI); cout <<"$" << cash << flush;  SetConsoleTextAttribute(hOut, BNULL | FW);
			cout <<". Enter your wager (enter 0 to quit) --> ";
			SetConsoleTextAttribute(hOut, BNULL | FGI);
			cin >> bet; SetConsoleTextAttribute(hOut, BNULL | FW);}
	else if (cash ==0)	//if no money, set bet as zero to quit
		bet = 0;
	while ( (!cin.good()) || (bet > cash)||(bet < 0))	//check for valid input
	if (!cin.good()) {	//numbers only
		cout << endl << "Enter a valid wager. Only money, no abc#$%^& shit, please!" <<endl;
		cout << "You have ";
			SetConsoleTextAttribute(hOut, BNULL | FGI); cout <<"$" << cash << flush;  SetConsoleTextAttribute(hOut, BNULL | FW);
			cout <<". Enter your wager (enter 0 to quit) --> ";
			SetConsoleTextAttribute(hOut, BNULL | FGI); SetConsoleTextAttribute(hOut, BNULL | FW);;
			cin.clear(); cin.sync(); cin >> bet;
			SetConsoleTextAttribute(hOut, BNULL | FW);}

	else if (bet > cash) {	//check that bet is less than available cash
		cout << endl << "Sorry, champ. You don't have enough money." <<endl;
		cout << "You have ";
			SetConsoleTextAttribute(hOut, BNULL | FGI); cout <<"$" << cash << flush;  SetConsoleTextAttribute(hOut, BNULL | FW);
			cout <<". Enter your wager (enter 0 to quit) --> ";
			SetConsoleTextAttribute(hOut, BNULL | FGI);
			cin.clear(); cin.sync();cin >> bet;
			SetConsoleTextAttribute(hOut, BNULL | FW);}

	else if (bet < 0) {	//check for negative bets
		cout << endl << "No negative bets, Mr. Smartypants." <<endl;
		cout << "You have ";
			SetConsoleTextAttribute(hOut, BNULL | FGI); cout <<"$" << cash << flush;  SetConsoleTextAttribute(hOut, BNULL | FW);
			cout <<". Enter your wager (enter 0 to quit) --> ";
			SetConsoleTextAttribute(hOut, BNULL | FGI);
			cin.clear(); cin.sync(); cin >> bet;
			SetConsoleTextAttribute(hOut, BNULL | FW); }

	cash=cash-bet;	//deduct bet from cash

}

void win_lose ( double bet,  double &cash, bool bought_ins) {	//check for winner

	HANDLE hOut;
	hOut = GetStdHandle(STD_OUTPUT_HANDLE);

	if ((dealer_hand.blackjack==true)&&(bought_ins==true)){ //if insurance is bought and dealer Blackjacks, payout
		cout << "You won ";
		SetConsoleTextAttribute(hOut, BNULL | FGI);
		cout << "$" << bet <<  flush;
		SetConsoleTextAttribute(hOut, BNULL | FW);
		cout << " from insurance!" << endl <<endl;
	}

	if ((dealer_hand.blackjack==false)&&(bought_ins==true)){ //lose insurance if not dealer blackjack
		cout << "You lost your insurance of ";
		SetConsoleTextAttribute(hOut, BNULL | FRI);
		cout << "$" << bet/2 <<  flush;
		SetConsoleTextAttribute(hOut, BNULL | FW);
		cout << "." << endl <<endl;
	}

	if (dealer_hand.blackjack==true) {	//player loses if dealer gets BJ
		SetConsoleTextAttribute (hOut, BNULL | FRI);
		cout << "Dealer got blackjack! " <<flush;
		SetConsoleTextAttribute(hOut, BNULL | FW);
		cout << " You lost your bet of "<<flush;
		SetConsoleTextAttribute(hOut, BNULL | FRI);
		cout<< "-- $" << bet <<" --"<< flush;
		SetConsoleTextAttribute(hOut, BNULL | FW);
		cout << endl <<endl;
		}

	else if (player_hand.blackjack==true) {		//player wins if BJ
		SetConsoleTextAttribute (hOut, BNULL | FGI);
		cout << "You got blackjack! " <<flush;
		SetConsoleTextAttribute(hOut, BNULL | FW);
		cout << " You get back your bet of "<<flush;
		SetConsoleTextAttribute(hOut, BNULL | FYI);
		cout<< "$" << bet<< flush;
		SetConsoleTextAttribute(hOut, BNULL | FGI);
		cout<< " + $" << bet*1.5 <<  flush;
		SetConsoleTextAttribute(hOut, BNULL | FW);
		cout << " in winnings!" << endl <<endl;
		cash = cash + bet*2.5;
		}

	else if (player_hand.calc_hand_value() > 21) {	//check for player over 21, if bust, player always loses
		SetConsoleTextAttribute (hOut, BNULL | FRI);
		cout << "Bust! " <<flush;
		SetConsoleTextAttribute(hOut, BNULL | FW);
		cout << " You lost your bet of "<<flush;
		SetConsoleTextAttribute(hOut, BNULL | FRI);
		cout<< "-- $" << bet <<" --"<< flush;
		SetConsoleTextAttribute(hOut, BNULL | FW);
		cout << endl <<endl;
		}
	else if ((dealer_hand.calc_hand_value() > 21) && (player_hand.calc_hand_value() <= 21)) {	//if dealer, but not player busts, player wins
		SetConsoleTextAttribute (hOut, BNULL | FGI);
		cout << "Dealer busts. You win!" <<flush;
		SetConsoleTextAttribute(hOut, BNULL | FW);
		cout << " You get back your bet of "<<flush;
		SetConsoleTextAttribute(hOut, BNULL | FYI);
		cout<< "$" << bet<< flush;
		SetConsoleTextAttribute(hOut, BNULL | FGI);
		cout<< " + $" << bet<<  flush;
		SetConsoleTextAttribute(hOut, BNULL | FW);
		cout << " in winnings!" << endl <<endl;
		cash+=bet*2;
	}
	else if ((dealer_hand.calc_hand_value() <= 21) && (player_hand.calc_hand_value() <= 21) && (player_hand.calc_hand_value() > dealer_hand.calc_hand_value())) {
		SetConsoleTextAttribute (hOut, BNULL | FGI);  //no one busts, check if player is higher
		cout << "You beat the dealer!"  <<flush;
		SetConsoleTextAttribute (hOut, BNULL | FW);
		cout << " You get back your bet of ";
		SetConsoleTextAttribute(hOut, BNULL | FYI);
		cout<<  "$" << bet<<  flush;
		SetConsoleTextAttribute(hOut, BNULL | FGI);
		cout<< " + $" << bet<<  flush;
		SetConsoleTextAttribute(hOut, BNULL | FW);
		cout << " in winnings!" << endl <<endl;
		cash+=bet*2;
	}
	else if ((dealer_hand.calc_hand_value() <= 21) && (player_hand.calc_hand_value() <= 21) && (player_hand.calc_hand_value() < dealer_hand.calc_hand_value())) {
		SetConsoleTextAttribute (hOut, BNULL | FRI); //no one busts, check if dealer is higher
		cout << "Dealer wins!" <<flush;
		SetConsoleTextAttribute(hOut, BNULL | FW);
		cout << " You lost your bet of ";
		SetConsoleTextAttribute(hOut, BNULL | FRI);
		cout<< "-- $" << bet<< " -- " << flush;
		SetConsoleTextAttribute(hOut, BNULL | FW);
		cout << endl <<endl;
			}
	else if ((dealer_hand.calc_hand_value() <= 21) && (player_hand.calc_hand_value() <= 21) && (player_hand.calc_hand_value() == dealer_hand.calc_hand_value())) {
		SetConsoleTextAttribute (hOut, BNULL | FWI); cout << "Push."  <<flush; SetConsoleTextAttribute(hOut, BNULL | FW);
		cout << " You get back your bet of ";			//check for non-bust tie
		SetConsoleTextAttribute(hOut, BNULL | FYI);
		cout<< "$" << bet<< flush;
		SetConsoleTextAttribute(hOut, BNULL | FW);
		cout << endl <<endl;
		cash+=bet;
	}

}

void bet_status(double bet, double cash) {		//display bet and cash info
	HANDLE hOut;
	hOut = GetStdHandle(STD_OUTPUT_HANDLE);

		cout << "You have ";
			SetConsoleTextAttribute(hOut, BNULL | FGI); cout <<"$" << cash << flush;  SetConsoleTextAttribute(hOut, BNULL | FW);
			cout <<". Your current bet is ";
			SetConsoleTextAttribute(hOut, BNULL | FMI);
			cout << "$" << bet <<"."<<endl<<endl;
			SetConsoleTextAttribute(hOut, BNULL | FW);
}

void insurance(double bet, double &cash, bool &bought_ins) {	//insurance function

	HANDLE hOut;
	hOut = GetStdHandle(STD_OUTPUT_HANDLE);

	bought_ins=false;
	char input='?';
	if ((dealer_hand.hidden_hand_value()==11) && (cash-bet/2 >0.01))  //if ace is showing and player has enough money, offer insurance
	{
		SetConsoleTextAttribute(hOut, BNULL | FMI);
		cout << "Would you like to buy insurance? y/n --> "<<flush;
		SetConsoleTextAttribute(hOut, BNULL | FW);
		cin>>input;
		while ( (!cin.good()) || ( (input!='y') && (input!='Y') && (input!='N') && (input!='n') )) { //check input
			SetConsoleTextAttribute(hOut, BNULL | FMI);
			cout << endl << "Do you want insurance or not? y/n --> "<<flush;
			SetConsoleTextAttribute(hOut, BNULL | FW);
			cin.clear(); cin.sync(); cin >> input;
		}

		if ( ((input=='Y')||(input=='y')) && (dealer_hand.calc_hand_value()==21))	//check if dealer has BJ and pay out if insurance bought
		{cash+=bet; bought_ins=true; }
		if ( ((input=='Y')||(input=='y')) && (dealer_hand.calc_hand_value()!=21))	//if not dealer BJ, lose insurance if bought
		{cash-=bet/2;bought_ins=true; }

	clear_screen();
	bet_status(bet, cash);
	dealer_hand.print_hidden_hand();
	cout << endl<< "The dealer has ";
	SetConsoleTextAttribute(hOut, BNULL | FYI);
	cout << dealer_hand.hidden_hand_value() << flush;
	SetConsoleTextAttribute(hOut, BNULL | FW);
	cout << " showing."<< endl<<endl;

	player_hand.print_hand();
	cout << endl<< "You have ";
	SetConsoleTextAttribute(hOut, BNULL | FCI);
	cout << player_hand.calc_hand_value() << flush;
	SetConsoleTextAttribute(hOut, BNULL | FW);
	cout << "."<< endl<<endl;

		if ( ((input=='Y')||(input=='y')) && (dealer_hand.calc_hand_value()==21))
		{cout << "Your insurance paid off!"<<endl;}
		if ( ((input=='Y')||(input=='y')) && (dealer_hand.calc_hand_value()!=21))
		{cout << "You lost your insurance!"<<endl;}

	}
}

void print_intro (){		//intro and explain rules
	HANDLE hOut;
	hOut = GetStdHandle(STD_OUTPUT_HANDLE);

	cout << "Welcome to ";
	SetConsoleTextAttribute(hOut, BW | FNULL);
	cout <<"Black" << flush;
	SetConsoleTextAttribute(hOut, BW | FRI);
	cout <<"Jack" << flush;
	SetConsoleTextAttribute(hOut, BNULL | FW);
	cout << "!" << endl << endl;
	cout << "At the beginning of each turn, enter your bet or press zero (0) to quit." <<endl<<endl;
	cout << "Press ";
	SetConsoleTextAttribute(hOut, BNULL | FCI);
	cout << "H"<<flush;
	SetConsoleTextAttribute(hOut, BNULL | FW);
	cout << " to hit, " << flush;
	SetConsoleTextAttribute(hOut, BNULL | FCI);
	cout << "S"<<flush;
	SetConsoleTextAttribute(hOut, BNULL | FW);
	cout << " to stay, or "<<flush;
	SetConsoleTextAttribute(hOut, BNULL | FCI);
	cout << "D"<<flush;
	SetConsoleTextAttribute(hOut, BNULL | FW);
	cout << " to double down."<<endl<<endl;
	cout << "Sorry, I haven't included the option to split." <<endl<<endl;
	cout << "The deck consists of 52 shuffled cards." <<endl <<"When the deck runs out, a new deck is shuffled and used."<<endl<<endl;
	cout << "The dealer stays on " << flush;
	SetConsoleTextAttribute(hOut, BNULL | FBI);
	cout << "17 and above "<< flush;
	SetConsoleTextAttribute(hOut, BNULL | FW);
	cout << "(including soft 17)." <<endl << endl;
	SetConsoleTextAttribute(hOut, BNULL | FMI);
	cout << "Press ENTER to start" <<endl;
	SetConsoleTextAttribute(hOut, BNULL | FW);

	string trash;
	getline(cin, trash);
}

int main () {

	HANDLE hOut;
	hOut = GetStdHandle(STD_OUTPUT_HANDLE);

	bool bought_ins=false;	//bool for whether insurance is bought
	card deck[52];			//deck of 52 cards
	 double bet=0;		//player bet
	 double cash=1000;	//player cash, starts at 1000
	int dealt_cards=0;	//number of cards dealt, for checking when to shuffle new deck
	char input;			//input for player moves

	srand((unsigned)time(0));	//seed pseudo random generator

	print_intro();

	clear_screen();
	make_deck(deck);
	shuffle_deck(deck);

	make_bet(bet, cash);

	while (bet != 0) {		//bet 0 to quit, otherwise play

	dealer_hand.get_deal(deck, dealt_cards);
	player_hand.get_deal(deck, dealt_cards);

	clear_screen();
	bet_status(bet, cash);
	dealer_hand.print_hidden_hand();
	cout << endl<< "The dealer has ";
	SetConsoleTextAttribute(hOut, BNULL | FYI);
	cout << dealer_hand.hidden_hand_value() << flush;
	SetConsoleTextAttribute(hOut, BNULL | FW);
	cout << " showing."<< endl<<endl;

	player_hand.print_hand();
	cout << endl<< "You have ";
	SetConsoleTextAttribute(hOut, BNULL | FCI);
	cout << player_hand.calc_hand_value() << flush;
	SetConsoleTextAttribute(hOut, BNULL | FW);
	cout << "."<< endl<<endl;

	insurance(bet, cash, bought_ins);	//check for and offer insurance as necessary

	do {

	if (dealer_hand.blackjack==true)	//if someone has blackjack, input is "Stay" since no moves are needed
		input='S';
	else if (player_hand.blackjack==true)
		input='S';
	else {

	if ((bet <=cash)&& !(player_hand.hand_count>2))		//only show double down if player has enough money
	cout << "Hit (H), Double Down (D), or Stay (S) --> ";
	else if ((bet >cash) || (player_hand.hand_count>2))
		cout << "Hit (H) or Stay (S) --> ";

	cin >> input;
	cout <<endl;

	if ( ((input == 'd') || (input == 'D')) && (bet>cash)){		//check for cash to double down
			cout << "You don't have enough cash to double down. Hit (H) or Stay (S) --> "; cin.clear(); cin.sync(); cin >> input;
				while ( (!cin.good()) || ( !(  (input == 's') || (input == 'S') || (input == 'h') || (input == 'H'))) || ((input == 'd') || (input == 'D')))	{	//check input
					if ((input == 'd') || (input == 'D'))
						{cout << "Dude, I told you...you don't have enough money.  Hit (H) or Stay (S) --> ";cin.clear(); cin.sync(); cin >> input;}
					else
					{cout << "Jeez, first you try to double down with no money..."<<endl;
					cout << "Now you can't even input properly. Hit (H) or Stay (S) --> "; cin.clear(); cin.sync(); cin >> input;}}
	}
	else if ( ((input == 'd') || (input == 'D')) && (player_hand.hand_count>2)){  //only allow double down on first move
			cout << "You've already hit, so you can't double down now. Hit (H) or Stay (S) --> "; cin.clear(); cin.sync(); cin >> input;
				while ( (!cin.good()) || ( !(  (input == 's') || (input == 'S') || (input == 'h') || (input == 'H'))) || ((input == 'd') || (input == 'D')))	{
					if ((input == 'd') || (input == 'D'))	//check input
						{cout << "The moment has passed. Let it go.  Hit (H) or Stay (S) --> ";cin.clear(); cin.sync(); cin >> input;}
					else
					{cout << "Not only are you late doubling down..."<<endl;
					cout << "But now you can't even input properly. Hit (H) or Stay (S) --> "; cin.clear(); cin.sync(); cin >> input;}}
	}
	else {
	while ( (!cin.good()) || ( !(  (input == 's') || (input == 'S') || (input == 'h') || (input == 'H') || (input == 'd') || (input == 'D')))) //check input
	{
		if (bet <=cash)
		{cout << "Try again. Hit (H), Double Down (D), or Stay (S) --> ";cin.clear(); cin.sync(); cin >> input;}
		else if (bet >cash)
		{cout << "Try again. Hit (H) or Stay (S) --> "; cin.clear(); cin.sync(); cin >> input;}}
	}}

	if ((input=='h')||(input=='H'))		//hit
	{player_hand.hit(deck, dealt_cards); cin.clear(); cin.sync();}

	if (((input=='D')||(input=='d')) && (bet<=cash)) //double down
	{player_hand.hit(deck, dealt_cards);
	cash-=bet;
	bet*=2;
	cin.clear(); cin.sync(); input='S';} //set to "stay" after double down to prevent other moves

	clear_screen();
	bet_status(bet, cash);

	dealer_hand.print_hidden_hand();		//I should have put all this display stuff in a function...it keeps repeating!
	cout << endl<< "The dealer has ";
	SetConsoleTextAttribute(hOut, BNULL | FYI);
	cout << dealer_hand.hidden_hand_value() << flush;
	SetConsoleTextAttribute(hOut, BNULL | FW);
	cout << " showing ."<< endl<<endl;

	player_hand.print_hand();
	cout << endl<< "You have ";
	SetConsoleTextAttribute(hOut, BNULL | FCI);
	cout << player_hand.calc_hand_value() << flush;
	SetConsoleTextAttribute(hOut, BNULL | FW);
	cout << "."<< endl<<endl;

	} while ((player_hand.calc_hand_value() <21) && ((input!='s') && (input != 'S')));  //let player hit until "stay"

	while (dealer_hand.calc_hand_value() <17) {		//dealer hits on less than 17

		clear_screen();
		SetConsoleTextAttribute(hOut, BWI | FBI);
		cout<<"Dealer's turn..."<<flush;
		SetConsoleTextAttribute(hOut, BNULL | FW);
		cout <<endl <<endl;
		dealer_hand.print_hand();
		cout << endl<< "The dealer has ";
		SetConsoleTextAttribute(hOut, BNULL | FYI);
		cout << dealer_hand.calc_hand_value() << flush;
		SetConsoleTextAttribute(hOut, BNULL | FW);
		cout << "."<< endl<<endl;

		player_hand.print_hand();
		cout << endl<< "You have ";
		SetConsoleTextAttribute(hOut, BNULL | FCI);
		cout << player_hand.calc_hand_value() << flush;
		SetConsoleTextAttribute(hOut, BNULL | FW);
		cout << "."<< endl<<endl;
		Sleep(800);				//pause so dealer cards don't fly out all at once
		dealer_hand.hit(deck, dealt_cards);

		}
	clear_screen();
	cout <<endl <<endl;
	dealer_hand.print_hand();
	cout << endl<< "The dealer has ";
	SetConsoleTextAttribute(hOut, BNULL | FYI);
	cout << dealer_hand.calc_hand_value() << flush;
	SetConsoleTextAttribute(hOut, BNULL | FW);
	cout << "."<< endl<<endl;
	player_hand.print_hand();
	cout << endl<< "You have ";
	SetConsoleTextAttribute(hOut, BNULL | FCI);
	cout << player_hand.calc_hand_value() << flush;
	SetConsoleTextAttribute(hOut, BNULL | FW);
	cout << "."<< endl<<endl;

	win_lose(bet, cash, bought_ins);
	make_bet(bet, cash);
	}

	if (cash==0)
		cout << "Sorry that you lost all your money!!"<<endl<<endl;		//GAMEOVER

	cout << "Press <Enter> to close.\n";
	string trash2;
	cin.clear(); cin.sync();
	getline(cin, trash2);

return 0;
}

Here’s all the windows color junk:


#ifndef SHORTCOLOURS_H

#define SHORTCOLOURS_H</code>

<code> </code>

<code>#define FW FOREGROUND_RED | \

FOREGROUND_GREEN | \

FOREGROUND_BLUE

#define FR FOREGROUND_RED

#define FG FOREGROUND_GREEN

#define FB FOREGROUND_BLUE

#define FY FOREGROUND_RED | \

FOREGROUND_GREEN

#define FC FOREGROUND_GREEN | \

FOREGROUND_BLUE

#define FM FOREGROUND_BLUE | \

FOREGROUND_RED

#define FWI FOREGROUND_RED | \

FOREGROUND_GREEN | \

FOREGROUND_BLUE | \

FOREGROUND_INTENSITY

#define FRI FOREGROUND_RED | \

FOREGROUND_INTENSITY

#define FGI FOREGROUND_GREEN | \

FOREGROUND_INTENSITY

#define FBI FOREGROUND_BLUE | \

FOREGROUND_INTENSITY

#define FYI FOREGROUND_RED | \

FOREGROUND_GREEN | \

FOREGROUND_INTENSITY

#define FCI FOREGROUND_GREEN | \

FOREGROUND_BLUE | \

FOREGROUND_INTENSITY

#define FMI FOREGROUND_BLUE | \

FOREGROUND_RED | \

FOREGROUND_INTENSITY

#define FNULL 0</code>

<code>#define BW BACKGROUND_RED | \

BACKGROUND_GREEN | \

BACKGROUND_BLUE

#define BR BACKGROUND_RED

#define BG BACKGROUND_GREEN

#define BB BACKGROUND_BLUE

#define BY BACKGROUND_RED | \

BACKGROUND_GREEN

#define BC BACKGROUND_GREEN | \

BACKGROUND_BLUE

#define BM BACKGROUND_BLUE | \

BACKGROUND_RED

#define BWI BACKGROUND_RED | \

BACKGROUND_GREEN | \

BACKGROUND_BLUE | \

BACKGROUND_INTENSITY

#define BRI BACKGROUND_RED | \

BACKGROUND_INTENSITY

#define BGI BACKGROUND_GREEN | \

BACKGROUND_INTENSITY

#define BBI BACKGROUND_BLUE | \

BACKGROUND_INTENSITY

#define BYI BACKGROUND_RED | \

BACKGROUND_GREEN | \

BACKGROUND_INTENSITY

#define BCI BACKGROUND_GREEN | \

BACKGROUND_BLUE | \

BACKGROUND_INTENSITY

#define BMI BACKGROUND_BLUE | \

BACKGROUND_RED | \

BACKGROUND_INTENSITY

#define BNULL 0

#endif

Related posts

Leave a Reply

Better Tag Cloud