Create Blackjack in c++ for multiple players with 5 decks. This is what i have s
ID: 3635965 • Letter: C
Question
Create Blackjack in c++ for multiple players with 5 decks. This is what i have so far, i'm not sure where to go at this point once the player already has 2 cards. any sort of help beyond that point would be very helpful. I have to use arrays for this project and i'm not quite sure how to use multiple decks with multiple players.#include <iostream>
#include <ctime>
int main
{
int deck[4][14] //Initialize Deck Array
[int Player[11] // initialize maximum amount of cards player can have
int playertotal //Total Value of Player's cards
int dealer[11]
bool (hit)= true
for (suit=0,suit<3, suit++) //Setup Array for Deck
{
for (face=0, face<12, face++)
{
suit=rand()%4
face=rand()%12
deck[suit][face] =face} //Gives each card a number
}
for (int x=0; x<1; x++) //DEAL 2 CARDS
{rand%()suit;
rand%()face;
if (deck[suit][face] =!0)
{Player[x]=deck[suit][face]
playertotal=playertotal+deck[suit][face]
deck [suit][face]=0; //card selected becomes a 0 in the array so it wont repeat
}
else { x=x-1;} //if card chosen is a zero, loop goes back to top
}
// Player currently has 2 cards
//not sure what to do after this point in terms of using bool statements to give the player the option to add cards----------------------------------------------------
//while (hit)
//if (playertotal=21)
//cout<<"blackjack"
//else
//cout<<"Your current hand value is"<<playertotal<<"Would you like to hit or stay?"<<endl;
//cin>> stay
//return 0;
Explanation / Answer
#include <iostream>
#include <ctime>
using namespace std;
enum hands {PLAYER_HAND = 0, DEALER_HAND};
void showResult (hands h, int theHand);
void whoWins (int playerHand, int houseHand);
int main()
{
int deck[4][13]; //Initialize Deck Array
int Player[11]; // initialize maximum amount of cards player can have
int playertotal = 0; //Total Value of Player's cards
int dealer[11];
int dealertotal = 0;
bool hit= true;
int face, suit;
hands hand;
for (suit=0;suit<4; suit++) //Setup Array for Deck
{
for (face=0; face<13; face++)
deck[suit][face] = face; //Gives each card a number
}
for (suit=0;suit<4; suit++) //Setup Array for Deck
{
for (face=0; face<13; face++)
{
int s = rand()%4;
int f = rand()%13;
int randomCard = deck[s][f];
int tmpCard = deck[suit][face];
deck[suit][face] = randomCard;
deck[s][f] = tmpCard;
}
}
char choise;
hand = PLAYER_HAND;
while(true)
{
do
{
if(hand == PLAYER_HAND)
cout << " Player: " << endl;
else
cout << " Dealer: " << endl;
for (int x=0; x<=1; x++) //DEAL 2 CARDS
{
suit = rand()%4;
face = rand()%13;
if (deck[suit][face] != -1)
{
Player[x] = deck[suit][face];
if(hand == PLAYER_HAND)
playertotal = playertotal + deck[suit][face];
else
dealertotal = dealertotal + deck[suit][face];
deck [suit][face] = -1; //card selected becomes a 0 in the array so it wont repeat
cout << "Card " << (x+1) << ": " << Player[x] << endl;
}
else { x=x-1;} //if card chosen is a zero, loop goes back to top
}
if(hand == PLAYER_HAND)
cout<<"Your current hand value is: "<< playertotal <<endl;
else
cout<<"Your current hand value is: "<< dealertotal <<endl;
if(hand == PLAYER_HAND)
{
showResult(hand, playertotal);
hand = DEALER_HAND;
}
else
{
showResult(hand, dealertotal);
hand = PLAYER_HAND;
}
cout << " Would you like to hit(h) or stay(s)?"<<endl;
cin >> choise;
}while(choise == 'h' || choise =='H');
}
whoWins(playertotal, dealertotal);
return 0;
}
void showResult (hands h, int theHand)
{
if (theHand > 21)
{
if (h == PLAYER_HAND)
cout << "Player Busted!" << endl;
else
cout << "House Busted!" << endl;
exit(0);
}
else
{
if (h == PLAYER_HAND)
cout << "Player Holds on "<< theHand << endl;
else
cout << "House Holds on " << theHand << endl;
}
}
void whoWins (int playerHand, int houseHand)
{
if (playerHand > houseHand)
cout << "Player Wins! " << endl;
else
cout << "House Wins! " << endl;
}