1. Engineering
  2. Computer Science
  3. array3cpp include ltiostreamgt include ltstringgt include ltctimegt using namespace std...

Question: array3cpp include ltiostreamgt include ltstringgt include ltctimegt using namespace std...

Question details
Array3.cpp

#include <iostream>
#include <string>
#include <ctime>
using namespace std;

int main()
{
        string suit[4] = {"Hearts","Clubs","Spades","Diamonds"};
        string value[13] = {"Ace","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King"};
        int Sval, Cval;
        int dealtSuit[52];
        int dealtValue[52];
        int x, y;
        bool valid;
        char var;
        
        srand((unsigned)time(0));  // "Seed" the random number generator
                
        cout << "This program deals a deck of cards.\n";
        
        for (x = 0; x < 52; x++)
        {
                var = ' ';
                while (var != 'y')
                {
                        cout << "Enter 'y' to deal a card and 'q' to quit: ";
                        cin  >> var;
                        if (var == 'q')
                                return 0;
                }
        
                valid = false;
                while (!valid)
                {
                        Sval = rand() % 4;
                        Cval = rand() % 13;
                        if (x > 0)
                        {
                                valid = true;
                                for (y = 0; y < x; y++)
                                {
                                        if ((dealtSuit[y] == Sval)&&(dealtValue[y] == Cval))
                                                valid = false;
                                }
                        }
                        else
                                valid = true;
                }
                dealtSuit[x] = Sval;
                dealtValue[x] = Cval;
                cout << "Your card is the " << value[Cval] << " of " << suit[Sval] << "\n";
        }
        
        cout << "Out of cards!\n";
        return 0;
}

Modify Arrays3.cpp so that the program plays Blackjack with a user. Make a basic menu system (review your 155 examples).

When run the program, you should see ONE card the computer has and one card that you have, and you should be able to HIT (get a new card) or STAY (keep your current cards and let the computer deal). If you do not BUST, after you stay the computer will try to beat your score. If either you or the computer get 21 total, that is an automatic win. If either busts, the other automatically wins. I recommend you move the main functionality of my code to a function called “deal(),” and then this makes the main a lot shorter and easier to follow. Note that only the value really matters with regard to the score… the suit is just to ensure that each value only occurs four times per deck.

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution