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

Enter the number of players in the game. There must be at least one player but n

ID: 3628221 • Letter: E

Question

Enter the number of players in the game.
There must be at least one player but no more than four.
Number of players: 3

Dealer Player 1 Player 2 Player 3
Card 1: 5 7 4 Q
Card 2: 5 K 5 2
Card 3: J 4 6 10
Card 4: Hold Hold Q Hold
Card 5: Hold Hold Hold Hold

Final: 20 21 25 22
Lose Win! Lose Lose


These are the program requirements:

1)There is always a dealer in the game. At the start of the game, the dealer’s first card will not be shown or displayed. The second card will be displayed. The dealer may draw additional cards.
2)The dealer must use a random-number generator to determine the maximum number of cards the dealer will draw--a value between 0 and 3. In other words, the dealer is a computer player.
3)The dealer does not show all the cards or the total until all the players have either gone bust (over 21) or hold (no more cards drawn).
4)There must be at least one other player (you) and up to a maximum of four other players (all played by you).
5)On a player’s turn, that player may either draw a card or hold. Once a player holds, he or she should not be asked to draw another card during this game.
6)All the cards for each player, including the first card dealt, are displayed, along with the suit symbol: spades ?, clubs ?, hearts ?, or diamonds ?.
7)Each game will start with a new, 52-card deck, which is modeled on a real deck of cards. The card deck has 52 cards with no jokers. The card deck is represented by a two-dimensional array of data-type character, where the first dimension represents the suit and the second dimension represents the card in the suit, such as the following.
char CardDeck[4][13];
8)At the start of each game, each element of the two-dimensional array is initialized to a value of " ", or the "space" character.
The deck has four suits, represented by the following dimension indices.
0 = diamonds
1 = hearts
2 = clubs
3 = spades
9)Each suit has 13 cards: 2, 3, 4, 5, 6, 7, 8,9 ,10, jack, queen, king, and ace.
Each card in a suit is represented by the following dimension indices.
0 = the 2 card
1 = the 3 card
2 = the 4 card
3 = the 5 card
4 = the 6 card
5 = the 7 card
6 = the 8 card
7 = the 9 card
8 = the 10 card
9 = the jack
10 = the queen
11 = the king
12 = the ace
All the number cards are worth their face value (i.e., a 3 of diamonds is worth 3).
All face cards are worth 10.
An ace is worth either 1 or 11. Your final-score calculation must be able to handle this correctly for both the dealer and each player.
10)A random-number generator must be used to select the suit and the card in the suit.
11)Once a card and suit are selected, the program should check if the value of that array element is a "space." If the array element = "space," set the element equal to an integer, identifying the dealer or the player.
0 = dealer
1 = player 1
2 = player 2
3 = player 3
4 = player 4
If the array element ! = "space," then the random-number and card-checking process should repeat until a "card" or an array element is selected that is = "space."
12)Once a card is drawn during a game, it cannot be drawn again.
When the program first starts, it should prompt the user, asking if he or she wants to play a game of Blackjack or exit the program.
If the user inputs to play the game, the next decision should be 1, 2, 3, or 4 players.
At the start of the game, the dealer and each player should be dealt two cards. One of the dealer’s card's value and suit should not be displayed.
The number of cards that the dealer will draw during a game should be determined by a random-number generator that will return a value of 0, 1, 2, or 3 cards to be drawn.
Each player may then draw a card or hold.
If, after drawing a card, any player or the dealer goes over a score of 21, he or she is not allowed to draw any more cards during the game.
Once a player holds, he or she should not be asked to draw a card again during the game.
The game continues until one of the following conditions occur:
all players have declared hold;
all players and the dealer have gone over 21;
a maximum of five cards total are held by any player at the end of a round of card draws; or
any combination of the above.
The display should show each player’s (and the dealer’s) hand and update the display after each round of card draws.

Real code is red, Pseudocode is black and comments
are (blue).

Explanation / Answer

please rate - thanks

#include <iostream>
#include <iomanip>
#include <ctime>
#include <string>
using namespace std;
void deal(int ,char [4][13]);
void printcards(int,char[4][13]);
int getPlayers();
void deal1(char[][13],int);
void scoreCards(char[4][13],int[],int,int&);
void initCards(char[][13],int&,int&,bool[],int&);
bool draw(int,int);
int getwinner(int[],int);
int playagain(int);
void final(int[],int,int,char[][13]);
int main ()
{char CardDeck[4][13];
int num,dealer,round;
bool playing[5];
int score[6];
int i,k, m,done,over21;
bool play=true;
srand(time(0));
srand(6);    
num=getPlayers();
while(num>=1)
{
initCards(CardDeck,round,dealer,playing,done);
deal(num,CardDeck);
printcards(num,CardDeck);
scoreCards(CardDeck,score,num,over21);
while(done<num&&over21<num+1&&round<5)
{round++;
if(round<=dealer&&score[0]<21)
     deal1(CardDeck,0);
for(i=1;i<=num;i++)
{
if(playing[i])
    if(draw(i,score[i]))
         deal1(CardDeck,i);
    else
       {playing[i]=false;
       done++;
       }
}
printcards(num,CardDeck);
scoreCards(CardDeck,score,num,over21);
}
i=getwinner(score,num);
cout<<" GAME OVER ";
final(score,num,i,CardDeck);
num=playagain(num);

}   
system("pause");
return 0;
}
int playagain(int num)
{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;
}
void final(int s[],int n,int w ,char CardDeck[][13])
{int i,j,k,maxx=0;
string card[]={"2","3","4","5","6","7","8","9","10","jack","queen","king","ace"};
int code[]={4,3,5,6};
int cards[5][7][2],count[5]={0};
cout<<" Dealer ";
for(i=1;i<=n;i++)
    cout<<"Player "<<i<<" ";
cout<<endl;
for(j=0;j<4;j++)
for(k=0;k<13;k++)
       if(CardDeck[j][k]!=' ')
                   {cards[CardDeck[j][k]][count[CardDeck[j][k]]][0]=j;
                   cards[CardDeck[j][k]][count[CardDeck[j][k]]][1]=k;
                   count[CardDeck[j][k]]++;
                        if(count[CardDeck[j][k]]>maxx)
                               maxx=count[CardDeck[j][k]];
                   }
for(i=0;i<maxx;i++)
    {cout<<"Card "<<i+1<<":"<<" ";
     for(j=0;j<=n;j++)
         if(i>=count[j])
             cout<<"Hold ";
         else
             cout<<card[cards[j][i][1]]<<(char)code[cards[j][i][0]]<<" ";
      cout<<endl;
      }
cout<<"Final: ";
for(i=0;i<=n;i++)
       cout<<s[i]<<" ";
cout<<endl<<" ";
for(i=0;i<=n;i++)
       if(i==w)
           cout<<"Win! ";
       else
           cout<<"Lose ";
cout<<endl;

}

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;
if(s[w]>21)
     return 10;
return w;
}

bool draw(int i,int s)
{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;
}
void scoreCards(char c[4][13],int s[],int n,int& over)
{int i,j,k,aces=0;
over=0;
for(i=0;i<=n;i++)
   {aces=0;
   s[i]=0;
   for(j=0;j<4;j++)   
        for(k=0;k<13;k++)
             {if(c[j][k]==i)
                 {if(k<9)
                      s[i]+=(k+2);
                 else if(k<12)
                      s[i]+=10;
                 else
                    {aces++;
                   
                    }
                  }
              
                      }
               if(aces>0)
                  {if(aces>1)
                       s[i]+=aces;
                   else
                       {
                        if(s[i]+11>21)
                              s[i]++;
                        else
                              s[i]+=11;
                              }
                   }     
       if(s[i]>21)
           over++;
       }
}               
void initCards(char CardDeck[][13],int& round,int& dealer,bool p[],int& done)
{int i,k,m;
round=0;
done=0;
dealer=rand()%4;
for(k=0;k<4;k++)
     for(m=0;m<13;m++)
          CardDeck[k][m]=' ';
for(i=0;i<5;i++)
     p[i]=true;

}
int getPlayers()
{int num;
cout<<"Welcome to Honest Sam's Blackjack Table ";
cout<<"Glad to have you back! ";
cout<<"Enter the number of players in the game. ";
cout<<"There must be at least one player but no more than four. ";
cout<<"Number of players: ";
cin>>num;
while(num<1||num>4)
    {cout<<"invalid entry ";
    cout<<"There must be at least one player but no more than four. ";
    cout<<"How many players in the game (1-4)? ";
    cin>>num;
    }
return num;
}
void deal(int players,char CardDeck[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(CardDeck[l][k]!=' ');
           CardDeck[l][k]=i;                    
          }
   
}
void printcards(int players,char CardDeck[4][13])
{int i,j,k;
string card[]={"2","3","4","5","6","7","8","9","10","jack","queen","king","ace"};
int code[]={4,3,5,6};
bool d=false;
for(i=0;i<=players;i++)
     {if(i==0)
          cout<<"Dealers hand ";
     else
         cout<<"Player "<<i<<"s hand ";
      for(j=0;j<4;j++)
          for(k=0;k<13;k++)
               if(CardDeck[j][k]==i)
                   if(i!=0||d)
                       cout<<(char)code[j]<<" "<<card[k]<<endl;
                   else
                       d=true;   
          }
}