C Blackjack Program

C Blackjack Program

Implementing blackjack in C++ can be a big task. Since the game involves players, a deck of cards and the house, we have to break down the code into smaller parts. This is where we use the power of C++’s object orientated abilities to implement classes. Firstly we need get a visual diagram of how we are sorting the code and an overview of the game. Without this, we would get confused and lose track of what’s going where.

Now Lets have a look at how exactly we are implementing blackjack in C++ with the help of organising the code into header and implementation files. Remember that with C++, we can share data from other header files by importing in the file.

Card header file

  1. 888casino is a multiple award-winning online casino that first opened its doors back in 1997. Today it boasts over 1000 top games enjoyed by over 17 million players worldwide. 888casino is licensed and regulated by the UK Gambling Commission and the Governments of Gibraltar and Malta. 888 Holdings plc has been listed on the London Stock Exchange since 2005. 88casino offers a wide.
  2. C Blackjack program. Part I: Here is one way to draw a random card from a deck. Create a string called “suit” that contains the characters CDHS, which stand for Clubs, Diamonds, Hearts, and Spades.

C Blackjack Example I created a simple blackjack example using C that allows the player to play against a CPU opponent and whoever gets closest to 21 without going over wins. This is a simple blackjack example because players only have the option to hit or stay each turn. You need to add the preceding space in the scanf statement, like so: '%c'. This will consume the newline character for you. One clue as to solving this is actually already in your code! The two getchar calls at the end are serving this very purpose: once the program finishes, wait for the user to hit Enter. However, you required two getchars.

Card implementation file

Program

Deck header file

Deck implementation file

Game header file

Project Blackjack

Game implementation file

Generic player header file

Generic Player Class Implementation file

Hand header file

Hand implementation file

House header file

Blackjack Program Darpa

House implementation file

Player header

C++ Craps Program

Player implementation file

Putting it all together in the “main.cpp” file

Projects‎ > ‎

C++ Blackjack Example

I created a simple blackjack example using C++ that allows the player to play against a CPU opponent and whoever gets closest to 21 without going over wins. This is a simple blackjack example because players only have the option to hit or stay each turn.

//Specification: This program plays a version of

//the card game of 21.

//A human player is pitted against the computer.

//The player who is the closest to 21 without

//going over wins the hand.

#include

<iostream>

#include

<ctime>

#include

<string>

using

namespace std;

//prototypes...

void

play21(void);

int

dealCards(int, string);

void

hit(int &);

void

determineWinner(int, int);

int

Random(int, int);
void
main(){

char keepPlaying = 'n'; //loop control variable

do {

play21();

//keep playing?

cout <<

'Do you want to play another hand (y/n)?';

cin >> keepPlaying;

}

while(keepPlaying 'Y' keepPlaying 'y');

}

void

play21(void){//play one hand of 21//randomize the cards

srand((

int) time(0));// deal the cardsint person = dealCards(2, 'Your Cards:');

cout <<

' = ' << person << endl;int house = dealCards(2, 'Computers Cards:');

cout <<

' = ' << house << endl;// Ask if human wants a hit

hit(person);

cout << endl;

//Determine if computer takes a hitwhile ((house < person) && (house <= 21) && (person <= 21)) {

house += dealCards(1,

'The Computer takes a card ');

cout << endl;

}

//show who won....

determineWinner(person, house);

}

void

determineWinner(int humanScore, int houseScore) {//compare the scores to see who won//Both the human and the house score totals are provided as arguments//Display total scores and indicate winner//possible outcomes: human wins, computer wins, tie//display the scores

cout <<

'Your Score: ' << humanScore << endl;

cout <<

'Computer Score: ' << houseScore << endl;//decide who should winif (humanScore houseScore)

cout <<

'Tie';if ((humanScore 21 humanScore >= houseScore houseScore > 21) && (humanScore <= 21))

cout <<

'nYou Won!n';else

cout <<

'nThe Computer won!n';

}

int

dealCards(int numberOfCards, string message){//deals cards//The number of cards to be delt is provided as an argument//A message indicating which player is recieving the cards is also//given as an argument //The player message and the cards delt is displayed to the screen//the total value of the cards delt is returnedint cardDealt = 0;int totalValue = 0;

cout << message <<

' ';//deal the number of required cardsfor (int i = 1 ; i <= numberOfCards ; i++){//get a card

cardDealt = Random(1, 10);

//accumulate the card values

totalValue += cardDealt;

cout << cardDealt <<

' ';

}

return totalValue;

}

void

hit(int &playerScore){//Ask the human if they want another card -- 'a hit'//the player's score total is accumulated as they take cards//the player can contiue taking cards until they wish to stop or they exceed 21//After a card is taken the user's current total is displayed//If the user goes over 21 'busted' is displayed//Keep giving the player cards until he wants to stop or goes over 21char takeHit = 'Y';while (takeHit != 'n') {if (playerScore < 21) {//does the player want a hit?

cout <<

'do you want a hit (Y/N)? ';

cin >> takeHit;

if (takeHit 'Y' takeHit 'y'){//the player wants a hit so deal a card

playerScore += dealCards(1,

'Hit: ');

cout <<

'Your total is ' << playerScore << endl;

}

else//the player does not want another card

takeHit =

'n';

}

else {//the player has bustedif (playerScore > 21)

cout <<

'You Busted..nn';

takeHit =

'n';

}

}

}

int

Random(int lowerLimit, int upperLimit) {

//returns a random number within the given boundary

return 1 + rand() % (upperLimit - lowerLimit + 1);

}