This lab requires you to design and implement a C++ program to simulate a game o
ID: 3628255 • Letter: T
Question
This lab requires you to design and implement a C++ program to simulate a game of Blackjack between two to four players. Your program must incorporate a two-dimensional array to represent the suit and the value of each card dealt to a player, keep track of which cards have been dealt to which player, and use a random-number generator to pick each card to be dealt to a player.
Below is the code and/or Pseudocode for main(). Real code is red, Pseudocode is black and comments are (blue).
My new changes to fix the card/suit display are in Green
#include <iostream>
#include <iomanip>
#include <windows.h>
#include <string>
using namespace std;
begin function void main ()
bool bPlayerDraw[5];
char cPlay = 'N';
char cCardDeck[4][13];
int iPlayerCount[5];
int iHighestCount = 0;
int suit, card;
int cNum, pNum;
string cardStr;
int cardValue[13]={2,3,4,5,6,7,8,9,10,10,10,10,11};
string cardName[13]={" 2"," 3"," 4"," 5"," 6"," 7"," 8"," 9","10"," J"," Q"," K"," A"}; // New array to hold card names
char suitSymbol[4]={char(4),char(3),char(5),char(6)}; //Name Change and data type change
srand(GetTickCount());
//Pseudocode
do
Clear Screen
Display User Heading and skip 2 lines
Prompt/Input/Error-Handle (1..4): numPlayers
(The deck is cleared by setting all the values to ' ')
for suit = 0 to 3
for card = 0 12
set cCardDeck[suit][card] to ’ ’
endfor
endfor
(Set Players Count to 0 Draw to true)
for player = 0 to 4
iPlayerCount[player] = 0
bPlayerDraw[player] = true
endfor
set iHighestCount to 0
Use setw(), left, right, etc for formatting columns
Display Headers: Dealer Player1 Player2 .. PlayerN:
(For each card to each player, determine card and suit)
(If card choosen already played, choose another card )
(Mark the deck location with 'X' to indicate card is played)
for cNum = 0 to 4
Display “Card ” & draw+1 & ”: ” &
for pNum = 0 to numPlayers
do
suit = rand()%4;
card = rand()%13;
while cCardDeck[suit][card] not ’ ’
(If Player count is 21 or > 18-(random# 0..2), set Draw to false)
if iPlayerCount[pNum] is 21 OR iPlayerCount[pNum] > 18 - rand()%3
bPlayerDraw[pNum] = false;
(If Player Draw is true, mark card in deck as played, )
(display card and increment Player count by card value)
if bPlayerDraw[pNum] is true
cCardDeck[suit][card] = ’X’
cardStr=cardName[card]+suitSymbol[suit]
display column aligned card$
iPlayerCount += cardValue[card]
else display “Hold”
endfor
endfor
Display “ Final: "
for pNum = 0 to iNumberOfPlayers
Display column aligned iPlayerCount[pNum]
endfor
(Find Highest Count <= 21)
for pNum = 0 to iNumOfPlayers
if iPlayerCount[pNum] !=21 AND > iHighestCount
iHighestCount = iPlayerCount[pNum]
endfor
(All Players whose count = iHighestCount "Win!")
(All other Players "Lose"
for pNum = 0 to iNumberOfPlayers
if iPlayerCount[pNum] is iHighestCount
Display column aligned " Win! ";
else
Display column aligned " Lose ";
endfor
skip 2 lines
Prompt/Input “Play Again Y/N?” : cPlay
while cPlay is ‘Y’ or ‘y’
(Please Note that I am NOT Asking you to Display the cards Used From the Deck!!!)
Explanation / Answer
please rate - thanks
see what you can do with this
#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;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.