But I am trying to break it down into the different parts that I need in order t
ID: 3628200 • Letter: B
Question
But I am trying to break it down into the different parts that I need in order to create the program in C I started to rigt a few of the program codes but I ma all over the place and I am getting a little confused and need help. I believe I have all of of the variabls that are needed to make the program work. Oh yeah it is for a blackjack program
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};
int suitValue[4]={char(4),char(3),char(5),char(6)};
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 ' ')
char cCardDeck[4][13];
//for suit = 0 to 3
int suit=0
int suitValue[suit]={char(4),char(3),char(5),char(6)};
//for card = 0 to 12
int card=0
int cardValue[card]={2,3,4,5,6,7,8,9,10,10,10,10,11};
char 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
int iHighestCount = 0;
//Use setw(), left, right, etc for formatting columns
//Display Headers: Dealer Player1 Player2 .. PlayerN:
int pNum = 0;
int iplayercount[pNum] = {Dealer, Player1, Player2, Player3, Player4};
for (int i=0; i < pNum; i++)
{
cout << << i << setw(3) << Numbers[i] << endl;
}
//(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] = 21)
do bPlayerDraw[pNum] = false;
if (iPlayerCount[pNum] > 18 - rand()%3)
do 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] = true
cCardDeck[suit][card] = ’X’
cardStr=cardValue[card]+suitValue[suit]
//display column aligned card$
iPlayerCount += cardValue[card]
cout << “Hold” << endl;
//endfor
//endfor
cout<<“ Final: "<//for pNum = 0 to iNumberOfPlayers
//Display column aligned iPlayerCount[pNum]
int pNum = 0;
int iplayercount[pNum] = {Dealer, Player1, Player2, Player3, Player4};
for (int i=0; i < pNum; i++)
{
cout << << i << setw(3) << Numbers[i] << endl;
}
endfor
//(Find Highest Count <= 21)
//for pNum = 0 to iNumOfPlayers
if (iPlayerCount[pNum] !=21 AND > iHighestCount)
then iHighestCount = iPlayerCount[pNum]
//endfor
//(All Players whose count = iHighestCount "Win!")
if (iHighestCount=iPlayerCount[pNum]
cout << "Win!"<< endl;
//(All other Players "Lose"
if (iHighestCount < iPlayerCount[pNum]
cout << "Lose" << endl
//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’
Explanation / Answer
please rate - thanks
try 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[5];
int i,k, m,done,over21;
bool play=true;
srand(time(0));
//srand(5);
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)
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 ";
for(k=0;k<=num;k++)
final(score[k],k,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;
string card[]={"2","3","4","5","6","7","8","9","10","jack","queen","king","ace"};
int code[]={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)code[j]<<" "<<card[k]<<", ";
cout<<s<<" ";
if(n==w)
cout<<"Win! ";
else
cout<<"Lose ";
}
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;
}
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++;
// cout<<i<<" "<<s[i]<<" "<<j<<" "<<k<<endl;
}
}
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
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.