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

I am trying to correct the resetting variables for this game. Some times it work

ID: 3675147 • Letter: I

Question

I am trying to correct the resetting variables for this game. Some times it works and sometimes it does not. Any ideas.

When doing the print out for the following: Should I put this all in a function or at the beginning. When I print the second card for player it shifts up between player card and computer card. Which I know is wrong.

b. Deal card to player //print console message
c. Deal card to computer //print console message
d. Deal card to player //print console message
e. Deal card to computer (hidden) //print x instead of # and suit
f. Display results //Print cards and show the totaled results

// This is our standard input/output library
#include<stdio.h>
#include<stdlib.h>
#include<time.h>

//Defining
typedef struct {
char cSuit;
int nVal;
} Card;

//Function Prototypes
void printCard(int cardNumber, char cSuit); //Prints card number and suit
void dealCard(Card deck[], int *index, Card *card); //Access to function in MAIN
void welcomeMenu(void);
void hitMenu(void);
void showMenu(void);
void exitMessage(void);
void hitMessage(void);

//Global Variables

int main(void)
{
welcomeMenu(); //Function that prints menu for game
int menuChoice = -1;
int nCardValue = 2; //Prints card value
int playerCardsCount = 0; //Inital count set to zero
int compCardsCount = 0; //Inital count set to zero
int index = 0;
int hitMenu; //Print hit menu
int nComputerTotal = 0;
int nPlayerTotal = 0;
char cSuit = 3; //Prints suit symbol
Card Deck[52]; //Assign an array for Card Deck
Card playerCards[5]; //Assign an array of 5 cards to player
Card computerCards[5]; //Assign an array of 5 cards to computer

srand(time(NULL)); // seed random number generator from time
////////////////////////////////////////////////////////////////////////////////////////////////////////   
for (int i = 0; i < 52; i++)
{
  Deck[i].nVal = nCardValue;
  Deck[i].cSuit = cSuit;
  nCardValue++;
  if (nCardValue > 14)
  {
   nCardValue = 2;
   cSuit++;
  }
}
////////////////////////////////////////////////////////////////////////////////////////////////////////   
//for loop to randomize the selection of cards a maxium of 299 times
//for loop first shuffles up to 199 and adds another 100 more
for (int i = 0; i < ((rand() % 200) + 100); i++)
{
  int nFirst = (rand() % 52);
  int nSecond = nFirst;
  while (nSecond == nFirst)
  {
   nSecond = (rand() % 52);
  }
  Card Temp;

  //assigning temp cards
  Temp.nVal = Deck[nFirst].nVal;
  Temp.cSuit = Deck[nFirst].cSuit;

  //assign second card to first
  Deck[nFirst].nVal = Deck[nSecond].nVal;
  Deck[nFirst].cSuit = Deck[nSecond].cSuit;

  //assign second card to temp position
  Deck[nSecond].nVal = Temp.nVal;
  Deck[nSecond].cSuit = Temp.cSuit;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////   
while (menuChoice != 0)
{
  showMenu();
  scanf("   %d", &menuChoice);
  if (menuChoice == 1) //Input from player
  {
   //Calling the function dealCard and feeding in the Deck into the function
   //Then passing in the address of index from the pointer of function
   //Then call in the address of card a from the pointer
   //Player
   dealCard(Deck, &index, &playerCards[playerCardsCount]); //Sub 0
   playerCardsCount++; //update playersCard count
   dealCard(Deck, &index, &playerCards[playerCardsCount]); //Sub 1
   playerCardsCount++; //update playersCard count
   //Computer
   printf(" ");
   dealCard(Deck, &index, &computerCards[compCardsCount]); //Sub 0
   compCardsCount++; //update computerCard count
   printf(" ");
   /*dealCard(Deck, &index, &computerCards[compCardsCount]);   //Sub 1
      compCardsCount++;    //update computerCard count */

////////////////////////////////////////////////////////////////////////////////////////////////////////   
   // print cards for how many "playerCardsCount" is active in playerCards[] array
   for (int i = 0; i < playerCardsCount; i++)
   {
    //print card playerCards[0]
    printCard(playerCards[i].nVal, playerCards[i].cSuit);

    // accumulate playerCards total
    if (playerCards[i].nVal > 10 && playerCards[i].nVal < 14)
    {
     nPlayerTotal += 10;
    }
    else if (playerCards[i].nVal == 14)
    {
     if ((nPlayerTotal + 11) > 21)
      nPlayerTotal += 1;
     else
      nPlayerTotal += 11;
    }

    nPlayerTotal += playerCards[i].nVal;
    printf("Player Card's total is %d ", nPlayerTotal);
   }
////////////////////////////////////////////////////////////////////////////////////////////////////////           
   // print cards for how many "compterCardsCount" is active in computerCards[] array
   for (int i = 0; i < compCardsCount; i++)
   {
    //print card playerCards[0]
    printCard(computerCards[i].nVal, computerCards[i].cSuit);

    // accumulate playerCards total
    if (computerCards[i].nVal > 10 && computerCards[i].nVal < 14)
    {
     nComputerTotal += 10;
    }
    else if (computerCards[i].nVal == 14)
    {
     if ((nComputerTotal + 11) > 21)
      nComputerTotal += 1;
     else
      nComputerTotal += 11;
    }

    nComputerTotal += computerCards[i].nVal;
    printf("Computer Card's total is %d ", nComputerTotal);
   }
////////////////////////////////////////////////////////////////////////////////////////////////////////   
   printf(" ");
   while (playerCardsCount < 5)
   {
    hitMessage();
    scanf("%d", &hitMenu);
    if (hitMenu == 1)
    {
     dealCard(Deck, &index, &playerCards[playerCardsCount]);
     playerCardsCount++;
     nPlayerTotal = 0;
     for (int i = 0; i < playerCardsCount; i++)
     {
      //print card playerCards[i]
      printCard(playerCards[i].nVal, playerCards[i].cSuit);
      //printf("Player Card #%d is a %d%c ", (i + 1), playerCards[i].nVal, playerCards[i].cSuit);

      // accumulate playerCards total, not considering 11 | 1 aces yet
      if (playerCards[i].nVal > 10 && playerCards[i].nVal < 14)
      {
       nPlayerTotal += 10;
      }
      else if (playerCards[i].nVal == 14)
      {
       if ((nPlayerTotal + 11) > 21)
        nPlayerTotal += 1;
       else
        nPlayerTotal += 11;
      }

      nPlayerTotal += playerCards[i].nVal;
      printf("Player Card's total is %d ", nPlayerTotal);

     }
    }
    else if (hitMenu == 2)
     break;
   } // end of while, "hit loop"
////////////////////////////////////////////////////////////////////////////////////////////////////////   
   // "Hit" loop is over and print results

   // print player cards

   // print total for player

   // print computer cards

   // print total for computer

   // Game over?
   //////////////////////////////////////////////////////////////////////////////

   //Menu for player
   showMenu();
   scanf("   %d", &menuChoice);
   //*
   if (menuChoice == 1)
   {
    // reset all variables for game
    nPlayerTotal = 0;
    playerCardsCount = 0;
    nComputerTotal = 0;
    compCardsCount = 0;

    for (int i = 0; i < 5; i++)
    {
     playerCards[0].nVal = 2;
     playerCards[1].nVal = 2;
     playerCards[2].nVal = 2;
     playerCards[3].nVal = 2;
     playerCards[4].nVal = 2;
     computerCards[0].nVal = 2;
     computerCards[1].nVal = 2;
     computerCards[2].nVal = 2;
     computerCards[3].nVal = 2;
     computerCards[4].nVal = 2;

    }
   } //*/

   else // don't want to play again
   {
    exitMessage();
    break; // original menu break
   }

   ///////////////////////////////////////////////////////////////////////////////
  }
  else
   exitMessage();
} // end of menu

return 0;
}
/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////Functions//////////////////////////////////////////

//Function welcomeMenu
void welcomeMenu(void)
{
printf("     ************BLACKJACK************ ");
printf("            **********PLEASE CHOOSE********** ");
}
//Function for dealCard
void dealCard(Card deck[], int *index, Card *card)
{
int spot = *index;
(*card).nVal = deck[spot].nVal; //temp value of card = the spot in the deck
(*card).cSuit = deck[spot].cSuit; //temp suit = the spot in deck
(*index)++;
} //End of Function

//Function for int cardNumber & char suit
void printCard(int cardNumber, char cSuit)
{
if (cardNumber <= 10)
{
  printf(" ----------- ");
  printf("|%c         | ", cSuit);
  printf("|          | ");
  printf("|          | ");
  printf("|    %d     | ", cardNumber);
  printf("|          | ");
  printf("|          | ");
  printf("|         %c| ", cSuit);
  printf(" ----------- ");
  //printf("Player card#: %d is a %c ", cardNumber, cSuit); // Can not store 2 digit in a char variable
}
else
{
  if (cardNumber == 11) // If nCardValue is equal to 11 change to letter
   printf("Player card#: J of %c ", cSuit); // Changes cSuit to Jack
  if (cardNumber == 12)
   printf("Player card#: Q of %c ", cSuit); // Changes cSuit to Queen
  if (cardNumber == 13)
   printf("Player card#: K of %c ", cSuit); // Changes cSuit to King
  if (cardNumber == 14)
   printf("Player card#: A of %c ", cSuit); // Changes cSuit to Ace
}
} //End of function

//Showmenu
void showMenu(void)
{
printf(" ");
printf("     --------------------------------- ");
printf("        |   A. 1- Play BlackJack    | "); //Choice 1
printf("        |   B. 0- Exit Program      | "); //Choice 2
printf("     --------------------------------- ");
} //End of function

//Exit Message
void exitMessage(void)
{
printf("Thank you for playing Blackjack!!! ");
} //End of function

//Reset all Varaiables
void resetVariables(void)
{
} //End of function

//Hit message
void hitMessage(void)
{
printf("Would you like to HIT!?! Enter 1 for yes and 2 for no. ");
}

/////////////////////////////////////////////////////////////////////////
////////////////Working on/////////////////////////////////////

Explanation / Answer

put all the code in beginning

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