Question

In: Computer Science

Use C++ Black Jack Create a program that uses methods and allows the user to play...

Use C++

Black Jack

Create a program that uses methods and allows the user to play the game of blackjack against the computer dealer. Rules of Blackjack to remember include:

1. You need one 52 card deck of cards with cards from  2-Ace (4 cards of each number).

2. Jacks, Queens and Kings count as 10 points.

3. An Ace can be used as either 1 or 11 depending on what the user decides during the hand.

4. Draw randomly two cards for the "dealer" and display one of them while keeping the other hidden. Also, randomly draw two cards for the player and display them in view.

5. Allow the user to hit (randomly draw a card) as many times as they wish. If the player "busts" or gets over 21, the dealer automatically wins the players wager.

6. After the user "stands" or is satisfied with his total, the dealer must take a card if his total is 16 or below, and cannot take a card if his total is 17 or above.

7. If the dealer "busts" or goes over 21, the player wins back his wager plus his wager again. (Example - if a player bets $3 he gets back his original $3 plus an additional $3).

8. If neither the dealer or the player "busts" or goes over 21, then the highest total wins. Ties go to the dealer.

9. Note - the player should start with $20 in the bank and cannot wager more than he currently has in the bank.  

10.  Note - before each round the user can bet in whole dollars how much to wager or how much of the total in the bank to risk.

11. Allow the user to quit at any time. Of course, the user must quit if he/she runs out of money.

12. Remember, somehow ensure that the same card (example - 6 of spades) cannot be drawn twice in a single hand.

Solutions

Expert Solution

PROGRAM:

#include <iostream>

#include <algorithm>

using namespace std;

string mainDeck[52];

string suits[4] = {"C","D","H","S"};

string cards[13] = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};

string Player[52], Dealer[52];

int playerInd=0, dealerInd=0,deckIndex=51;

int playerWager=20;

pair<string, int> Points[13];

void initAndShuffleDeck(){

for(int i=0;i<4;i++){

for(int j=0;j<13;j++){

mainDeck[13*i + j] = cards[j]+suits[i];

}

}

srand(time(0));

random_shuffle(&mainDeck[0],&mainDeck[52]);

}

void printDeck(){

for(int i=0;i<4;i++){

for(int j=0;j<13;j++){

cout << mainDeck[13*i + j] << " ";

}

cout << endl;

}

}

int askWager(){

int currBet;

cout << "How much do you want to bet? (max:" << playerWager << ") ";

cin >> currBet;

while(currBet>playerWager || currBet<0){

cout << "Invalid Bet amount" << endl;

cout << "How much do you want to bet? (max:" << playerWager << ") ";

cin >> currBet;

}

return currBet;

}

int cardToPoint(string card){

card = card.substr(0,card.length()-1);

for(int i=0;i<13;i++){

if(Points[i].first == card){

return Points[i].second;

}

}

}

int getPointsPlayer(){

int sum = 0;

for(int i=0;i<playerInd;i++){

sum += cardToPoint(Player[i]);

}

return sum;

}

int getPointsDealer(){

int sum = 0;

for(int i=0;i<dealerInd;i++){

sum += cardToPoint(Dealer[i]);

}

return sum;

}

void drawCardsFirstTime(){

Player[playerInd++] = mainDeck[deckIndex--];

Player[playerInd++] = mainDeck[deckIndex--];

cout << "Player Cards: " << Player[playerInd-1] << " " << Player[playerInd-2];

Dealer[dealerInd++] = mainDeck[deckIndex--];

cout << "\tDealer Cards: " << Dealer[dealerInd-1] << " |/|" << endl;

Dealer[dealerInd++] = mainDeck[deckIndex--];

}

bool endGame(){

cout << "Current Wager: " << playerWager << endl;

if(playerWager==0){return true;}

cout << "Enter 0 to Quit else 1: ";

char command;

cin >> command;

if(command=='0'){

return true;

}

return false;

}

char getAction(){

cout << "S - Stand" << endl;

cout << "H - Stand" << endl;

cout << "Enter Command: ";

char command;

cin >> command;

if(command=='H' || command=='S'){

return command;

}else{

cout << "Invalid command" << endl;

getAction();

}

}

char onHit(){

Player[playerInd++] = mainDeck[deckIndex--];

cout << "Card Drawn: " << Player[playerInd-1] << endl;

}

void restart(){

playerInd = 0;

dealerInd = 0;

deckIndex = 51;

initAndShuffleDeck();

}

void playGame(){

int currBet;

currBet = askWager();

drawCardsFirstTime();

if(getPointsPlayer()==21){

playerWager += currBet;

if(endGame()){

return;

}

}

while(getPointsPlayer()<=21){

char a = getAction();

if(a=='H'){

onHit();

}

if(a=='S'){

break;

}

}

if(getPointsPlayer()>21){

cout << "Player Bust" << endl;

playerWager -= currBet;

}else{

if(getPointsDealer()<=16){

Dealer[dealerInd++] = mainDeck[deckIndex--];

}

if(getPointsDealer()>21){

cout << "Dealer Bust" << endl;

playerWager += 2*currBet;

}else{

if(getPointsDealer()>=getPointsPlayer()){

cout << "Player Lost" << endl;

playerWager -= currBet;

}else{

cout << "Player Won" << endl;

playerWager += currBet;

}

}

}

if(endGame()){

return;

}else{

restart();

playGame();

}

}

void initPoints(){

Points[0] = make_pair("2",2);

Points[1] = make_pair("3",3);

Points[2] = make_pair("4",4);

Points[3] = make_pair("5",5);

Points[4] = make_pair("6",6);

Points[5] = make_pair("7",7);

Points[6] = make_pair("8",8);

Points[7] = make_pair("9",9);

Points[8] = make_pair("10",10);

Points[9] = make_pair("J",10);

Points[10] = make_pair("Q",10);

Points[11] = make_pair("K",10);

Points[12] = make_pair("A",11);

}

int main(){

initAndShuffleDeck();

initPoints();

playGame();

}

OUTPUT:


Related Solutions

Fat Percentage Calculator Create a C++ program that allows the user to enter the number of...
Fat Percentage Calculator Create a C++ program that allows the user to enter the number of calories and fat grams in a food. The application should display the percentage of the calories that come from fat. If the calories from fat are less than 30% of the total calories of the food, it should also display a message indicating the food is low in fat. One gram of fat has 9 calories, so: Calories from fat = fat grams *...
Create a basic program (C programming language) that accomplishes the following requirements: Allows the user to...
Create a basic program (C programming language) that accomplishes the following requirements: Allows the user to input 2 numbers, a starting number x and ending number y Implements a while loop that counts from x (start) to y(end). Inside the loop, print to the screen the current number Print rather the current number is even or odd If the number is even , multiply by the number by 3 and print the results to the screen. If the number is...
Create a Python program that: Allows the user to enter a phrase or sentence. The program...
Create a Python program that: Allows the user to enter a phrase or sentence. The program should then take the phrase or sentence entered Separate out the individual words entered Each individual word should then be added to a list After all of the words have been place in a list Sort the contents of the list Display the contents of the sorted list with each individual word displayed on a separate line Display a message to the user indicating...
PLEASE DO IN C++ Create an object-oriented program that initially allows the user to save customer...
PLEASE DO IN C++ Create an object-oriented program that initially allows the user to save customer information in the list and to search for a customer by specifying the customer’s ID. Sample Run Customer Information Management System --------------------------------------------------------------- CUSTOMER DATA ENTRY: Full Name (First, Last): Jenny Ha Company: Convergent Laser Technologies Street: 1000 International Ave City: Oakland State: CA Zip Code: 94506 ID: 100 Continue Your Data Entry? (y/n): y Full Name (First, Last): Bill Martinez Company: Cisco Systems Street:...
Write an interactive program in c++ that allows the user toenter 2-15 vectors (use the...
Write an interactive program in c++ that allows the user to enter 2-15 vectors (use the if statement). Display the resultant on the screen.
Program must be in C Write a program that allows the user to enter the last...
Program must be in C Write a program that allows the user to enter the last names of five candidates in a local election and the number of votes received by each candidate in two different arrays. The program should then output each candidate’s name, the number of votes received, and the percentage of the total votes received by the candidate. Your program should also output the winner of the election. Example (Letters and numbers with underscore indicate an input):...
3) Create a Java program that uses NO methods, but use scanner: Write a program where...
3) Create a Java program that uses NO methods, but use scanner: Write a program where you will enter the flying distance from one continent to another, you will take the plane in one country, then you will enter miles per gallon and price of gallon and in the end it will calculate how much gas was spend for that distance in miles. Steps: 1) Prompt user to enter the name of country that you are 2) Declare variable to...
(JAVA) Create a program that creates a mini database of numbers that allows the user to:...
(JAVA) Create a program that creates a mini database of numbers that allows the user to: reset the database, print the database, add a number to the database, find the sum of the elements in the database, or quit. In main, you will declare an array of 10 integers (this is a requirement). Then you will define the following methods: • printArray (int[ ] arr) – this takes in an array and prints it • initArray (int[ ] arr) –...
Use c# Create a program called ResortPrices that prompts the user to enter the number of...
Use c# Create a program called ResortPrices that prompts the user to enter the number of days for a resort stay. Then display the price per night and the total price. Nightly rates are R 500.00 for one or two nights, R 650.00 for three or four nights, R 850.00 for five, six or seven nights, and R 1500.00 for eight nights or more.
Write a program using Python that allows the user to play a guessing game (please provide...
Write a program using Python that allows the user to play a guessing game (please provide a picture of code so it is easier to read). The game will choose a "secret number", a positive integer less than 10000. The user has 10 tries to guess the number. Requirements: Normally, we would have the program select a random number as the "secret number". However, for the purpose of testing your program (as well as grading it), simply use an assignment...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT