Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++: In the game of blackjack, if the player scores higher than the dealer or th

ID: 3787943 • Letter: C

Question

C++: In the game of blackjack, if the player scores higher than the dealer or the dealer goes over 21, and the player does not go over 21, then he wins his bet (in addition to keeping his original wager). If he scores 21 or less, and the same as the dealer, the hand is a push, and he keeps his bet. Otherwise, if he scores lower than the dealer, or goes over 21 (regardless of whether or not the dealer also does so), then he loses his bet.

A "blackjack" is a special kind of 21 point hand that beats all other hands. In such a case, the player wins 1.5 times his original bet. If both dealer and player have a blackjack, then the hand is a push. Note that if the dealer has a blackjack, and the player has a 21 (but does not have blackjack), then the dealer wins, and the player loses his wager.

You are given an bet indicating the players wager, an dealer indicating the dealer's score, and an player indicating the player's score. Finally, you are given an blackjack, which will be equal to 1 if the player has a blackjack, or 0 otherwise. Likewise, you are given the dealerBlackjack.

You are to output the amount of money the player wins or loses on the hand.

Explanation / Answer

CODE:

#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>

using namespace std;

// Blackjack class
class Blackjack {

   public:
       // bet is the bet value
       // dealer is the dealer value.
       // dbj is the dealerBlackjack value.
       // player is the player value
       // bj is the blackjack value.
       int winnings(int bet, int dealer, int dbj, int player, int bj) {
           // checking all the conditions and returning the suitable bet.
           if(bj==1 && dbj==0)
               return bet*3/2;
           if(bj==1 && dbj==1)
               return 0;
           if(bj==0 && dbj==1)
               return -bet;
           if(player==dealer && player <=21)
               return 0;
           if(player<=21 && (dealer>21 || dealer<player))
               return bet;

           // if none of the conditions match return negative bet value.
           return -bet;
       }

};

int main() {
   int bet = 0;
   int dealer = 0;
   int dealerBlackjack = 0;
   int player = 0;
   int blackjack = 0;

   cout << "Enter the bet indicating the players wager" << endl;
   cin >> bet;

   cout << "Enter the dealer's score" << endl;
   cin >> dealer;

   cout << "Enter the players score" << endl;
   cin >> player;

   cout << "Enter if dealer has blackjack (1 for yes and 0 for no)" << endl;
   cin >> dealerBlackjack;

   cout << "Enter if player has blackjack (1 for yes and 0 for no)" << endl;
   cin >> blackjack;

   Blackjack bj;
   int totalAmount = bj.winnings(bet,dealer,dealerBlackjack,player,blackjack);

   cout << "Amount left with the player is: " << endl;
   cout << totalAmount << endl;
}

OUTPUT:
$ g++ blackwinner.cpp
$ ./a.out

Enter the bet indicating the players wager
10
Enter the dealer's score
20
Enter the players score
21
Enter if dealer has blackjack (1 for yes and 0 for no)
0
Enter if player has blackjack (1 for yes and 0 for no)
0

Amount left with the player is:
10

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote