C programming BlackJack game simplified This program simulat
**C++ programming**: BlackJack game (simplified)
This program simulates the following simplified black jack game. Every card must be drawn randomly. Initially this program will draw two cards for you and the dealer from a fresh deck. The program will turn two cards face up for you and turn one card face up and turn one card face down(hidden card) for dealer. Whenever the sum of the displayed cards number is less than 21, you can decide whether you will have one more card or not. The game continues until you decide to stop to have a card or your sum is over 21. After that, if you have not busted, the dealer must have a card until the sum of the cards is 17 or more. You win by not busting and having a total higher than the dealers. The dealer loses by busting or having a lesser total than yours Ifyou and the dealer have the same total, then TIE. If both you and the dealer bust, then the dealer win. This assignment has to use a class inheritance.Solution
#include <iostream>
#include <ctime>
using namespace std;
// These are the various global Variables //
char *thecard_number[] = {\"2\",\"3\",\"4\",\"5\",\"6\",\"7\",\"8\",\"9\",\"10\",\"Jack\",\"Queen\",\"King\",\"Ace\"};
char *thecard_type[] = {\"Clubs\",\"Diamonds\",\"Spades\",\"Hearts\"};
struct thecardtype {
int color;
int number;
int value;
};
struct thedecktype {
bool deck[52];
int size;
};
// These are the various functions //
void SeedRandom() {
time_t tTime;
time(&tTime);
srand(tTime);
rand();
}
struct thecardtype letsPickACard(struct decktype &thedeck) {
struct thecardtype givencard;
int randnum = (int) (1.0 * rand() / (RAND_MAX + 1) * thedeck.size);
int counter = 0;
int i;
for (i=0; i<52; ++i) {
if (thedeck.thedeck[i] == true) {
++counter;
}
if (counter == randnum+1) {
break;
}
}
--thedeck.size;
thedeck.thedeck[i] = false;
givencard.number = i / 4;
givencard.color = i % 4;
if (givencard.number == 10 || givencard.number == 11) {
givencard.value = 10;
} else if (givencard.number == 9) {
givencard.value = 1;
} else if (givencard.number == 12) {
givencard.value = 11;
} else {
givencard.value = givencard.number + 2;
}
return givencard;
}
struct thedecktype createDeckInit() {
struct thedecktype givendeck;
givendeck.size = 52;
for (int i=0; i<52; ++i) {
givendeck.givendeck[i] = true;
}
return givendeck;
}
char *TextCard(struct thecardtype &pickcard) {
char *string = (char *) malloc (22 * sizeof(char));
sprintf(string,\"%s of %s (%d)\",pickcard_num[pickcard.number],pickcard_type[pickcard.color],pickcard.value);
return string;
}
// Main //
void main() {
SeedRandom();
bool checkIsGameOver = false;
char userWantCard;
int giventotalvalue = 0;
cout << \"Welcome to the game of blackjack!\" << endl;
struct thedecktype deckone = createDeckInit();
while (!checkIsGameOver) {
bool ValidChoice = false;
while (!ValidChoice) {
cout << \"please tell that do you want a card? (Y)es / (N)o\" << endl;
cin >> userWantCard;
if (userWantCard == \'Y\') {
struct thecardtype card = letsPickACard(deck);
giventotalvalue += card.value;
cout << \"This is to tell you that received a \" << TextCard(card) << \",with a current total value at \" << giventotalvalue << \".\" << endl;
if (giventotalvalue > 21) {
cout << \"Sorry, you have exceeded 21 points and thus lost!\" << endl;
checkIsGameOver = true;
}
if (giventotalvalue == 21) {
cout << \"WOW Congratulations, you have won!\" << endl;
checkIsGameOver = true;
}
ValidChoice = true;
} else if (WantCard == \'N\') {
cout << \"BRVO there isn\'t an opponent, you won :)\" << endl;
ValidChoice = true;
checkIsGameOver = true;
} else {
cout << \"This is a very invalid choice, please try again\";
}
}
}
cout << \"The game is over”<< endl;
cin.ignore();
cin.get();
}
</ctime></iostream>




