Give an explanation as to why everything in this code is there. /* Final Project
ID: 3714402 • Letter: G
Question
Give an explanation as to why everything in this code is there.
/*
Final Project: Black Jack
Programmers: Brittany Tomblin and Kyle Galante
CIT163
2/13/18
Description: This program will simulate a real game of Black Jack with a dealer vs human. This Blackjack game
resembles the casino version of Black Jack this game where betting takes place.
*/
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <ctime> // Allows for time to be used
#include <vector> // Allows for the usage of vectors
using namespace std;
// Function Prototypes for Black Jack Game
void menu();
void gameComputer(bool CardsDealt[], int HouseHand[], int PlayerHand[], int HouseCardCount, int PlayerCardCount, int x);
void shuffle(bool CardDelt[]);
void displayCard(int Card);
void displayHand(int Hand[], int CardCount);
int nextCard(bool CardsDealt[]);
int scoreHand(int Hand[], int CardCount);
//=============================
// main() =
// The section for BlackJack =
//=============================
int main() {
// For random numbers
srand(time(0));
// Declarations for variables
bool CardsDealt[52]; // Number of cards in the deck (0-51)
int HouseCardCount = 0; // Starting card count for the house
int HouseHand[12];
int PlayerCardCount = 0; // Starting card count for the player
int PlayerHand[12];
int x = 0;
// The menu for which game mode to play in
menu();
shuffle(CardsDealt);
gameComputer(CardsDealt, HouseHand, PlayerHand, HouseCardCount, PlayerCardCount, x);
// For windows to show final screen
system("pause");
return 0; // Return success
}
//=============================
// menu() =
// Selection to play with =
// computer or another user =
//=============================
void menu(){
cout << "**********************" << endl;
cout << "|Welcome to BlackJack|" << endl;
cout << "**********************" << endl;
cout << "Here are the rules: " << endl;
cout << " 1. The goal of the game is to assemble a hand whose numerical point value is as close to 21" << endl;
cout << " without going over. Whoever gets closes to 21 without going over wins. " << endl;
cout << " 2. Aces are worth either 1 point or 11 points at the players dicretion. Picture cards are worth" << endl;
cout << " 10 points. All other cards are worth a number of points equal to their numerical value. " << endl;
cout << " 3. The dealer must draw on 17 or less and hold on 18 or more. " << endl;
cout << "LET'S START THE GAME !" << endl;
}
//=============================
// gameComputer() =
//The Game With the computer =
//=============================
void gameComputer(bool CardsDealt[], int HouseHand[], int PlayerHand[], int HouseCardCount, int PlayerCardCount, int x)
{
//Declaring Variables
char endgame;
endgame = 'n';
int bets;
// Loops for hand (player)
while ((endgame != 'n' || endgame != 'N')) {
// Shuffles the cards
shuffle(CardsDealt); // Shuffles the deck of cards before the beginning of the game
// Two cards are dealt for each player and house
PlayerHand[0] = nextCard(CardsDealt); // first card
HouseHand[0] = nextCard(CardsDealt); // first card
PlayerHand[1] = nextCard(CardsDealt); // second card
HouseHand[1] = nextCard(CardsDealt); // second card
HouseCardCount = 2; // total number of cards
PlayerCardCount = 2; // total number of cards
//Declaring more Variables
char PlayerChoice = ''; // allows the player to choose what they want to do with their 2 cards
bool PlayerHits = true;
int PlayerScore = scoreHand(PlayerHand, PlayerCardCount);
// The start of a new hand
cout << "===========================================" << endl;
cout << "= Black Jack 21 =" << endl;
cout << "===========================================" << endl;
cout << "Money: $" << "1000" << endl; // This pulls the users money amount up
cout << "Place your bets!" << endl;
cout << "Bet: ";
cin >> bets;
while (bets > 1000) { // Validation if user enters more money than in account
cout << "You entered a bet more than you have in your account. "
<< "Please enter your bet: ";
cin >> bets;
}
// The start of a new hand
cout << "===========================================" << endl;
cout << "= Black Jack 21 =" << endl;
cout << "===========================================" << endl;
// money betting
PlayerScore = 1000 - bets;
cout << "You bet: $" << bets << " Money left: $" << endl;
// Get Player's hits. Calculate the score and redisplay after each hit.
do { // Do loop only runs once
// Displays the dealt cards, but only the House's second card.
cout << "House's Hand" << endl;
cout << "** "; // House's hidden other card
displayCard(HouseHand[1]); // House has one card currently
cout << endl;
cout << " Player's Hand: Score = " << scoreHand(PlayerHand, PlayerCardCount) << endl;
displayHand(PlayerHand, PlayerCardCount); // shows the player their hand
// Ask the Player whether he wants a hit or to stay or do a double
if (PlayerScore <= 21) { // If player's score is equal to or less than 21
cout << " Hit(h) or stay(s): ";
cin >> PlayerChoice; //Player's choice
}
// If the player choices to hit or stay option
if (PlayerChoice == 'h') { // H is for if the player wants another hit
PlayerHand[PlayerCardCount] = nextCard(CardsDealt);
PlayerCardCount++; // Adds one card to the player's card count
}
else if (PlayerChoice == 's') { // S is for if the player wants to stay
PlayerHits = false; // Does not add another card to the player's hand
}
else {
cout << "Error: Try Again!" << endl; // This is for anything other than D, H, and S.
}
cout << endl;
// Get the Player's current score to update and check for bust.
PlayerScore = scoreHand(PlayerHand, PlayerCardCount);
} // End of do loop
while (PlayerHits && PlayerScore < 22); // If the cards are less than 22 if this is true then the game is over and the player loses
// Once the player is done, a check is taken place to see if busted
if (PlayerScore > 21) {
// The Player busted. The House wins.
cout << "The House Wins!" << endl;
cout << "You lost $" << bets << endl;
}
else {
// If the player didn't bust, then the house takes hits below 17
int HouseScore = scoreHand(HouseHand, HouseCardCount);
while (HouseScore < 17) {
HouseHand[HouseCardCount] = nextCard(CardsDealt);
HouseCardCount++; // Adds to the house's card count
HouseScore = scoreHand(HouseHand, HouseCardCount); // Displays the house's hand
}
bool HouseBusts = (HouseScore > 21);
if (HouseBusts) { // The House busted. Player wins
cout << "You Win!" << endl;
bets = bets * 2;
cout << "You gained $" << bets << endl;
PlayerScore = bets + bets * 2;
}
// Compare scores and determine the winner
else if (PlayerScore == HouseScore) {
// Tie. This is called a "push."
cout << "Tie!" << endl;
cout << "You gained $0" << endl;
PlayerScore = bets + PlayerScore; // Allows the money from the round to be returned to the player and added to their score
}
else if (PlayerScore > HouseScore) {
// The Player wins
cout << "You Win!" << endl;
bets = bets * 2; // Gives money from both the house(player) to the winner(player) of the round
cout << "You gained $" << bets << endl;
PlayerScore = bets + bets * 2; // Money won is added to the winnner's account
}
else {
// The House wins
cout << "The House Wins!" << endl;
cout << "You lost $" << bets << endl << endl;
}
}
cout << "Would you like to play another game or end? yes(y) or no(n) " << endl; // Gives the option to play another round.
cin >> endgame;
}
}
// Shuffling the deck
void shuffle(bool CardsDealt[]) { // How the deck of cards is shuffled
for (int Index = 0; Index < 52; Index++) {
CardsDealt[Index] = false;
}
}
// Displays the card for the player and computer
void displayCard(int Card) { // The ranking of cards in the deck
int Rank = (Card % 13); // Rank of cards is 0-12
if (Rank == 0) {
cout << "Ace";
}
else if (Rank < 9) {
cout << (Rank + 1);
}
else if (Rank == 9) {
cout << "Ten";
}
else if (Rank == 10) {
cout << "Jack";
}
else if (Rank == 11) {
cout << "Queen";
}
else {
cout << "King";
}
int Suit = (Card / 13); // Allows the cards to be placed into different suits
if (Suit == 0) {
cout << " Club";
}
else if (Suit == 1) {
cout << " Diamond";
}
else if (Suit == 2) {
cout << " Heart";
}
else {
cout << " Spade";
}
}
//=============================================
// displayHand() =
// This is were the whole hand is displayed =
// this is different from displayCard because =
// it shows the whole hand not just one card =
//=============================================
void displayHand(int Hand[], int CardCount) {
for (int CardIndex = 0; CardIndex < CardCount; CardIndex++) {
int NextCard = Hand[CardIndex];
displayCard(NextCard);
cout << " ";
}
cout << endl;
}
//============================================
// nextCard() =
// A new card is dealt to the user and the =
// computer based on hit or who's turn =
//============================================
int nextCard(bool CardsDealt[]) {
bool CardDealt = true; // If newcard is required it is true
int NewCard = -1; // It is -1 because there is two less cards in the deck
do {
NewCard = (rand() % 52); // How the new card being dealt is randomized
if (!CardsDealt[NewCard]) { // If no new card is required
CardDealt = false; // No card is dealt
}
} while (CardDealt);
return NewCard; // Gives the new card back to the player
}
//============================================
// scoreHand() =
// The scoreHand is the score for the hand =
// If the user has 1 ace and 1 jack the =
// hand score will be 21 =
//============================================
int scoreHand(int Hand[], int CardCount) {
int AceCount = 0;
int Score = 0;
for (int CardIndex = 0; CardIndex < CardCount; CardIndex++) {
int NextCard = Hand[CardIndex];
int Rank = (NextCard % 13); // Ranking of cards is between 0-12
if (Rank == 0) {
AceCount++; // Different ways that the ace can be counted
Score++;
}
else if (Rank < 9) {
Score = Score + (Rank + 1);
}
else {
Score = Score + 10;
}
}
while (AceCount > 0 && Score < 12) {
AceCount--;
Score = Score + 10;
}
return Score; // Returned the scores from the round
}
Explanation / Answer
Explanation of everything that is there in the Code of BalckJack
/*
Final Project: Black Jack
Programmers: Brittany Tomblin and Kyle Galante
CIT163
2/13/18
Description: This program will simulate a real game of Black Jack with a dealer vs human. This Blackjack game
resembles the casino version of Black Jack this game where betting takes place.
*/
The text inside /*...*/, is called multiline comments, resembles a comment, theses are included to give an ease of understanding of the code to the programmer,
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <ctime> // Allows for time to be used
#include <vector> // Allows for the usage of vectors
The above statement tells the compiler, to include some prewritten decalrations in the code, to allow ceratin functionalities to achieve, that is
iostream:- this header file allows the user to create an stream between program and console, to allow the input and output
fstream:- this header file allows the user to enable the programmer to use the concepts of file handling, that is establishing a stream between a file and program can be achieved using this.
string:- this header file, holds the definition of several functions to be operated on a string, e.g. strcmp() {compares to strings], strcpy(){copy the text of one string to the another}, strlen(){computes length of the string} etc.
algorithm:- this header files holds the definition for the funtion, designed to work upon a range of elements, e.g. find(), find_if() etc.
ctime:- this contains time and date function declarations, to give away time and date manipulation and formating
vector:- This is used to define vector container class
using namespace std:- this shows a predefined standard namespace, to avoid occurence of anamoly between the names of user defined names, and predefined names, the baisc example of this is use of cout, cin, cerr as std::cout, std::cin, std::cerr
// Function Prototypes for Black Jack Game
The above line denotes a single line comment, a single line comment starts with //
void menu();
void gameComputer(bool CardsDealt[], int HouseHand[], int PlayerHand[], int HouseCardCount, int PlayerCardCount, int x);
void shuffle(bool CardDelt[]);
void displayCard(int Card);
void displayHand(int Hand[], int CardCount);
int nextCard(bool CardsDealt[]);
int scoreHand(int Hand[], int CardCount);
The above statements are called function prototypes or function declarations, that tells the basic details about a funcion declared inside the program to the compiler, the details required are return_type, name of the function, type of arguments the function takes.
that is,
void menu():- this function prtotype tells the compiler, function named menu(), will not return any value and will not accept any arguments is declared in the
void gameComputer(bool CardsDealt[], int HouseHand[], int PlayerHand[], int HouseCardCount, int PlayerCardCount, int x);- this function prototype tells the compiler, a function named gameComputer, that returns nothing and accepts 6 parameters,with types, bool, int[], int[], int, int, int, in this sequence, is defined in the program.
void shuffle(bool CardDelt[]):- this function prototype, tells the compiler, a function named shuffle, that returns nothing, ad accepts a parameter of type bool[], is defined in the program
void displayCard(int Card);- this function prototype tells the compiler, a function named displayCard, that returns nothing and accepts a integer value as argument, is defined in the program
void displayHand(int Hand[], int CardCount):- this function prototype tells the compiler, a function named displayHand, that returns nothing and accepts two parameters, int[] and int in that order, is defined in the program
int nextCard(bool CardsDealt[]);- this function prototype tells the compiler, a function names nextCard that returns an integer value and accepts a boll[], parameter, is defined in the program
int scoreHand(int Hand[], int CardCount):- this function prototype tells the compiler, afunction named scoreHand, that returns an integer value and accepts in[], int as its parameters in that sequence is defined in the program.
int main() //Compilation of the code starts from main(), this main() also returns an integer value
{ //start of main function
// For random numbers
srand(time(0));
/*srand() function is used to seed a integer value to randomizing the stream of random numbers, and time(0) returns the system time in seconds*/
// Declarations for variables
bool CardsDealt[52]; // Number of cards in the deck (0-51)
int HouseCardCount = 0; // Starting card count for the house
int HouseHand[12];
int PlayerCardCount = 0; // Starting card count for the player
int PlayerHand[12];
int x = 0;
// The menu for which game mode to play in
menu();
//Call for menu function, that displays the menu on the console
shuffle(CardsDealt);
/*Call for shuffle function, that initializes each card with the bool parameter false, to represent start of the game no card is dealt*/
gameComputer(CardsDealt, HouseHand, PlayerHand, HouseCardCount, PlayerCardCount, x);
/*Call for gameComputer, th elogic for entire game play resides in this function*/
// For windows to show final screen
system("pause");
//This pauses the output control, untill any input is made by the user
return 0; // Return success
//returns integer value 0
} //end of main() function
void menu() //Function definition for function menu()
{ //start function definition menu()
//cout, puts the output n the console with the input string, and endl shows the end of the line, that is nextline
cout << "**********************" << endl;
cout << "|Welcome to BlackJack|" << endl;
cout << "**********************" << endl;
cout << "Here are the rules: " << endl;
cout << " 1. The goal of the game is to assemble a hand whose numerical point value is as close to 21" << endl;
cout << " without going over. Whoever gets closes to 21 without going over wins. " << endl;
cout << " 2. Aces are worth either 1 point or 11 points at the players dicretion. Picture cards are worth" << endl;
cout << " 10 points. All other cards are worth a number of points equal to their numerical value. " << endl;
cout << " 3. The dealer must draw on 17 or less and hold on 18 or more. " << endl;
cout << "LET'S START THE GAME !" << endl;
}//end of function deinition menu()
void gameComputer(bool CardsDealt[], int HouseHand[], int PlayerHand[], int HouseCardCount, int PlayerCardCount, int x) //function definition for gameComputer() function
{ //start of function definition gameComputer()
//Declaring Variables
char endgame;
endgame = 'n';
int bets;
// Loops for hand (player)
//loop until the end of the game
while ((endgame != 'n' || endgame != 'N')) {
// Shuffles the cards
//call shuffle(), to initialize the cards, to begin with the game play
shuffle(CardsDealt); // Shuffles the deck of cards before the beginning of the game
// Two cards are dealt for each player and house
//call function nextCard(), to deal four cards two cards to each player
PlayerHand[0] = nextCard(CardsDealt); // first card
HouseHand[0] = nextCard(CardsDealt); // first card
PlayerHand[1] = nextCard(CardsDealt); // second card
HouseHand[1] = nextCard(CardsDealt); // second card
//Change the number of cards holded by each player
HouseCardCount = 2; // total number of cards
PlayerCardCount = 2; // total number of cards
//Declaring more Variables
char PlayerChoice = ''; // allows the player to choose what they want to do with their 2 cards
bool PlayerHits = true;
//call function scoreHand(), to check the current score of the player
int PlayerScore = scoreHand(PlayerHand, PlayerCardCount);
// The start of a new hand
cout << "===========================================" << endl;
cout << "= Black Jack 21 =" << endl;
cout << "===========================================" << endl;
//Declare an initial amount for the user, and place in the bets to create a pot
cout << "Money: $" << "1000" << endl; // This pulls the users money amount up
//bet placed is matched by the house to create a pot
cout << "Place your bets!" << endl;
cout << "Bet: ";
cin >> bets;
//checks that user cannot place a bet more than he has the money in his account
while (bets > 1000) { // Validation if user enters more money than in account
cout << "You entered a bet more than you have in your account. "
<< "Please enter your bet: ";
cin >> bets;
}
// The start of a new hand
cout << "===========================================" << endl;
cout << "= Black Jack 21 =" << endl;
cout << "===========================================" << endl;
//Deduct the betted amount from players account and hold onto it until player wins
// money betting
PlayerScore = 1000 - bets;
cout << "You bet: $" << bets << " Money left: $" << endl;
// Get Player's hits. Calculate the score and redisplay after each hit.
do { // Do loop only runs once
// Displays the dealt cards, but only the House's second card.
cout << "House's Hand" << endl;
cout << "** "; // House's hidden other card
//call displayCard() to show the cards holded by house
displayCard(HouseHand[1]); // House has one card currently
cout << endl;
//call scoreHand(), to show the current score of player's hand
cout << " Player's Hand: Score = " << scoreHand(PlayerHand, PlayerCardCount) << endl;
//call displayHand(), to show all cards currently holded by the player
displayHand(PlayerHand, PlayerCardCount); // shows the player their hand
// Ask the Player whether he wants a hit or to stay or do a double
if (PlayerScore <= 21) { // If player's score is equal to or less than 21
cout << " Hit(h) or stay(s): ";
cin >> PlayerChoice; //Player's choice
}
// If the player choices to hit or stay option
if (PlayerChoice == 'h') { // H is for if the player wants another hit
PlayerHand[PlayerCardCount] = nextCard(CardsDealt);
PlayerCardCount++; // Adds one card to the player's card count
}
else if (PlayerChoice == 's') { // S is for if the player wants to stay
PlayerHits = false; // Does not add another card to the player's hand
}
else {
cout << "Error: Try Again!" << endl; // This is for anything other than D, H, and S.
}
cout << endl;
// Get the Player's current score to update and check for bust.
PlayerScore = scoreHand(PlayerHand, PlayerCardCount);
} // End of do loop
//loosing condition for the player is a playerhit leads to the count of PlayerScore less then 22
while (PlayerHits && PlayerScore < 22); // If the cards are less than 22 if this is true then the game is over and the player loses
// Once the player is done, a check is taken place to see if busted
if (PlayerScore > 21) {
// The Player busted. The House wins.
cout << "The House Wins!" << endl;
cout << "You lost $" << bets << endl;
}
else {
// If the player didn't bust, then the house takes hits below 17
int HouseScore = scoreHand(HouseHand, HouseCardCount);
while (HouseScore < 17) {
HouseHand[HouseCardCount] = nextCard(CardsDealt);
HouseCardCount++; // Adds to the house's card count
HouseScore = scoreHand(HouseHand, HouseCardCount); // Displays the house's hand
}
bool HouseBusts = (HouseScore > 21);
if (HouseBusts) { // The House busted. Player wins
cout << "You Win!" << endl;
bets = bets * 2;
cout << "You gained $" << bets << endl;
//Player wins add total amount in the pot to the amount of player
PlayerScore = bets + bets * 2;
}
// Compare scores and determine the winner
else if (PlayerScore == HouseScore) {
// Tie. This is called a "push."
cout << "Tie!" << endl;
cout << "You gained $0" << endl;
PlayerScore = bets + PlayerScore; // Allows the money from the round to be returned to the player and added to their score
}
else if (PlayerScore > HouseScore) {
// The Player wins
cout << "You Win!" << endl;
bets = bets * 2; // Gives money from both the house(player) to the winner(player) of the round
cout << "You gained $" << bets << endl;
PlayerScore = bets + bets * 2; // Money won is added to the winnner's account
}
else {
// The House wins
cout << "The House Wins!" << endl;
cout << "You lost $" << bets << endl << endl;
}
}
//Asks the user if he wants to play more
cout << "Would you like to play another game or end? yes(y) or no(n) " << endl; // Gives the option to play another round.
cin >> endgame;
}
}//end of function definition
void shuffle(bool CardsDealt[]) //Initialize the each value of the card to false to show begining of the game
{ //start function definition shuffle()
for (int Index = 0; Index < 52; Index++) { //loop through each value in the deck
CardsDealt[Index] = false; //assign flase to show that the card is not dealt till now
} //end of loop
}//end of function definition
void displayCard(int Card) // The ranking of cards in the deck
/*for the cards 2, 3, 4, 5, 6, 7, 8, 9,10 rank is the value of card
for ace the rank is 11
for card value Jack, rank is 10
for card value Queen, rank is 10
for card value King, rank is 10
*/
{
int Rank = (Card % 13); // Rank of cards is 0-12
if (Rank == 0) {
cout << "Ace";
}
else if (Rank < 9) {
cout << (Rank + 1);
}
else if (Rank == 9) {
cout << "Ten";
}
else if (Rank == 10) {
cout << "Jack";
}
else if (Rank == 11) {
cout << "Queen";
}
else {
cout << "King";
}
int Suit = (Card / 13); // Allows the cards to be placed into different suits
//this chooses the suit of the dealt card
if (Suit == 0) {
cout << " Club";
}
else if (Suit == 1) {
cout << " Diamond";
}
else if (Suit == 2) {
cout << " Heart";
}
else {
cout << " Spade";
}
}//end of function definition
void displayHand(int Hand[], int CardCount) //displays the cards in hand
{//start of function defintion
//loop through each index of the crds holded in the hand
for (int CardIndex = 0; CardIndex < CardCount; CardIndex++) {
int NextCard = Hand[CardIndex];
displayCard(NextCard); //Call function displayHand(), to show each card on the console
cout << " ";
}
cout << endl;
}// end of function definition
int nextCard(bool CardsDealt[])
//function definition of nextCard(), to generate a newCard each the deck is dealt
{//start function definition
bool CardDealt = true; // If newcard is required it is true
int NewCard = -1; // It is -1 because there is two less cards in the deck
do {
//genertae a random number between0 to 51 and return the number alog to represent the card
NewCard = (rand() % 52); // How the new card being dealt is randomized
if (!CardsDealt[NewCard]) { // If no new card is required
CardDealt = false; // No card is dealt
}
} while (CardDealt);
return NewCard; // Gives the new card back to the player
}//end of function definition
int scoreHand(int Hand[], int CardCount) { //Calculates the score of the hands of user and house
/* Score can be evaluated wih the cards holded by the player*/
int AceCount = 0;
int Score = 0;
for (int CardIndex = 0; CardIndex < CardCount; CardIndex++) {
int NextCard = Hand[CardIndex];
int Rank = (NextCard % 13); // Ranking of cards is between 0-12
if (Rank == 0) {
AceCount++; // Different ways that the ace can be counted
Score++;
}
else if (Rank < 9) {
Score = Score + (Rank + 1);
}
else {
Score = Score + 10;
}
}
while (AceCount > 0 && Score < 12) {
AceCount--;
Score = Score + 10;
}
return Score; // Returned the scores from the round
}//end of function definition
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.