Need some help here with finishing the rest of this code. Create a new Visual St
ID: 3636542 • Letter: N
Question
Need some help here with finishing the rest of this code.Create a new Visual Studio Win32 Console project named LastName_Lab1 or Lastname_BlackJack, where lastname is your lastname. For Example: Johnson_Lab1 or Nguyen_BlackJack.
Enter the following source, which will set up the 2D array and the recommended variable declarations. It is up to the student to design and implement the remainder of the program code.
// Programmer: (put your name here)
// 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
#include
#include
#include
using namespace std;
void main (void)
{
bool playerHit[5];
char play = 'N';
char CardDeck[4][13];
int playerCount[5];
int highestCount = 0;
int suit, card;
int cNum, pNum;
int numPlayers;
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"};
char suitSymbol[4]={char(4),char(3),char(5),char(6)};
srand(GetTickCount()); //Seed the random-number generator
//Main game loop
//Enter your code here…
}
Here is a sample of the finished program’s output.
COMP220 BlackJack Game
Enter the number of players (1..4) ..... 4
Dealer Player 1 Player 2 Player 3 Player 4
Card 1: 9? 8? Q? 3? J?
Card 2: 7? 3? 4? J? 10?
Card 3: 6? 6? 9? 8? Hold
Card 4: Hold 5? Hold Hold Hold
Card 5: Hold Hold Hold Hold Hold
Final: 22 21 23 21 20
Lose Win! Lose Win! Lose
Play Again y/n? ...
COMP220 BlackJack Game
Enter the number of players (1..4) ..... 4
Dealer Player 1 Player 2 Player 3 Player 4
Card 1: 10? A? Q? Q? 3?
Card 2: 2? 5? 3? 6? K?
Card 3: K? 4? A? 10? K?
Card 4: Hold Hold 2? Hold Hold
Card 5: Hold Hold 9? Hold Hold
Final: 22 20 25 26 23
Lose Win! Lose Lose Lose
Play Again y/n? ...
STEP 3: Program Specifications
The dealer will deal a maximum of 5 cards to any player.
Dealer and other players are all initially dealt 2 cards randomly chosen from the deck.
a. Cards are displayed using 2..10, J,Q,K,A followed by the Suit symbol.
b. Cards must be checked to make sure duplicate cards are not played.
b. Display cards in formatted Columns as shown above.
For each additional card (after the first two), each player ( dealer, player1, player2, etc) will
decide whether to Hit (draw a card) or Stay (take no more cards) based on the formula:
if playerCardCount>(18-rand[0,1 or 2] Then STAY else HIT
In other words, if a player's card count > 18 - (random value 0,1 or 2) Stay, otherwise Hit
Depending on the random value generated, you could be hitting 16, 17 or 18
Once a player holds, he or she should not be asked to draw a card again during the game.
Number cards count their card value and Jack, Queen or King counts 10.
Ace's count 11 unless adding 11 puts the player over 21.
The dealing ends after the fifth card is dealt or when all players decide to stay.
Skip a blank line and display the player card counts in the column for each player.
On the next line, display Lose or Win! for each player. Winners are those players with the high card count <= 21.
There may be more than 1 winner per hand of BlackJack.
The players is then prompted to Play Again y/n? ...
If the response is 'Y' or 'y', repeat the game, any other response quits game
STEP 4: Program Variables
bool playerHit[5]; // indices 0..5 represent Dealer(0) and up to 4 player
char play = 'N'; // Used to hold Play Again repsonse
char cardDeck[4][13]; // [4] are suits 0 = diamonds, 1 = hearts, 2 = clubs, 3 = spades
// [13] are cards 2..10 and J,Q,K,A, No Jokers in this deck
int playerCount[5]; // Keeps track of current cardCounts for Player
int highestCount = 0; // Keeps track of highest count <=21 to see who wins
int suit, card; // Index values for randomly selected suit and card
int cNum, pNum; // Descriptive counter variables used by for loops
int numPlayers; // Selected number of players
string cardStr; // Holds String consisting of 2-character card and 1-character suit symbol
int cardValue[13]={2,3,4,5,6,7,8,9,10,10,10,10,11}; // cardValue Index array
string cardName[13]={" 2"," 3"," 4"," 5"," 6"," 7"," 8", // cardName Index array
" 9","10"," J"," Q"," K"," A"};
char suitSymbol[4]={char(4),char(3),char(5),char(6)}; // suitSymbol Index array
STEP 5: Suggested Pseudo Code
Black is Pseudo code, Red is actual code, Blue is Comments
do
Re-seed Random# Generator with TickCount
Clear Screen
Display User Heading and skip 2 lines
Prompt/Input/Error-Handle (1..4): numPlayers
Increment numPlayers to include Dealer
(The deck is cleared by setting all the values to ' ')
for suit = 0 to 3
for card = 0 12
set cardDeck[suit][card] to ' '
endfor
endfor
(Set Players Count to 0 and Draw to true)
for pNum=0,pNum<5,pNUm++
PlayerCount[pNum] = 0
PlayerHit[pNum] = true
endfor
set highestCount 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 chosen already played, choose another card )
(Mark the deck location with 'X' to indicate card is played)
!!! START THE DEAL !!!
for cNum=0,cNum<5,cNum++
Display “Card ” & draw+1 & ”: ” &
for pNum=0,pNum< to numPlayers
(Get Un-Played Card From Deck)
do
suit = rand()%4;
card = rand()%13;
while cardDeck[suit][card] not ’ ’
(Determine if Player wants to Hit or Stay)
if playerCount[pNum] > 18 - rand() mod 3
playerHit[pNum] = false;
(If Player Hits, mark card in deck as played,
(display card, add card value to Player count)
if PlayerHit[pNum] is true
cardDeck[suit][card] = ’X’
cardStr=cardValue[card]+suitValue[suit]
display column aligned card$
(Test for Ace value = 1 or 11)
if cardValue is 11 & playerCount[pNum]+11 exceeds 21)
playerCount[pNum]+= 1;
else
playerCount[pNum]+= cardValue[card];
end if/else
else
display “Hold”
end if/else
endfor
endfor
(End of Hand, Display Player Count Values
Display "Final:"
for pNum=0,pNum Display column aligned playerCount[pNum]
endfor
skip line
(Find Highest Count <= 21)
for pNum=0,pNum if playerCount[pNum] is not 21 AND > highestCount
HighestCount = PlayerCount[pNum]
endfor
(All Players whose count = highestCount "Win!")
(All other Players "Lose"
for pNum=0,pNum if playerCount[pNum] is highestCount
Display column aligned " Win! ";
else
Display column aligned " Lose ";
endfor
skip 2 lines
Prompt/Input “Play Again Y/N?” : play
while play is ‘Y’ or ‘y’
Explanation / Answer
please rate - thanks
note it sometimes hangs on the srand with getTickcount--when I get rid of that it never hangs
// Programmer: (put your name here)
// 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
#include <iostream>
#include <iomanip>
#include <string>
#include <windows.h>
using namespace std;
void deal(int ,bool [4][13],int [5][5][2],int[],int[]);
void printcards(int,int[5][5][2],int[],int[]);
bool check(int,int,int[5][5][2],int[],int[]);
int main (void)
{char play = 'N';
int playerCount[5];
int numPlayers;
srand(GetTickCount());
bool used[4][13];
int hand[5][5][2];
int score[5];
int k, m; //integer loop counters
do
{
for(k=0;k<4;k++)
for(m=0;m<13;m++)
used[k][m]=false;
cout<<"COMP220 BlackJack Game ";
cout<<"Enter the number of players (1..4) ..... ";
cin>>numPlayers;
while(numPlayers<2||numPlayers>4)
{cout<<"invalid entry ";
cout<<"Enter the number of players (1..4) ..... ";
cin>>numPlayers;
}
for(k=0;k<5;k++)
{playerCount[k]=0;
score[k]=0;
}
deal(numPlayers,used,hand,playerCount,score);
printcards(numPlayers,hand,playerCount,score);
cout<<"Play again y/n? ";
cin>>play;
}while(play=='y'||play=='Y');
return 0;
}
void deal(int players,bool used[4][13],int hand[5][5][2],int cards[5],int score[])
{int i,j,k,l;
bool yes;
for(i=0;i<=players;i++)
for(j=0;j<5;j++)
{
yes=check(i,j,hand,cards,score);
if(j<2)
yes=true;
if(yes)
{
do
{k=rand()%13;
l=rand()%4;
}while(used[l][k]);
hand[i][j][0]=k;
hand[i][j][1]=l;
used[l][k]=true;
cards[i]++;
}
else
j=10; //force go to next person
}
}
bool check(int i,int j,int hand [5][5][2],int cards[],int score[])
{int k,aces=0;
int cardValue[13]={2,3,4,5,6,7,8,9,10,10,10,10,11};
score[i]=0;
for(k=0;k<j;k++)
{if(cardValue[hand[i][k][0]]==11)
aces++;
score[i]=score[i]+cardValue[hand[i][k][0]];
}
while(aces>0)
if(score[i]>21)
{score[i]-=10;
aces--;
}
if(score[i]>21)
return false;
if(score[i]>18-rand()%3)
return false;
return true;
}
void printcards(int players,int hand[5][5][2],int cards[],int score[])
{int i,j,win,winscore;
string cardName[13]={" 2"," 3"," 4"," 5"," 6"," 7"," 8"," 9","10"," J"," Q"," K"," A"};
char suitSymbol[4]={char(4),char(3),char(5),char(6)};
for(i=0;i<=players;i++)
{if(i==0)
cout<<" Dealer ";
else
cout<<"Player "<<i<<" ";
}
cout<<endl;
for(j=0;j<5;j++)
{cout<<"Card "<<j+1<<": ";
{for(i=0;i<=players;i++)
if(j<cards[i])
//cout<<(int)hand[i][j][0]<<" "<<(int)hand[i][j][1]<<endl;
cout<<" "<<cardName[hand[i][j][0]]<<" "<<suitSymbol[hand[i][j][1]]<<" ";
else
cout<<" Hold ";
}
cout<<endl;
}
cout<<" Final ";
for(i=0;i<=players;i++)
cout<<score[i]<<" ";
cout<<endl<<" ";
win=-1;
winscore=-1;
for(i=0;i<=players;i++)
if(score[i]<22&&score[i]>winscore)
{win=i;
winscore=score[i];
}
for(i=0;i<=players;i++)
if(i==win)
cout<<"Win! ";
else
cout<<"Lose ";
cout<<endl<<endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.