Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The most important part of the lab specifications that I need to emphasize up fr

ID: 3551898 • Letter: T

Question

The most important part of the lab specifications that I need to emphasize up front is that there is no interaction with the user except to ask if the user wants to play and how many players are playing. After that information is gathered by the program, the drawing of the cards, calculating results and displaying is done automatically by your program and the user just watches the results (as explained below). This is supposed to be a simulation of a game, not a real, interactive game. In a real game, you (the user) would decide whether to hit or hold. In this program, you do not have this capability. Because of this, the design is much simpler.

Specification 1 says that the dealer's first card is not displayed at first. Completely disregard this nonsense because the cards are displayed when drawn (there is no going back to redisplay after the game is finished). The cards are displayed incrementally (first card for each player first, then second card for each player, etc.).

Specification 3i says that an ace is worth 1 or 11 points. To make the design easier, please just set the value to 11 only.

Specification number seven says that the number of cards drawn for a player is based on a random number. You should interpret from what it says: if the total of the cards plus a randomly generated value from 0 to 3 is greater than 18, hold (also if the total is 21). Otherwise, draw another card. I must say when I read the written specification, I did not come to this conclusion myself, but what I am explaining is the correct interpretation, so disregard what spec. 7 actually says.

Specification 8 is needless because there is no interaction with the user when drawing cards (the drawing is controlled by random numbers and the user just sees the result after the program has been internally drawing the cards and adding totals).

Specification 11 is very confusing. Consider this instead: your program continues to draw cards until all the players either have hold, 21, or bust (the hold is determined based on a random number as explained above).

For those familiar with how BlackJack is actually played, keep in mind that (in this simulation program) the first cards for each player are dealt, then the second cards for each player are dealt, then the third cards, etc. No third cards are played until all second cards have been dealt, similarly no fourth cards are played until all third cards have been dealt (if a player is holding, no further cards for that player, of course). In a real game, each player continues to hit until a decision to hold (plus some other options not included in this simulation because there is no betting). In a real game, the dealer is last to draw more cards to give the house the advantage. In this program, the dealer gets a card before the other players for each set of draws.

I suggest that your program implement the decision algorithm after each card is dealt, starting with the first cards (to make things simpler). It will only be relevant after two cards are already played for each player.



I have my code written but it is not outputting anything, here is my code! Please use my code and correct my code if you will!! Thank you so much!! I am new to C++ and I am stuck!!!!!!


// Programmer:


// Course: COMP220


// Assignment: Two-Dimensional Arrays


// Description: The program will use a 2D array and a random-number

// generation to play Blackjack and keep track of a playing-card deck.


// Input: User data entry and a playing-card deck represented as a two-

// dimensional array


// Output: A screen display showing the current card hands of each player

// and the dealer, their score, win and lose status, and a final representation

// of the card deck after the game is over


#include <iostream>

#include <iomanip>

#include <windows.h>

#include <string>


using namespace std;


//Prototypes

void initializeDeck(char cCardDeck[][13], int& round, int& dealer, bool p[], int& done);

int iNumberOfPlayers();

void deal(int players, char cCardDeck[4][13]);

void displayCards(int players, char cCardDeck[4][13]);

void scoreCards(char cCardDeck[4][13], int s[], int n, int& over);

bool draw(int i, int s);

void deal1(char CardDeck[][13],int n);

int getWinner(int s[],int num);

void final(int s,int n,int w ,char CardDeck[][13]);

int playAgain (int num);


int main ()


{

bool bPlayerDraw[5]; //Boolean to determine if player holds (F)

//or draws card (T)

char cPlay = 'N'; //Character variable for play game input

char cCardDeck[4][13]; //Character array representing the card deck

int iSuit;

//0 = 2 card

//1 = 3 card

//2 = 4 card

//3 = 5 card

//4 = 6 card

//5 = 7 card

//6 = 8 card

//7 = 9 card

//8 = 10 card

//9 = jack card

//10 = queen card

//11 = king card

//12 = ace card

int iNumberOfDraws = 0; //Number of rounds of card draws


//0 = diamonds

//1 = hearts

//2 = clubs

//3 = spades


// ASCII character display reference for display card suit symbols

//3 = heart symbol

//4 = diamond symbol

//5 = club symbol

//6 = spade symbol


int iNumberOfPlayers = 0; //Number of players in current game

//iPlayer[0] is always the dealer

int iHighestCount = 0; //Highest count for a single game

int i, k; //integer loop counters

srand(GetTickCount()); //Seed the random-number generator

int num, dealer, round, done, over21;

int score[5];


while (iNumberOfPlayers >= 1)

{

initializeDeck(cCardDeck, round, dealer, bPlayerDraw, done);

deal(num, cCardDeck);

displayCards(num, cCardDeck);

scoreCards(cCardDeck, score, num, over21);

while(done < num && over21 < num + 1 && round < 5)

{

round++;

if(round <= dealer)

deal1(cCardDeck, 0);

for(i =1; i <= num; i++)

if (bPlayerDraw[i])

if(draw(i, score[i]))

deal1(cCardDeck,i);

else

{

bPlayerDraw[i] = false;

done++;

}

displayCards(num, cCardDeck);

scoreCards(cCardDeck,score, num, over21);

}

i = getWinner(score, num);

cout << " GAME OVER"<< endl;

for (k = 0; k <= num; k++)

final(score[k], k, i, cCardDeck);

num = playAgain(num);

}

system("pause");


}

void initializeDeck(char cCardDeck[][13], int& round, int& dealer, bool p[], int& done)

{

{

round = 0;

done = 0;

dealer = rand() % 4;

//Initialize card array with "space"

for (int i = 0; i < 4; i++)

for (int j = 0; j < 13; j++)

cCardDeck[i][j] = ' ';

for(int k = 0; k < 4; k++)

p[k] = true;

}

}


int iNumberOfPlayers()

{

int num;

cout << "Welcome to Honest Sam's Blackjack Table."

<< " Glad to have you back!"

<< " Enter the number of players in the game."

<< " There must be at least 1 player but no more than four."

<< " Number of players:";

cin >> num;

while(num < 1 || num > 4) //Check to ensure correct user input

{

cout << "There must be at least one player but no more than four."

<< " Number of players:";

cin >> num;

}

return num;

}

void deal(int players, char cCardDeck[4][13])

{

int i, j, k, l;


for (i = 0; i < players; i++)

for (j = 0; j < 2; j++)

{

do

{

k = rand () % 13;

l = rand () % 4;

}while(cCardDeck[l][k] != ' ');

cCardDeck[1][k] = i;

}

}

void displayCards(int players, char cCardDeck[4][13])

{

int i, j ,k;

string card[] = {"2","3","4","5","6","7","8","9","10", "jack", "queen","king","ace"};

int iSuit[] = {3, 4, 5, 6};

bool d = false;

for (i = 0; i <= players; i++)

{

if (i == 0)

cout << "Dealers hand ";

else

cout << "Player" << i << "hand ";

for(j = 0; j < 4; j++)

for (k = 0; k< 13; k++)

if (cCardDeck[j][k] == i)

if ( !0 || d)

cout << (char) iSuit[j] << " " << card[k] << endl;

else

d = true;

}

}


void scoreCards(char cCardDeck[4][13], int s[], int n, int& over)

{

int i, j ,k;

over = 0;

for (i = 0; i <= n; i++)

{

s[i] = 0;

for (j = 0; j < 4; j++)

for(k = 0; k < 13; k++)

{

if (cCardDeck[j][k] == i)

{

if(k < 9)

s[i]+=(k+2);

else if(k < 12)

s[i]+= 10;

else

if(s[i]+11>21)

s[i]++;

else

s[i]+=11;

}

if (s[i]>21)

over++;

}

}

}

bool draw(int i, int s) //Prompt player to hold or draw

{

char h;

if(s>21)

return false;

cout << "Player" << i << "hold or draw?";

cin >> h;

if (toupper(h) == 'H')

return false;

return true;

}


void deal1(char CardDeck[][13],int n)

{

int k,l;

do

{

k=rand()%13;

l=rand()%4;

}while(CardDeck[l][k]!=' ');

CardDeck[l][k]=n;

}


int getWinner(int s[],int num)

{

int i,w;

for(i=0;i<=num;i++)

if(s[i]<=21)

{w=i;

i=num+3;

}

for(i=0;i<=num;i++)

if(s[i]>s[w]&&s[i]<=21)

w=i;

return w;

}

void final(int s,int n,int w ,char CardDeck[][13])

{

int i,j,k;

string card[]={"2","3","4","5","6","7","8","9","10","jack","queen","king","ace"};

int iSuit[] = {4,3,5,6};

if(n==0)

cout<<"Dealers hand ";

else

cout<<"Player "<<n<<"s hand ";

for(j=0;j<4;j++)

for(k=0;k<13;k++)

if(CardDeck[j][k]==n)

cout<<(char)iSuit[j]<<" "<<card[k]<<", ";

cout<<s<<" ";

if(n==w)

cout<<"Win! ";

else

cout<<"Lose ";


}

int playAgain (int num) // Prompt user to play again, yes or no

{

int a=0,i;

char again;

for(int i=1;i<=num;i++)

{

cout<<"player "<<i<<" play again(y/n)? ";

cin>>again;

if(toupper(again)=='Y')

a++;

}

return a;

}





Explanation / Answer

#include <iostream> #include <ctime> using namespace std; // Global Variables // char *card_num[] = {"2","3","4","5","6","7","8","9","10","Jack","Queen","King","Ace"}; char *card_type[] = {"Clubs","Diamonds","Spades","Hearts"}; // Structures // struct cardtype {     int color;     int number;     int value; }; struct decktype {     bool deck[52];     int size; }; // Functions // void SeedRandom() {     time_t tTime;     time(&tTime);     srand(tTime);     rand(); } struct cardtype PickACard(struct decktype &deck) {     struct cardtype card;     int randnum = (int) (1.0 * rand() / (RAND_MAX + 1) * deck.size);     int counter = 0;     int i;     for (i=0; i<52; ++i) {         if (deck.deck[i] == true) {             ++counter;         }         if (counter == randnum+1) {             break;         }     }     --deck.size;     deck.deck[i] = false;     card.number = i / 4;     card.color = i % 4;     if (card.number == 10 || card.number == 11) {         card.value = 10;     } else if (card.number == 9) {         card.value = 1;     } else if (card.number == 12) {         card.value = 11;     } else {         card.value = card.number + 2;     }     return card; } struct decktype DeckInit() {     struct decktype deck;     deck.size = 52;     for (int i=0; i<52; ++i) {         deck.deck[i] = true;     }     return deck; } char *TextCard(struct cardtype &card) {     char *str = (char *) malloc (22 * sizeof(char));     sprintf(str,"%s of %s (%d)",card_num[card.number],card_type[card.color],card.value);     return str; } // Main // void main() {     SeedRandom();     bool IsGameOver = false;     char WantCard;     int totalvalue = 0;     cout << "Welcome to blackjack!" << endl;     struct decktype deck = DeckInit();     while (!IsGameOver) {         bool ValidChoice = false;         while (!ValidChoice) {             cout << "Do you want a card? (Y)es / (N)o" << endl;             cin >> WantCard;             if (WantCard == 'Y') {                 struct cardtype card = PickACard(deck);                 totalvalue += card.value;                 cout << "You received a " << TextCard(card) << ", current total value at " << totalvalue << "." << endl;                 if (totalvalue > 21) {                     cout << "Sorry, you have exceeded 21 points and thus lost!" << endl;                     IsGameOver = true;                 }                 if (totalvalue == 21) {                     cout << "Congratulations, you have won!" << endl;                     IsGameOver = true;                 }                 ValidChoice = true;             } else if (WantCard == 'N') {                 cout << "You stopped prematurely, and since there isn't an opponent, you won :)" << endl;                 ValidChoice = true;                 IsGameOver = true;             } else {                 cout << "Invalid choice, please try again";             }         }     }     cout << "The game is over now, thanks for playing!" << endl; cin.ignore(); cin.get(); }
#include <iostream> #include <ctime> using namespace std; // Global Variables // char *card_num[] = {"2","3","4","5","6","7","8","9","10","Jack","Queen","King","Ace"}; char *card_type[] = {"Clubs","Diamonds","Spades","Hearts"}; // Structures // struct cardtype {     int color;     int number;     int value; }; struct decktype {     bool deck[52];     int size; }; // Functions // void SeedRandom() {     time_t tTime;     time(&tTime);     srand(tTime);     rand(); } struct cardtype PickACard(struct decktype &deck) {     struct cardtype card;     int randnum = (int) (1.0 * rand() / (RAND_MAX + 1) * deck.size);     int counter = 0;     int i;     for (i=0; i<52; ++i) {         if (deck.deck[i] == true) {             ++counter;         }         if (counter == randnum+1) {             break;         }     }     --deck.size;     deck.deck[i] = false;     card.number = i / 4;     card.color = i % 4;     if (card.number == 10 || card.number == 11) {         card.value = 10;     } else if (card.number == 9) {         card.value = 1;     } else if (card.number == 12) {         card.value = 11;     } else {         card.value = card.number + 2;     }     return card; } struct decktype DeckInit() {     struct decktype deck;     deck.size = 52;     for (int i=0; i<52; ++i) {         deck.deck[i] = true;     }     return deck; } char *TextCard(struct cardtype &card) {     char *str = (char *) malloc (22 * sizeof(char));     sprintf(str,"%s of %s (%d)",card_num[card.number],card_type[card.color],card.value);     return str; } // Main // void main() {     SeedRandom();     bool IsGameOver = false;     char WantCard;     int totalvalue = 0;     cout << "Welcome to blackjack!" << endl;     struct decktype deck = DeckInit();     while (!IsGameOver) {         bool ValidChoice = false;         while (!ValidChoice) {             cout << "Do you want a card? (Y)es / (N)o" << endl;             cin >> WantCard;             if (WantCard == 'Y') {                 struct cardtype card = PickACard(deck);                 totalvalue += card.value;                 cout << "You received a " << TextCard(card) << ", current total value at " << totalvalue << "." << endl;                 if (totalvalue > 21) {                     cout << "Sorry, you have exceeded 21 points and thus lost!" << endl;                     IsGameOver = true;                 }                 if (totalvalue == 21) {                     cout << "Congratulations, you have won!" << endl;                     IsGameOver = true;                 }                 ValidChoice = true;             } else if (WantCard == 'N') {                 cout << "You stopped prematurely, and since there isn't an opponent, you won :)" << endl;                 ValidChoice = true;                 IsGameOver = true;             } else {                 cout << "Invalid choice, please try again";             }         }     }     cout << "The game is over now, thanks for playing!" << endl; cin.ignore(); cin.get(); }