This isn\'t for a homework assignment as I\'ve already turned in my assignment b
ID: 3556425 • Letter: T
Question
This isn't for a homework assignment as I've already turned in my assignment but I would appreiate it if someone could help me understand how this code works. Basically if you could go line by line and help explain it to me. Now I know what a lot of the code means but it'll be easier if someone could just explain all of it to me. This is our first lesson in doing something like 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;
}
}
Explanation / Answer
#include <iostream>
#include <iomanip>
#include <ctime>
#include <string>
#include<cstdlib>
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(int(time(0)));
srand(6);
num = getPlayers();
while (num >= 1) //because no of players should be greater than 1.
{
initCards(CardDeck, round, dealer, playing, done); //to initialize the
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)//if s[i]<21
{
w = i; // put i in w
i = num + 3; //exit for loop
}
for (i = 0; i <= num; i++)
if (s[i]>s[w] && s[i] <= 21) //if score of i is greater than score of w and score of i is less than 22
w = i; //ut new i in w
if (s[w]>21) //if score of w is less than 21
return 10; //return 10
return w; //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;//randomly select a card
l = rand() % 4;//randomly select a suit
} while (CardDeck[l][k] != ' '); //if CardDeck[l][k] is not equal to space
CardDeck[l][k] = n; //make it to n which is between 0 to number of players
}
void scoreCards(char c[4][13], int s[], int n, int& over) //to calculate score
{
int i, j, k, aces = 0; //aces is certain value
over = 0;
for (i = 0; i <= n; i++)
{
aces = 0;
s[i] = 0;
for (j = 0; j<4; j++) //for each suit
for (k = 0; k<13; k++) //for each card
{
if (c[j][k] == i) //if i is selected card
{
if (k<9) //if no of that card is less than 9
s[i] += (k + 2); // give it score "no+2"
else if (k<12) //if between 9 and 12
s[i] += 10; // give it score "no+10"
else //otherwise
{
aces++; //increase aces
}
}
}
if (aces>0) //enter if aces is greater than 0
{
if (aces>1) // if aces is greater than 1
s[i] += aces; //new score is " score + aces"
else //otherwise
{
if (s[i] + 11>21) //" score+11" is greater than 21
s[i]++; //increase score by 1
else
s[i] += 11; //increase prev score by 11
}
}
if (s[i]>21) //if score>21
over++; //increase score by 1
}
}
void initCards(char CardDeck[][13], int& round, int& dealer, bool p[], int& done) //to initialize the CardDeck
{
int i, k, m;
round = 0;
done = 0;
dealer = rand() % 4; //randomly selects a suit
for (k = 0; k<4; k++)
for (m = 0; m<13; m++)
CardDeck[k][m] = ' '; //initialize every card with space ' '
for (i = 0; i<5; i++)
p[i] = true; //initializes playiing[] to true
}
int getPlayers() //to get number of players
{
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; //input no of players
while (num<1 || num>4) //check their range
{
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; //if not in range, re- enter no of players
}
return num;
}
void deal(int players, char CardDeck[4][13])
{
int i, j, k, l;
for (i = 0; i <= players; i++) //for each player
for (j = 0; j<2; j++) //for every move
{
do
{
k = rand() % 13; //randomly selects a card
l = rand() % 4; //randomly selects a suit
} while (CardDeck[l][k] != ' '); //if cardDeck is empty, exit the loop
CardDeck[l][k] = i; // give that particular CardDeck value i.
}
}
void printcards(int players, char CardDeck[4][13]) //for printing the card's details
{
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.