Thread: Resetting my vairables for blackjack and printing order.

  1. #1
    Registered User
    Join Date
    Feb 2016
    Location
    Middleburg, Florida
    Posts
    3

    Resetting my vairables for blackjack and printing order.

    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.

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

    Thank you!

    Code:
    // 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("\n");
       dealCard(Deck, &index, &computerCards[compCardsCount]); //Sub 0
       compCardsCount++; //update computerCard count
       printf("\n");
       /*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\n", 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\n", 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 card playerCards[i]
          printCard(playerCards[i].nVal, playerCards[i].cSuit);
          //printf("Player Card #%d is a %d%c\n", (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\n", 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**********\n\n");
    }
    //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(" -----------\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 %c\n", 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\n", cSuit); // Changes cSuit to Jack
      if (cardNumber == 12)
       printf("Player card#: Q of %c\n", cSuit); // Changes cSuit to Queen
      if (cardNumber == 13)
       printf("Player card#: K of %c\n", cSuit); // Changes cSuit to King
      if (cardNumber == 14)
       printf("Player card#: A of %c\n", 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!!!\n\n");
    } //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.\n");
    }
    
    
    /////////////////////////////////////////////////////////////////////////
    ////////////////Working on/////////////////////////////////////

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,337
    You need to break up your main function into smaller functions that do one thing and do it well. This will also make it easier to "reset" your variables.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,508
    Factoring your code into functions will definitely be a big help. But do you understand the basic logic of what you're writing?

    For example, one thing I noticed:

    Code:
    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; 
    }
    This does the same thing each time, so any looping is useless.

    If you wanted this loop to have meaning, you might actually use the changing value of "i", maybe in in this way:

    Code:
    for (int i = 0; i < 5; i++)
    {
         playerCards[i].nVal = 2;
         computerCards[i].nVal = 2;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stdout printing out of order?
    By cpjust in forum C Programming
    Replies: 7
    Last Post: 10-30-2007, 11:39 AM
  2. Reverse order printing
    By JFonseka in forum C Programming
    Replies: 1
    Last Post: 08-19-2007, 06:47 AM
  3. Printing in reverse order
    By JFonseka in forum C Programming
    Replies: 1
    Last Post: 08-17-2007, 04:30 AM
  4. Printing out in wrong order
    By DarkDot in forum C++ Programming
    Replies: 1
    Last Post: 05-05-2007, 02:56 PM
  5. why private/protected vairables?
    By MadHatter in forum C++ Programming
    Replies: 9
    Last Post: 12-15-2002, 11:25 PM