Blackjack Game Implementation
VerifiedAdded on 2019/10/16
|16
|3992
|288
Practical Assignment
AI Summary
This assignment details the implementation of a Blackjack card game in C++. It begins with an explanation of the game's rules and mechanics, clarifying the win/lose conditions and player actions (hit or stand). The document then lists the major variables used in the program, including a `deck` structure to hold card information (name, type, value, dealt status), and variables to track player and computer totals. The core of the assignment is the provided C++ source code, which includes functions for loading the deck, shuffling cards, handling player input, and determining the winner. The code demonstrates object-oriented programming principles with a `play_cards` class. Finally, the assignment mentions screenshots (not included in this text) that would visually demonstrate the game's functionality and output.

1. Name and date
2. A short explanation of the game and how it works
Blackjack is also known as twenty-one. It is a game played between the
computer and the player.
In this game, two cards are given to each player and dealer randomly.
Player's both cards have face up and computer's only one card has face up.
Player can win if the sum of its cards is 21(perfect blackjack), or if the sum
of his cards is less than 21 and greater than computer's, or if the sum of
computer's cards is greater than 21.
Player will lose if the sum of his cards is greater than 21 or if the sum is less
than computer's cards' sum. Now the player has two options – hit or stand.
Hit- Take another undealt card from the deck.
Stand- Do not take any card and let the computer play its turn.
If the sum of player's two cards is quite less than 21(usually less than 15 or
16), he can hit and get another card to make the sum greater and the player
can hit as many times as he wants until the sum is greater than 21.
If the sum of computer's first two cards is less than 17, computer too will get
another card until the sum exceeds 17.The value of ace depends on the
player, he can take it 1 or 11 depending upon the need.
3. List of all major variables used in the program and what they
are used for
● play_cards value – object of class play_cards to invoke the play function
that controls the flow of the game.
● int p_total – integer representing the sum of card values of the player.
● int c_total – integer representing the sum of card values of the computer.
● deck p_cards[5] – array of structure deck to contain the card information
of the cards of the player.
● deck c_cards[5] - array of structure deck to contain the card information of
the cards of the computer.
● deck shuf[52] - array of structure deck to contain the card information
initially loaded in order to shuffle the cards in function shuff.
● struct deck
2. A short explanation of the game and how it works
Blackjack is also known as twenty-one. It is a game played between the
computer and the player.
In this game, two cards are given to each player and dealer randomly.
Player's both cards have face up and computer's only one card has face up.
Player can win if the sum of its cards is 21(perfect blackjack), or if the sum
of his cards is less than 21 and greater than computer's, or if the sum of
computer's cards is greater than 21.
Player will lose if the sum of his cards is greater than 21 or if the sum is less
than computer's cards' sum. Now the player has two options – hit or stand.
Hit- Take another undealt card from the deck.
Stand- Do not take any card and let the computer play its turn.
If the sum of player's two cards is quite less than 21(usually less than 15 or
16), he can hit and get another card to make the sum greater and the player
can hit as many times as he wants until the sum is greater than 21.
If the sum of computer's first two cards is less than 17, computer too will get
another card until the sum exceeds 17.The value of ace depends on the
player, he can take it 1 or 11 depending upon the need.
3. List of all major variables used in the program and what they
are used for
● play_cards value – object of class play_cards to invoke the play function
that controls the flow of the game.
● int p_total – integer representing the sum of card values of the player.
● int c_total – integer representing the sum of card values of the computer.
● deck p_cards[5] – array of structure deck to contain the card information
of the cards of the player.
● deck c_cards[5] - array of structure deck to contain the card information of
the cards of the computer.
● deck shuf[52] - array of structure deck to contain the card information
initially loaded in order to shuffle the cards in function shuff.
● struct deck
Paraphrase This Document
Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser

{
string card_name;
string card_type;
int card_value;
bool dealt;
}; - structure containing the information of any card.
● deck card[52] – array of structure deck to load the card information
initially.
4. Source Code
#include <iostream>
#include <string>
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
using namespace std;
class play_cards
{
private:
struct deck
{
string card_name;
string card_type;
int card_value;
bool dealt;
};
public:
void load();
void printdeck();
int shuff(deck cards[]);
int convert_jkq(int a);
void print(deck num);
int play(void);
deck card[52];
int i;
};
int main()
{
play_cards value;
value.load();
value.printdeck();
system("pause");
char again;
string card_name;
string card_type;
int card_value;
bool dealt;
}; - structure containing the information of any card.
● deck card[52] – array of structure deck to load the card information
initially.
4. Source Code
#include <iostream>
#include <string>
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
using namespace std;
class play_cards
{
private:
struct deck
{
string card_name;
string card_type;
int card_value;
bool dealt;
};
public:
void load();
void printdeck();
int shuff(deck cards[]);
int convert_jkq(int a);
void print(deck num);
int play(void);
deck card[52];
int i;
};
int main()
{
play_cards value;
value.load();
value.printdeck();
system("pause");
char again;

do
{
value.play();
cout<<"\nWould you like to play again? Input 'y' or 'n':\n";
do{
again = getchar();
} while (again!='y' && again!='n');
if (again == 'y')
{
cout<<"\nOK, let's go again!\n\n";
}
}
while(again == 'y');
return 0;
}
void play_cards::load()
{
card[0].card_name="TWO";
card[0].card_type="heart";
card[0].card_value=2;
card[0].dealt=false;
card[1].card_name="THREE";
card[1].card_type="heart";
card[1].card_value=3;
card[1].dealt=false;
card[2].card_name="FOUR";
card[2].card_type="heart";
card[2].card_value=4;
card[2].dealt=false;
card[3].card_name="FIVE";
card[3].card_type="heart";
card[3].card_value=5;
card[3].dealt=false;
card[4].card_name="SIX";
card[4].card_type="heart";
card[4].card_value=6;
card[4].dealt=false;
card[5].card_name="SEVEN";
card[5].card_type="heart";
card[5].card_value=7;
card[5].dealt=false;
card[6].card_name="EIGHT";
card[6].card_type="heart";
card[6].card_value=8;
card[6].dealt=false;
card[7].card_name="NINE";
{
value.play();
cout<<"\nWould you like to play again? Input 'y' or 'n':\n";
do{
again = getchar();
} while (again!='y' && again!='n');
if (again == 'y')
{
cout<<"\nOK, let's go again!\n\n";
}
}
while(again == 'y');
return 0;
}
void play_cards::load()
{
card[0].card_name="TWO";
card[0].card_type="heart";
card[0].card_value=2;
card[0].dealt=false;
card[1].card_name="THREE";
card[1].card_type="heart";
card[1].card_value=3;
card[1].dealt=false;
card[2].card_name="FOUR";
card[2].card_type="heart";
card[2].card_value=4;
card[2].dealt=false;
card[3].card_name="FIVE";
card[3].card_type="heart";
card[3].card_value=5;
card[3].dealt=false;
card[4].card_name="SIX";
card[4].card_type="heart";
card[4].card_value=6;
card[4].dealt=false;
card[5].card_name="SEVEN";
card[5].card_type="heart";
card[5].card_value=7;
card[5].dealt=false;
card[6].card_name="EIGHT";
card[6].card_type="heart";
card[6].card_value=8;
card[6].dealt=false;
card[7].card_name="NINE";
⊘ This is a preview!⊘
Do you want full access?
Subscribe today to unlock all pages.

Trusted by 1+ million students worldwide

card[7].card_type="heart";
card[7].card_value=9;
card[7].dealt=false;
card[8].card_name="TEN";
card[8].card_type="heart";
card[8].card_value=10;
card[8].dealt=false;
card[9].card_name="ACE";
card[9].card_type="heart";
card[9].card_value=1;
card[9].dealt=false;
card[10].card_name="JACK";
card[10].card_type="heart";
card[10].card_value=10;
card[10].dealt=false;
card[11].card_name="QUEEN";
card[11].card_type="heart";
card[11].card_value=10;
card[11].dealt=false;
card[12].card_name="KING";
card[12].card_type="heart";
card[12].card_value=10;
card[12].dealt=false;
card[13].card_name="TWO";
card[13].card_type="diamond";
card[13].card_value=2;
card[13].dealt=false;
card[14].card_name="THREE";
card[14].card_type="diamond";
card[14].card_value=3;
card[14].dealt=false;
card[15].card_name="FOUR";
card[15].card_type="diamond";
card[15].card_value=4;
card[15].dealt=false;
card[16].card_name="FIVE";
card[16].card_type="diamond";
card[16].card_value=5;
card[16].dealt=false;
card[17].card_name="SIX";
card[17].card_type="diamond";
card[17].card_value=6;
card[17].dealt=false;
card[18].card_name="SEVEN";
card[18].card_type="diamond";
card[18].card_value=7;
card[7].card_value=9;
card[7].dealt=false;
card[8].card_name="TEN";
card[8].card_type="heart";
card[8].card_value=10;
card[8].dealt=false;
card[9].card_name="ACE";
card[9].card_type="heart";
card[9].card_value=1;
card[9].dealt=false;
card[10].card_name="JACK";
card[10].card_type="heart";
card[10].card_value=10;
card[10].dealt=false;
card[11].card_name="QUEEN";
card[11].card_type="heart";
card[11].card_value=10;
card[11].dealt=false;
card[12].card_name="KING";
card[12].card_type="heart";
card[12].card_value=10;
card[12].dealt=false;
card[13].card_name="TWO";
card[13].card_type="diamond";
card[13].card_value=2;
card[13].dealt=false;
card[14].card_name="THREE";
card[14].card_type="diamond";
card[14].card_value=3;
card[14].dealt=false;
card[15].card_name="FOUR";
card[15].card_type="diamond";
card[15].card_value=4;
card[15].dealt=false;
card[16].card_name="FIVE";
card[16].card_type="diamond";
card[16].card_value=5;
card[16].dealt=false;
card[17].card_name="SIX";
card[17].card_type="diamond";
card[17].card_value=6;
card[17].dealt=false;
card[18].card_name="SEVEN";
card[18].card_type="diamond";
card[18].card_value=7;
Paraphrase This Document
Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser

card[18].dealt=false;
card[19].card_name="EIGHT";
card[19].card_type="diamond";
card[19].card_value=8;
card[19].dealt=false;
card[20].card_name="NINE";
card[20].card_type="diamond";
card[20].card_value=9;
card[20].dealt=false;
card[21].card_name="TEN";
card[21].card_type="diamond";
card[21].card_value=10;
card[21].dealt=false;
card[22].card_name="ACE";
card[22].card_type="diamond";
card[22].card_value=1;
card[22].dealt=false;
card[23].card_name="JACK";
card[23].card_type="diamond";
card[23].card_value=10;
card[23].dealt=false;
card[24].card_name="QUEEN";
card[24].card_type="diamond";
card[24].card_value=10;
card[24].dealt=false;
card[25].card_name="KING";
card[25].card_type="diamond";
card[25].card_value=10;
card[25].dealt=false;
card[26].card_name="TWO";
card[26].card_type="spade";
card[26].card_value=2;
card[26].dealt=false;
card[27].card_name="THREE";
card[27].card_type="spade";
card[27].card_value=3;
card[27].dealt=false;
card[28].card_name="FOUR";
card[28].card_type="spade";
card[28].card_value=4;
card[28].dealt=false;
card[29].card_name="FIVE";
card[29].card_type="spade";
card[29].card_value=5;
card[29].dealt=false;
card[30].card_name="SIX";
card[19].card_name="EIGHT";
card[19].card_type="diamond";
card[19].card_value=8;
card[19].dealt=false;
card[20].card_name="NINE";
card[20].card_type="diamond";
card[20].card_value=9;
card[20].dealt=false;
card[21].card_name="TEN";
card[21].card_type="diamond";
card[21].card_value=10;
card[21].dealt=false;
card[22].card_name="ACE";
card[22].card_type="diamond";
card[22].card_value=1;
card[22].dealt=false;
card[23].card_name="JACK";
card[23].card_type="diamond";
card[23].card_value=10;
card[23].dealt=false;
card[24].card_name="QUEEN";
card[24].card_type="diamond";
card[24].card_value=10;
card[24].dealt=false;
card[25].card_name="KING";
card[25].card_type="diamond";
card[25].card_value=10;
card[25].dealt=false;
card[26].card_name="TWO";
card[26].card_type="spade";
card[26].card_value=2;
card[26].dealt=false;
card[27].card_name="THREE";
card[27].card_type="spade";
card[27].card_value=3;
card[27].dealt=false;
card[28].card_name="FOUR";
card[28].card_type="spade";
card[28].card_value=4;
card[28].dealt=false;
card[29].card_name="FIVE";
card[29].card_type="spade";
card[29].card_value=5;
card[29].dealt=false;
card[30].card_name="SIX";

card[30].card_type="spade";
card[30].card_value=6;
card[30].dealt=false;
card[31].card_name="SEVEN";
card[31].card_type="spade";
card[31].card_value=7;
card[31].dealt=false;
card[32].card_name="EIGHT";
card[32].card_type="spade";
card[32].card_value=8;
card[32].dealt=false;
card[33].card_name="NINE";
card[33].card_type="spade";
card[33].card_value=9;
card[33].dealt=false;
card[34].card_name="TEN";
card[34].card_type="spade";
card[34].card_value=10;
card[34].dealt=false;
card[35].card_name="ACE";
card[35].card_type="spade";
card[35].card_value=1;
card[35].dealt=false;
card[36].card_name="JACK";
card[36].card_type="spade";
card[36].card_value=10;
card[36].dealt=false;
card[37].card_name="QUEEN";
card[37].card_type="spade";
card[37].card_value=10;
card[37].dealt=false;
card[38].card_name="KING";
card[38].card_type="spade";
card[38].card_value=10;
card[38].dealt=false;
card[39].card_name="TWO";
card[39].card_type="club";
card[39].card_value=2;
card[39].dealt=false;
card[40].card_name="THREE";
card[40].card_type="club";
card[40].card_value=3;
card[40].dealt=false;
card[41].card_name="FOUR";
card[41].card_type="club";
card[41].card_value=4;
card[30].card_value=6;
card[30].dealt=false;
card[31].card_name="SEVEN";
card[31].card_type="spade";
card[31].card_value=7;
card[31].dealt=false;
card[32].card_name="EIGHT";
card[32].card_type="spade";
card[32].card_value=8;
card[32].dealt=false;
card[33].card_name="NINE";
card[33].card_type="spade";
card[33].card_value=9;
card[33].dealt=false;
card[34].card_name="TEN";
card[34].card_type="spade";
card[34].card_value=10;
card[34].dealt=false;
card[35].card_name="ACE";
card[35].card_type="spade";
card[35].card_value=1;
card[35].dealt=false;
card[36].card_name="JACK";
card[36].card_type="spade";
card[36].card_value=10;
card[36].dealt=false;
card[37].card_name="QUEEN";
card[37].card_type="spade";
card[37].card_value=10;
card[37].dealt=false;
card[38].card_name="KING";
card[38].card_type="spade";
card[38].card_value=10;
card[38].dealt=false;
card[39].card_name="TWO";
card[39].card_type="club";
card[39].card_value=2;
card[39].dealt=false;
card[40].card_name="THREE";
card[40].card_type="club";
card[40].card_value=3;
card[40].dealt=false;
card[41].card_name="FOUR";
card[41].card_type="club";
card[41].card_value=4;
⊘ This is a preview!⊘
Do you want full access?
Subscribe today to unlock all pages.

Trusted by 1+ million students worldwide

card[41].dealt=false;
card[42].card_name="FIVE";
card[42].card_type="club";
card[42].card_value=5;
card[42].dealt=false;
card[43].card_name="SIX";
card[43].card_type="club";
card[43].card_value=6;
card[43].dealt=false;
card[44].card_name="SEVEN";
card[44].card_type="club";
card[44].card_value=7;
card[44].dealt=false;
card[45].card_name="EIGHT";
card[45].card_type="club";
card[45].card_value=8;
card[45].dealt=false;
card[46].card_name="NINE";
card[46].card_type="club";
card[46].card_value=9;
card[46].dealt=false;
card[47].card_name="TEN";
card[47].card_type="club";
card[47].card_value=10;
card[47].dealt=false;
card[48].card_name="ACE";
card[48].card_type="club";
card[48].card_value=1;
card[48].dealt=false;
card[49].card_name="JACK";
card[49].card_type="club";
card[49].card_value=10;
card[49].dealt=false;
card[50].card_name="QUEEN";
card[50].card_type="club";
card[50].card_value=10;
card[50].dealt=false;
card[51].card_name="KING";
card[51].card_type="club";
card[51].card_value=10;
card[51].dealt=false;
}
void play_cards::printdeck()
{
//loop to print all cards
for (i=0; i<=51; ++i)
card[42].card_name="FIVE";
card[42].card_type="club";
card[42].card_value=5;
card[42].dealt=false;
card[43].card_name="SIX";
card[43].card_type="club";
card[43].card_value=6;
card[43].dealt=false;
card[44].card_name="SEVEN";
card[44].card_type="club";
card[44].card_value=7;
card[44].dealt=false;
card[45].card_name="EIGHT";
card[45].card_type="club";
card[45].card_value=8;
card[45].dealt=false;
card[46].card_name="NINE";
card[46].card_type="club";
card[46].card_value=9;
card[46].dealt=false;
card[47].card_name="TEN";
card[47].card_type="club";
card[47].card_value=10;
card[47].dealt=false;
card[48].card_name="ACE";
card[48].card_type="club";
card[48].card_value=1;
card[48].dealt=false;
card[49].card_name="JACK";
card[49].card_type="club";
card[49].card_value=10;
card[49].dealt=false;
card[50].card_name="QUEEN";
card[50].card_type="club";
card[50].card_value=10;
card[50].dealt=false;
card[51].card_name="KING";
card[51].card_type="club";
card[51].card_value=10;
card[51].dealt=false;
}
void play_cards::printdeck()
{
//loop to print all cards
for (i=0; i<=51; ++i)
Paraphrase This Document
Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser

{
if((i==13)||(i==26)||(i==39))
cout<<endl;
cout<<card[i].card_name<<" "<<card[i].card_type<<"
”<<card[i].card_value<<" " <<card[i].dealt<<endl;
}//for loop
}
int play_cards::shuff(deck cards[])
{
deck shuf[52];
for(int i=0;i<52;i++)
{
shuf[i] = cards[i];
}
for(int i=0;i<52;i++)
{
cards[rand() % 52] = shuf[i];
}
return 0;
}
void play_cards::print(deck num)
{
cout<<"*******\n";
cout<< num.card_name <<endl;
cout<< num.card_type <<endl;
cout<< num.card_value <<endl;
}
int play_cards::play(void)
{
int i;
int p_total=0;
int c_total=0;
deck p_cards[5];
deck c_cards[5];
char go_on;
char d;
cout<<"Welcome to BlackJack!\n\nEnjoy! Press Enter to go on......\n\n\n";
do{
go_on = getchar();
} while (go_on != '\n');
cout<<"\n";
//shuff the cards
shuff(card);
if((i==13)||(i==26)||(i==39))
cout<<endl;
cout<<card[i].card_name<<" "<<card[i].card_type<<"
”<<card[i].card_value<<" " <<card[i].dealt<<endl;
}//for loop
}
int play_cards::shuff(deck cards[])
{
deck shuf[52];
for(int i=0;i<52;i++)
{
shuf[i] = cards[i];
}
for(int i=0;i<52;i++)
{
cards[rand() % 52] = shuf[i];
}
return 0;
}
void play_cards::print(deck num)
{
cout<<"*******\n";
cout<< num.card_name <<endl;
cout<< num.card_type <<endl;
cout<< num.card_value <<endl;
}
int play_cards::play(void)
{
int i;
int p_total=0;
int c_total=0;
deck p_cards[5];
deck c_cards[5];
char go_on;
char d;
cout<<"Welcome to BlackJack!\n\nEnjoy! Press Enter to go on......\n\n\n";
do{
go_on = getchar();
} while (go_on != '\n');
cout<<"\n";
//shuff the cards
shuff(card);

//give the cards
p_cards[0]=card[0];
p_cards[1]=card[1];
c_cards[0]=card[2];
c_cards[1]=card[3];
card[0].dealt = true;
card[1].dealt = true;
card[2].dealt = true;
card[3].dealt = true;
//the 2 cards player get
cout<<"One of computer's cards:\n";
print(c_cards[0]);
cout<<"\n";
cout<<"Cards of player:\n";
print(p_cards[0]);
cout<<"\n";
print(p_cards[1]);
cout<<"\n";
i=0;
for (i=0; i<2; i++)
{
if (p_cards[i].card_value%100 == 1)
{
cout<<"choose A value of the card "<<i+1<<", input 'y' for 11 or 'n' for
1 :\n" ;
do{
d = getchar();
} while (d!='y' && d!='n');
if (d == 'y')
{
cout<<"You've chosen value 11 for card A.\n";
p_total = p_total + 11;
}
else if(d == 'n')
{
cout<<"You've chosen value 1 for card A.\n";
p_total = p_total +1;
}
}
p_total= p_total + p_cards[i].card_value%100;
if (p_total > 21)
{
cout<<"Sum of player's cards now:"<<p_total<<"\n\n";
p_cards[0]=card[0];
p_cards[1]=card[1];
c_cards[0]=card[2];
c_cards[1]=card[3];
card[0].dealt = true;
card[1].dealt = true;
card[2].dealt = true;
card[3].dealt = true;
//the 2 cards player get
cout<<"One of computer's cards:\n";
print(c_cards[0]);
cout<<"\n";
cout<<"Cards of player:\n";
print(p_cards[0]);
cout<<"\n";
print(p_cards[1]);
cout<<"\n";
i=0;
for (i=0; i<2; i++)
{
if (p_cards[i].card_value%100 == 1)
{
cout<<"choose A value of the card "<<i+1<<", input 'y' for 11 or 'n' for
1 :\n" ;
do{
d = getchar();
} while (d!='y' && d!='n');
if (d == 'y')
{
cout<<"You've chosen value 11 for card A.\n";
p_total = p_total + 11;
}
else if(d == 'n')
{
cout<<"You've chosen value 1 for card A.\n";
p_total = p_total +1;
}
}
p_total= p_total + p_cards[i].card_value%100;
if (p_total > 21)
{
cout<<"Sum of player's cards now:"<<p_total<<"\n\n";
⊘ This is a preview!⊘
Do you want full access?
Subscribe today to unlock all pages.

Trusted by 1+ million students worldwide

cout<<"Computer win!\n";
return 1;
}
else if (p_total == 21)
{
cout<<"Sum of player's cards now:"<<p_total<<"\n\n";
cout<<"Player win!\n";
return 0;
}
}
cout<<"Sum of player's cards now:"<<p_total<<"\n\n";
//whether player get another cards
i=0;
for (i=0; i<3; i++)
{
char j = 'n';
cout<<"Want to hit or stand? Input y for hit or n for stand:\n";
do{
j = getchar();
} while (j!='y' &&j!='n');
if (j=='y')
{
cout<<"You've got another card now.\n";
p_cards[i+2]=card[i+4];
card[i+4].dealt = true;
cout<<"and your card "<<i+3<<" is:\n";
print(p_cards[i+2]);
if (p_cards[i+2].card_value%100 == 1)
{
cout<<"Choose A value of the card"<< i+3<<", input 'y' for 11 or
'n' for 1:\n";
do{
d = getchar();
} while (d!='y' && d!='n');
if (d == 'y')
{
cout<<"You've chosen value 11 for card A.\n";
p_total = p_total+ 11;
}
else if(d == 'n')
{
cout<<"You've chosen value 1 for card A.\n";
p_total = p_total +1;
return 1;
}
else if (p_total == 21)
{
cout<<"Sum of player's cards now:"<<p_total<<"\n\n";
cout<<"Player win!\n";
return 0;
}
}
cout<<"Sum of player's cards now:"<<p_total<<"\n\n";
//whether player get another cards
i=0;
for (i=0; i<3; i++)
{
char j = 'n';
cout<<"Want to hit or stand? Input y for hit or n for stand:\n";
do{
j = getchar();
} while (j!='y' &&j!='n');
if (j=='y')
{
cout<<"You've got another card now.\n";
p_cards[i+2]=card[i+4];
card[i+4].dealt = true;
cout<<"and your card "<<i+3<<" is:\n";
print(p_cards[i+2]);
if (p_cards[i+2].card_value%100 == 1)
{
cout<<"Choose A value of the card"<< i+3<<", input 'y' for 11 or
'n' for 1:\n";
do{
d = getchar();
} while (d!='y' && d!='n');
if (d == 'y')
{
cout<<"You've chosen value 11 for card A.\n";
p_total = p_total+ 11;
}
else if(d == 'n')
{
cout<<"You've chosen value 1 for card A.\n";
p_total = p_total +1;
Paraphrase This Document
Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser

}
}
p_total = p_total + p_cards[i+2].card_value%100;
if (p_total > 21)
{
cout<<"Sum of player's cards now:"<<p_total<<"\n\n";
cout<<"Computer win!\n";
return 1;
}
else if (p_total == 21)
{
cout<<"Sum of player's cards now:"<<p_total<<"\n\n";
cout<<"Player win!\n";
return 0;
}
else cout<<"Sum of player's cards now:"<<p_total<<"\n\n";
}
else
{
cout<<"Sum of player's cards now:"<<p_total<<"\n\n";
break;
}
}
if (i == 3)
{
cout<<"Player win! Because the sum of your 5 cards is no larger than 21! So
lucky!\n";
return 0;
}
//the 2 cards of boss/computer
//i=0;
cout<<"Computer's cards:\n";
print(c_cards[0]);
print(c_cards[1]);
if (c_cards[0].card_value%100 + c_cards[1].card_value%100 == 2)
{
c_total=12; //two A cards
cout<<"Sum of computer's cards now:"<<c_total<<"\n\n";
}
else if ((c_cards[0].card_value)%100 + (c_cards[1].card_value)%100 ==1)//
{
c_total=21;
cout<<"Sum of computer's cards now:"<<c_total<<"\n\n";
cout<<"Computer win!\n";
return 1;
}
p_total = p_total + p_cards[i+2].card_value%100;
if (p_total > 21)
{
cout<<"Sum of player's cards now:"<<p_total<<"\n\n";
cout<<"Computer win!\n";
return 1;
}
else if (p_total == 21)
{
cout<<"Sum of player's cards now:"<<p_total<<"\n\n";
cout<<"Player win!\n";
return 0;
}
else cout<<"Sum of player's cards now:"<<p_total<<"\n\n";
}
else
{
cout<<"Sum of player's cards now:"<<p_total<<"\n\n";
break;
}
}
if (i == 3)
{
cout<<"Player win! Because the sum of your 5 cards is no larger than 21! So
lucky!\n";
return 0;
}
//the 2 cards of boss/computer
//i=0;
cout<<"Computer's cards:\n";
print(c_cards[0]);
print(c_cards[1]);
if (c_cards[0].card_value%100 + c_cards[1].card_value%100 == 2)
{
c_total=12; //two A cards
cout<<"Sum of computer's cards now:"<<c_total<<"\n\n";
}
else if ((c_cards[0].card_value)%100 + (c_cards[1].card_value)%100 ==1)//
{
c_total=21;
cout<<"Sum of computer's cards now:"<<c_total<<"\n\n";
cout<<"Computer win!\n";
return 1;

}
else if (c_cards[0].card_value%100==1 || c_cards[1].card_value%100==1)
{
c_total=(c_cards[0].card_value+c_cards[1].card_value)%100+(rand()%2)*10;
cout<<"Sum of computer's cards now:"<<c_total<<"\n\n";
}
else
{
c_total = (c_cards[0].card_value)%100 + (c_cards[1].card_value)%100;//
cout<<"Sum of computer's cards now:"<<c_total<<"\n\n";
}
//whether computer get another cards until c_total>16
//i=0;
for (i=0; i<3 && c_total<17; i++)
{
c_cards[i+2]=card[i+7];
cout<<"Computer's card "<<i+3<< " is:\n";
print(c_cards[i+2]);
if (c_cards[i+2].card_value%100 == 1)
{
if (c_total+11 <= 21)
{
cout<<"Computer has chosen A as 11\n";
c_total = c_total+11;
cout<<"Sum of computer's cards now:"<<c_total<<"\n\n";
}
else
{
cout<<"Computer has chosen A as 1\n";
c_total = c_total+1;
cout<<"Sum of computer's cards now:"<<c_total<<"\n\n";
}
}
else
{
c_total = c_total + (c_cards[i+2].card_value)%100;//
cout<<"Sum of computer's cards now:"<<c_total<<"\n\n";
}
}
if (i == 3)
{
cout<<"Computer win! Because the sum of its 5 cards is no larger than 21! So
lucky!\n";
return 1;
}
else if (c_cards[0].card_value%100==1 || c_cards[1].card_value%100==1)
{
c_total=(c_cards[0].card_value+c_cards[1].card_value)%100+(rand()%2)*10;
cout<<"Sum of computer's cards now:"<<c_total<<"\n\n";
}
else
{
c_total = (c_cards[0].card_value)%100 + (c_cards[1].card_value)%100;//
cout<<"Sum of computer's cards now:"<<c_total<<"\n\n";
}
//whether computer get another cards until c_total>16
//i=0;
for (i=0; i<3 && c_total<17; i++)
{
c_cards[i+2]=card[i+7];
cout<<"Computer's card "<<i+3<< " is:\n";
print(c_cards[i+2]);
if (c_cards[i+2].card_value%100 == 1)
{
if (c_total+11 <= 21)
{
cout<<"Computer has chosen A as 11\n";
c_total = c_total+11;
cout<<"Sum of computer's cards now:"<<c_total<<"\n\n";
}
else
{
cout<<"Computer has chosen A as 1\n";
c_total = c_total+1;
cout<<"Sum of computer's cards now:"<<c_total<<"\n\n";
}
}
else
{
c_total = c_total + (c_cards[i+2].card_value)%100;//
cout<<"Sum of computer's cards now:"<<c_total<<"\n\n";
}
}
if (i == 3)
{
cout<<"Computer win! Because the sum of its 5 cards is no larger than 21! So
lucky!\n";
return 1;
}
⊘ This is a preview!⊘
Do you want full access?
Subscribe today to unlock all pages.

Trusted by 1+ million students worldwide
1 out of 16
Your All-in-One AI-Powered Toolkit for Academic Success.
+13062052269
info@desklib.com
Available 24*7 on WhatsApp / Email
Unlock your academic potential
Copyright © 2020–2026 A2Z Services. All Rights Reserved. Developed and managed by ZUCOL.
