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

C++ Can\'t seem to make this program work right... Need to draw cards based on s

ID: 2988446 • Letter: C

Question

C++

Can't seem to make this program work right... Need to draw cards based on score, but it seems to only draw cards at random or not at all when it probably should.

Any help is appreciated. Pretty sure the problem lies in the function hitStay

#include<iostream>
#include<iomanip>
#include<windows.h>
#include<string>
#include<array>

using namespace std;

//Declare Constants
const int NUMBER_OF_ROWS = 4;
const int NUMBER_OF_COLUMNS = 13;

//Function Headers.
int numberOfPlayers();
void initCardDeck(char [][NUMBER_OF_COLUMNS], int NUMBER_OF_ROWS);
void passCardDeck(int, char[][NUMBER_OF_COLUMNS], int NUMBER_OF_ROWS);
void scoreHand(int [],int, char [][NUMBER_OF_COLUMNS], int NUMBER_OF_ROWS);
void displayCards(int, char[][NUMBER_OF_COLUMNS], int NUMBER_OF_ROWS); //not there yet...
void hitStay(int, char[][NUMBER_OF_COLUMNS], int NUMBER_OF_ROWS, int[]);
void finalScore(int, int[]);

void main(void)

{
   bool bPlayerDraw[5];   //Boolean to determine if player holds (F) or draws card (T)
   char cCardDeck[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS];    //Character array representing the card deck
   //initialized with spaces, I can see ?.
   int iCard;       //Card array index
   //0 = 2 card
   //1 = 3 card
   //2 = 4 card
   //3 = 5 card
   //4 = 6 card
   //5 = 7 card
   //6 = 8 card
   //7 = 9 card
   //8 = 10 card
   //9 = jack card
   //10 = queen card
   //11 = king card
   //12 = ace card
   int iSuit;           //Suit array index
   //0 = diamonds
   //1 = hearts
   //2 = clubs
   //3 = spades
   // ASCII character display reference for display card suit symbols
   //3 = heart symbol
   //4 = diamond symbol
   //5 = club symbol
   //6 = spade symbol
   int   iNumberOfDraws = 0; //Number of rounds of card draws
   int iNumberOfPlayers = 0;//Number of players in current game
   int iPlayerCount[5];   //Integer array to holder each player's count
   //iPlayer[0] is always the dealer
   int iHighestCount = 0;   //Highest count for a single game
   int k, m;       //integer loop counters
   srand(GetTickCount());   //Seed the random-number generator
   bool bContPlay;
   char cPlay;   //Character variable for play game inputchar cPlay;
   //Main Body.
   bool bDealerBlackjack;

   while (bContPlay = true)
   {
       cout << "Welcome to Dave's Crooked Blackjack Table "
           << "Glad to have you back! " << endl << endl;

       iNumberOfPlayers = numberOfPlayers();   //function gets number of players and assigns that
       //number to iNumberOfPlayers.
       initCardDeck(cCardDeck, NUMBER_OF_ROWS); //initialize cCardDeck array of ' '
       passCardDeck(iNumberOfPlayers, cCardDeck, NUMBER_OF_ROWS); //passes num of players to assign card deck location.
       displayCards(iNumberOfPlayers, cCardDeck, NUMBER_OF_ROWS);
       scoreHand(iPlayerCount, iNumberOfPlayers, cCardDeck, NUMBER_OF_ROWS);
       hitStay(iNumberOfPlayers, cCardDeck, NUMBER_OF_ROWS, iPlayerCount);
       displayCards(iNumberOfPlayers, cCardDeck, NUMBER_OF_ROWS);
       scoreHand(iPlayerCount, iNumberOfPlayers, cCardDeck, NUMBER_OF_ROWS);
       finalScore(iNumberOfPlayers, iPlayerCount);

       cout << "Hope you had fun, would you like to try your luck again?" << endl;
       cout << "Press "Y" for yes or "N" for no" << endl;
       cin >> cPlay;
       if (toupper(cPlay) == 'Y')
       {
           bContPlay == true;
       }

       else
       {
           bContPlay == false;

           exit(0);

       }
   }
}

//working functionnumberOfPlayers  
int numberOfPlayers() // Working function to ensure 1-4 players.
{
   int num;

       cout << "Enter the number of players in the game. ";
       cout << "There must be at least one player but no more than four. ";
       cout << "Number of players: ";
       cin >> num;

       while (num < 1 || num > 4)
       {
           cout << "You suck at this... try again! ";
           cout << "There must be at least one player but no more than four. ";
           cout << "How many players in the game (1-4)? ";
           cin >> num;
       }
       return num;
          
}

//initCardDeck initialized cCardDeck to all ' '.
void initCardDeck(char cCardDeck[][NUMBER_OF_COLUMNS], int noOfRows) //function passes to main as it did when body
//was in main! Good declaration and execute
{
   //this function body worked outside the function, don't touch!!!!
   int i, j;
  
   for (i = 0; i < 4; i++)
   {
       for (j = 0; j < 13; j++)
       {
           cCardDeck[i][j] = ' ';
       }
   }
}

void passCardDeck(int iNumberOfPlayers, char cCardDeck[][NUMBER_OF_COLUMNS], int noOfRows)

//first round.

{
   //Need to pass iPlayer number and matrix array location to function to store card held here.
   int i, j, k, l;
   char c;
   for (i = 0; i <= iNumberOfPlayers; i++)
   {
       for (j = 0; j < 2; j++) //variable for multipledraws?
       {
           do
           {
               k = rand() % 13;
               l = rand() % 4;
           }

           while (cCardDeck[l][k] != ' ');
           c = i + '0'; //converts matrix location of i (player) to c (character).
           cCardDeck[l][k] = c;
       }
   }
}

//Working, output is still off from the example, but at least scoring is happening.
void scoreHand(int iPC[], int num, char cCardDeck[][NUMBER_OF_COLUMNS], int NUMBER_OF_ROWS)
{
   int i, j, k, aces = 0;
   for (i = 0; i <= num; i++)
   {
       iPC[i] = 0;
       for (j = 0; j < 4; j++)

       for (k = 0; k < 13; k++)
       {
           int c = i + '0';
           if (cCardDeck[j][k] == c) //I think its looking for a int but there are chars here...
           if (k < 9) //should make less than 10 value of cards 2-9.
               iPC[i] += (k + 2);
           else if (k < 12) //should make 10-K worth 10.
               iPC[i] += 10;
           else
           if (iPC[i] + 11 > 21) //fixes fun times with Aces...
               iPC[i]++;
           else
               iPC[i] += 11;
       }
       if (i == 0)
       {
           if (iPC[i] == 21)
           {
               cout << "Dealer has 21. Sorry folks..." << endl;
               break;
           }

           else
           {
               cout << "Dealer's score is " << iPC[i] << endl;
           }
       }

           if (i >= 1)
           {
               if (iPC[i] == 21)
                   cout << "Player " << i << " has 21!!!" << endl;
               else
                   cout << "Player " << i << "'s score is " << iPC[i] << endl;
              
           }
       }
}

//working for 2 cards...
void displayCards(int iNumberOfPlayers, char cCardDeck[][NUMBER_OF_COLUMNS], int NUMBER_OF_ROWS)
//work in progress... definitely having problems... AFU comes to mind...
{
   int i, j, k;
   int c;
   string card[] = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A" };
   int suit[] = { 4, 3, 5, 6 };

   for (i = 0; i <= iNumberOfPlayers; i++)
   {
       if (i == 0)
           cout << "Dealer's hand is: ";
       else
           cout << " Player " << i << "'s hand is: ";
       for (j = 0; j < 4; j++)
       for (k = 0; k < 13; k++)
       {
           c = i + '0';  
           if (cCardDeck[j][k] == c)
           cout << card[k] << (char)suit[j] << " ";
       }

       cout << endl;
   }
}

void hitStay(int iNumberOfPlayers, char cCardDeck[][NUMBER_OF_COLUMNS], int NUMBER_OF_ROWS, int iPlayerCount[])

//for rounds 3 and up.

{
   {
       //Need to pass iPlayer number and matrix array location to function to store card held here.
       int i, j, k, l;
       char c;
       for (i = 0; i <= iNumberOfPlayers; i++)
       {
           //Dealer loop.
           if (i == 0)
           {  
               if (iPlayerCount[i] >= 17)
                   break;
               else if (iPlayerCount[i] < 17);
               {
                   for (j = 0; j < 1; j++) //variable for multipledraws?
                   {
                       do
                       {
                           k = rand() % 13;
                           l = rand() % 4;
                       }

                       while (cCardDeck[l][k] != ' ');
                       c = i + '0'; //converts matrix location of i (player) to c (character).
                       cCardDeck[l][k] = c;
                   }
               }
           }
           //Player loop.
           if (i >= 1)
           {
               if ( iPlayerCount[i] <= 15)

               {
                  
                   for (j = 0; j < 1; j++) //variable for multipledraws?
                   {
                       do
                       {
                           k = rand() % 13;
                           l = rand() % 4;
                       }

                       while (cCardDeck[l][k] != ' ');
                       c = i + '0'; //converts matrix location of i (player) to c (character).
                       cCardDeck[l][k] = c;
                   }
               }
           }
       }
   }
}

void finalScore(int iNumberOfPlayers, int iPlayerCount[])
{
   int i;
   for (i = 0; i <= iNumberOfPlayers; i++)
   {
  
   if (i == 0)
   {
       if (iPlayerCount[i] == 21)
       {
           cout << "Dealer Wins, sorry folks..." << endl;
           break;
       }

       if (iPlayerCount[i] > 21)
           cout << "Dealer Busts" << endl;
   }
   if (i >= 1)
       {
           if (iPlayerCount[i] > 21)
               cout << "Player " << i << " Busts. Sorry bout that..." << endl;
           else if ((iPlayerCount[i] < iPlayerCount[0]) && iPlayerCount[0] <= 21)
               cout << "Dealer beats Player " << i << endl;
           else if (iPlayerCount[i] >= iPlayerCount[0])
               cout << "Player " << i << " WINS!" << endl;
       }
   }
}

Explanation / Answer

Please Rate

Thank You