Trying Correct Resetting Variables Game Times Works Sometimes Ideas Print Following Put F Q11092155

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 allin a function or at the beginning. When I print the second card forplayer it shifts up between player card and computer card. Which Iknow 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 # andsuit
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 numberand suit
void dealCard(Card deck[], int *index, Card *card); //Access tofunction 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 fromtime
////////////////////////////////////////////////////////////////////////////////////////////////////////   
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 299times
//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 inthe Deck into the function
   //Then passing in the address of index from thepointer of function
   //Then call in the address of card a from thepointer
   //Player
   dealCard(Deck, &index,&playerCards[playerCardsCount]); //Sub 0
   playerCardsCount++; //update playersCardcount
   dealCard(Deck, &index,&playerCards[playerCardsCount]); //Sub 1
   playerCardsCount++; //update playersCardcount
   //Computer
   printf(“n”);
   dealCard(Deck, &index,&computerCards[compCardsCount]); //Sub 0
   compCardsCount++; //update computerCardcount
   printf(“n”);
   /*dealCard(Deck, &index,&computerCards[compCardsCount]);   //Sub 1
      compCardsCount++;   //update computerCard count */

////////////////////////////////////////////////////////////////////////////////////////////////////////   
   // print cards for how many “playerCardsCount” isactive 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 %dn”,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 %dn”,nComputerTotal);
   }
////////////////////////////////////////////////////////////////////////////////////////////////////////   
   printf(“n”);
   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 cardplayerCards[i]
      printCard(playerCards[i].nVal,playerCards[i].cSuit);
      //printf(“Player Card #%d is a%d%cn”, (i + 1), playerCards[i].nVal, playerCards[i].cSuit);

      // accumulate playerCardstotal, 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%dn”, 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************n”);
printf(”           **********PLEASE CHOOSE**********nn”);
}
//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 inthe deck
(*card).cSuit = deck[spot].cSuit; //temp suit = the spot indeck
(*index)++;
} //End of Function

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

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

//Exit Message
void exitMessage(void)
{
printf(“Thank you for playing Blackjack!!!nn”);
} //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 forno.n”);
}

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

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *