Assistance with unknown errors?

This is a discussion on Assistance with unknown errors? within the C++ Programming forums, part of the General Programming Boards category; Having a few errors in my code (not stated by C++). The program attempts to run a blackjack game. One ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    6

    Assistance with unknown errors?

    Having a few errors in my code (not stated by C++).
    The program attempts to run a blackjack game.

    One of the errors is that when an ace is drawn (I believe of any suit but spades), the ace is not displayed, but is still added to the score.

    Another error is that if run continuously, the characters start to change and no longer work (the suit characters).

    Also, when asked for another card or another game, if enter is pressed twice instead of y or n then enter, it skips that process and just says "you win!"

    Sorry I don't have the comments done before each function. Pretty much, the main array has 53 parts, the 52 cards, then a last to tell the program that when it gets there....to re shuffle the array/deck.

    Any help is very much appreciated, thanks.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <time.h>
    
    /* function prototypes */
    void InitializeDeck();
    int RandomNumber(int);
    void ShuffleDeck();
    void DisplayCard(int);
    void DisplayDeck();
    int ScoreCard(int);
    int DrawCard(int[], int);
    void clean();
    #define DECK_SIZE  53
    int main()
    {
    int ppeanuts = 10;
    int cpeanuts = 10;
    int deck[DECK_SIZE];
    int cardcount = 0;
    int scorep;
    int scorec;
    char answer;
    char anotheranswer = 'y';
    printf("Welcome to ULTIMATE BlackJack \n");
    printf("In ULTIMATE BlackJack, Aces are worth 1, and only 1\n");
    printf("Face cards are all worth 10. All other cards are worth their face value \n");
    printf("You and I start with 10 peanuts each. \nThe winner of a round takes one peanut from the loser.\n");
    printf("When one of us runs out, the game ends.\n");
    printf("Good Luck, you'll need it. \n\n\n");
    srand(time(NULL));
    InitializeDeck(deck);
    ShuffleDeck(deck);
                      while (ppeanuts != 0  && cpeanuts != 0 && anotheranswer == 'y')
                            {
                            scorep = 0; scorec = 0;
                            printf("Your Peanuts: %d My Peanuts: %d\n", ppeanuts, cpeanuts);
                            printf("--------------------------------\n\n");
                            printf("Your turn:\n\n");
                            scorep = scorep + DrawCard(deck, cardcount); 
                            printf("Score: %i\n\n", scorep);
                            cardcount++;
                            scorep = scorep + DrawCard(deck, cardcount); 
                            printf("Score: %i\n\n", scorep);
                            cardcount++;
                            printf("Another Card?\n\n");
                            scanf("%c", &answer);
                            clean();
                               while (scorep <= 21 && answer == 'y' )
                               {
                                 scorep = scorep + DrawCard(deck, cardcount),
                                 printf("Score: %i\n\n", scorep);
                                 if(scorep >21) 
                                 break;
                                 cardcount++,
                                 printf("Another Card?\n\n"),
                                 scanf("%c", &answer);
                                 clean();
                                 }
                                 
                                 if (answer == 'n')
                                 {
                                      printf("My turn:\n\n");
                                             while (scorec < 17)
                                             {
                                             scorec = scorec + DrawCard(deck, cardcount),
                                             printf("Score: %i\n\n", scorec),
                                             cardcount++;
                                             }
                                 if (answer != 'n' && answer != 'y')
                                 {
                                 printf("That is not a valid answer\n");
                                 }}
                                 if (scorep > 21)
                                 {
                                 printf("You Bust! I win.\n\n");
                                 ppeanuts --;
                                 cpeanuts ++;
                                 printf("Another Round?");
                                 scanf("%c", &anotheranswer);
                                 clean();
                                 }
                                 if (scorec > 21)
                                 {
                                 printf("I Bust, You Win...This Time\n\n");
                                 ppeanuts++;
                                 cpeanuts--;
                                 printf("Another Round?");
                                 scanf("%c", &anotheranswer);
                                 clean();
                                 }
                                 if (scorep > scorec && scorep < 22 && scorec < 22 )
                                 {
                                 printf("You Win\n\n");
                                 ppeanuts++;
                                 cpeanuts--;
                                 printf("Another Round?");
                                 scanf("%c", &anotheranswer);
                                 clean();
                                 }
                                 if (scorec > scorep && scorec < 22 && scorep < 22)
                                 {
                                 printf("I Win. \n\n");
                                 ppeanuts--;
                                 cpeanuts++;
                                 printf("Another Round?");
                                 scanf("%c", &anotheranswer);
                                 clean();
                                 }
                                 if (scorec == scorep)
                                 {
                                 printf("Push!\n\n");
                                 printf("Another Round?");
                                 scanf("%c", &anotheranswer);
                                 clean();
                                 }}
                                 if (cardcount == deck[52])
                                 {
                                 ShuffleDeck(deck);
                                 }
                                 if (ppeanuts < cpeanuts)
                                 {
                                 printf("Game Over. I win. \n\n");
                                 }
                                 else if (ppeanuts > cpeanuts)
                                 {
                                 printf("Game Over. You Win. \n\n");
                                 }
      system("PAUSE");
      return 0;
    }
    /*********************************************************************
     * FunctionName(Parameter Names) 
     *		Brief, general description of function's purpose -- what does it do?
     * PRE:	 list all preconditions here -- things that must be true for function to work.
     * POST: list all postconditions here -- things that are true when function returns.
     *********************************************************************/
    void clean (void)
    {
         char dummy;
         do 
         {
             scanf("%c",&dummy); 
         } while(dummy != '\n'); 
    }
    
    
    
    /*********************************************************************
     * FunctionName(Parameter Names) 
     *		Brief, general description of function's purpose -- what does it do?
     * PRE:	 list all preconditions here -- things that must be true for function to work.
     * POST: list all postconditions here -- things that are true when function returns.
     *********************************************************************/
    void InitializeDeck(int deck[DECK_SIZE])
         {
            int index;
                for (index = 0; index < DECK_SIZE; index++)
                {
                    deck[index] = index;
                    }
                    }
    /*********************************************************************
     * FunctionName(Parameter Names) 
     *		Brief, general description of function's purpose -- what does it do?
     * PRE:	 list all preconditions here -- things that must be true for function to work.
     * POST: list all postconditions here -- things that are true when function returns.
     *********************************************************************/
    int RandomNumber(int upperlimit)
        {
         int number;
         number = rand()%(upperlimit +1);
         return number;
         }
    /*********************************************************************
     * FunctionName(Parameter Names) 
     *		Brief, general description of function's purpose -- what does it do?
     * PRE:	 list all preconditions here -- things that must be true for function to work.
     * POST: list all postconditions here -- things that are true when function returns.
     *********************************************************************/
    void ShuffleDeck(int deck[DECK_SIZE])
         {
            int rand;
            int index;
            int dummy;
                for(index = 51; index >= 0; index--)
                {
                rand = RandomNumber(index);
                dummy = deck[rand];
                deck[rand] =  deck[index];
                deck[index] = dummy;
                }
                }
    
    /*********************************************************************
     * FunctionName(Parameter Names) 
     *		Brief, general description of function's purpose -- what does it do?
     * PRE:	 list all preconditions here -- things that must be true for function to work.
     * POST: list all postconditions here -- things that are true when function returns.
     *********************************************************************/
    void DisplayCard(int size)
    { 
         int num = 0;
         int rank;
         char suit;
         num = (size / 13);
         rank = (size % 13) ;
            
             if (num == 0)
                suit = 6;
             else if (num == 1)
                suit = 3;
             else if (num == 2)
                suit = 4;
             else if (num == 3)
                suit = 5;
         
         switch(size % 13)
         {
                     case 1:
                          printf("______\n"),
                          printf("|A   |\n"),
                          printf("|%c   |\n", suit),
                          printf("| %c%c |\n", suit, suit),
                          printf("| %c%c |\n", suit, suit),
                          printf("|   %c|\n", suit),
                          printf("|   A|\n"),
                          printf("______\n");
                          break;
                     case 2:
                          printf("______\n"),
                          printf("|2   |\n"),
                          printf("|%c   |\n", suit),
                          printf("| %c%c |\n", suit, suit),
                          printf("| %c%c |\n", suit, suit),
                          printf("|   %c|\n", suit),
                          printf("|   2|\n"),
                          printf("______\n");
                          break;
                     case 3:
                          printf("______\n"),
                          printf("|3   |\n"),
                          printf("|%c   |\n", suit),
                          printf("| %c%c |\n", suit, suit),
                          printf("| %c%c |\n", suit, suit),
                          printf("|   %c|\n", suit),
                          printf("|   3|\n"),
                          printf("______\n");
                          break;
                     case 4:
                          printf("______\n"),
                          printf("|4   |\n"),
                          printf("|%c   |\n", suit),
                          printf("| %c%c |\n", suit, suit),
                          printf("| %c%c |\n", suit, suit),
                          printf("|   %c|\n", suit),
                          printf("|   4|\n"),
                          printf("______\n");
                          break;
                     case 5:
                          printf("______\n"),
                          printf("|5   |\n"),
                          printf("|%c   |\n", suit),
                          printf("| %c%c |\n", suit, suit),
                          printf("| %c%c |\n", suit, suit),
                          printf("|   %c|\n", suit),
                          printf("|   5|\n"),
                          printf("______\n");
                          break;
                     case 6:
                          printf("______\n"),
                          printf("|6   |\n"),
                          printf("|%c   |\n", suit),
                          printf("| %c%c |\n", suit, suit),
                          printf("| %c%c |\n", suit, suit),
                          printf("|   %c|\n", suit),
                          printf("|   6|\n"),
                          printf("______\n");
                          break;
                     case 7:
                          printf("______\n"),
                          printf("|7   |\n"),
                          printf("|%c   |\n", suit),
                          printf("| %c%c |\n", suit, suit),
                          printf("| %c%c |\n", suit, suit),
                          printf("|   %c|\n", suit),
                          printf("|   7|\n"),
                          printf("______\n");
                          break;
                     case 8:
                          printf("______\n"),
                          printf("|8   |\n"),
                          printf("|%c   |\n", suit),
                          printf("| %c%c |\n", suit, suit),
                          printf("| %c%c |\n", suit, suit),
                          printf("|   %c|\n", suit),
                          printf("|   8|\n"),
                          printf("______\n");
                          break;
                     case 9:
                          printf("______\n"),
                          printf("|9   |\n"),
                          printf("|%c   |\n", suit),
                          printf("| %c%c |\n", suit, suit),
                          printf("| %c%c |\n", suit, suit),
                          printf("|   %c|\n", suit),
                          printf("|   9|\n"),
                          printf("______\n");
                          break;
                     case 10:
                          printf("______\n"),
                          printf("|10  |\n"),
                          printf("|%c   |\n", suit),
                          printf("| %c%c |\n", suit, suit),
                          printf("| %c%c |\n", suit, suit),
                          printf("|   %c|\n", suit),
                          printf("|  10|\n"),
                          printf("______\n");
                          break;
                     case 11:
                          printf("______\n"),
                          printf("|J   |\n"),
                          printf("|%c   |\n", suit),
                          printf("| %c%c |\n", suit, suit),
                          printf("| %c%c |\n", suit, suit),
                          printf("|   %c|\n", suit),
                          printf("|   J|\n"),
                          printf("______\n");
                          break;
                     case 12:
                          printf("______\n"),
                          printf("|Q   |\n"),
                          printf("|%c   |\n", suit),
                          printf("| %c%c |\n", suit, suit),
                          printf("| %c%c |\n", suit, suit),
                          printf("|   %c|\n", suit),
                          printf("|   Q|\n"),
                          printf("______\n");
                          break;
                     case 13:
                          printf("______\n"),
                          printf("|K   |\n"),
                          printf("|%c   |\n", suit),
                          printf("| %c%c |\n", suit, suit),
                          printf("| %c%c |\n", suit, suit),
                          printf("|   %c|\n", suit),
                          printf("|   K|\n"),
                          printf("______\n");
                          break;
         }
    }
    /*********************************************************************
     * FunctionName(Parameter Names) 
     *		Brief, general description of function's purpose -- what does it do?
     * PRE:	 list all preconditions here -- things that must be true for function to work.
     * POST: list all postconditions here -- things that are true when function returns.
     *********************************************************************/
    void DisplayDeck()
    {
         int index;
         for (index = 0; index < DECK_SIZE; index++)
         {
         DisplayCard(index);
         }
    }
    
    /*********************************************************************
     * FunctionName(Parameter Names) 
     *		Brief, general description of function's purpose -- what does it do?
     * PRE:	 list all preconditions here -- things that must be true for function to work.
     * POST: list all postconditions here -- things that are true when function returns.
     *********************************************************************/
    int ScoreCard(int card)
    {
        int score = 0;
        if (card%13 == 0)
        score = 1;
        else if (card%13 > 0 && card%13 <= 9)
        score = card%13;
        else score = 10;  
      return score;
    }
    /*********************************************************************
     * FunctionName(Parameter Names) 
     *		Brief, general description of function's purpose -- what does it do?
     * PRE:	 list all preconditions here -- things that must be true for function to work.
     * POST: list all postconditions here -- things that are true when function returns.
     *********************************************************************/
    int DrawCard(int deck[DECK_SIZE], int count)
    {
         int score;
         int card;   
         card = deck[count];
         DisplayCard(card);
         score = ScoreCard(card);
         return score;
    }

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    The bug is probably directly related to your incorrect indentation (to understate things). It is never acceptable to put two closing braces on the same line under any halfway-decent coding style.

    Make it all clean and tidy, like your conveniently named "clean" function and then we'll take a look at it. Everything inside a pair of curly braces should be indented furthur out. Search the web for how to indent code properly.
    Replace tabs with a fixed number of spaces before posting.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    6
    So it seems the issue of the characters getting messed up and the 5 aces being drawn in a row comes when the deck runs out of cards. So obviously it is not shuffling at the end of the deck again?

    Other than that, still same issues, sorry about initial indentation.
    Once again, any advice is appreciated.


    Code:
    /* Comp 120 - 01 Assignment 3, BlackJack */
    /* November 10th 2009 */
    /* Author: Clifford Donovan */
    /* This program runs a game of blackjack, starting with the player's turn, and
       then the computer's. */
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <time.h>
    
    /* function prototypes */
    void InitializeDeck();
    int RandomNumber(int);
    void ShuffleDeck();
    void DisplayCard(int);
    void DisplayDeck();
    int ScoreCard(int);
    int DrawCard(int[], int);
    void clean();
    #define DECK_SIZE  53
    int main()
    {
        
       int ppeanuts = 10; /*player's peanuts*/
       int cpeanuts = 10; /*computer's peanuts*/
       int deck[DECK_SIZE]; /*deck array*/
       int cardcount = 0; /*count that increases after each drawn card*/
       int scorep; /*player's score*/
       int scorec; /*computer's score*/
       char answer; /*answer for another card question*/
       char anotheranswer = 'y'; /*answer for another round question*/
       
         printf("Welcome to ULTIMATE BlackJack \n");
         printf("In ULTIMATE BlackJack, Aces are worth 1, and only 1\n");
         printf("Face cards are all worth 10. All other cards are worth their face value \n");
         printf("You and I start with 10 peanuts each. \nThe winner of a round takes one peanut from the loser.\n");
         printf("When one of us runs out, the game ends.\n");
         printf("Good Luck, you'll need it. \n\n\n");
         
                srand(time(NULL)); /* uses time as a seed for the random function used later on */
                InitializeDeck(deck); /* initializes the deck */
                ShuffleDeck(deck); /* shuffles the deck */
                
                      while (ppeanuts != 0  && cpeanuts != 0 && anotheranswer == 'y')
                            {
                                if (cardcount == (DECK_SIZE - 1))
                                 {
                                    ShuffleDeck(deck);
                                 }
                                scorep = 0; scorec = 0;
                                printf("Your Peanuts: %d My Peanuts: %d\n", ppeanuts, cpeanuts);
                                printf("--------------------------------\n\n");
                                printf("Your turn:\n\n");
                                scorep = scorep + DrawCard(deck, cardcount); 
                                printf("Score: %i\n\n", scorep);
                                cardcount++;
                                scorep = scorep + DrawCard(deck, cardcount); 
                                printf("Score: %i\n\n", scorep);
                                cardcount++;
                                printf("Another Card?\n\n");
                                scanf("%c", &answer);
                                clean();
                                
                                   while (scorep <= 21 && answer == 'y' )
                                 {
                                    if (cardcount == (DECK_SIZE - 1))
                                    {
                                        ShuffleDeck(deck);
                                    }  
                                    scorep = scorep + DrawCard(deck, cardcount),
                                    printf("Score: %i\n\n", scorep);
                                    if(scorep >21) 
                                    break; /* breaks the while loop if the player busts */
                                    cardcount++,
                                    printf("Another Card?\n\n"),
                                    scanf("%c", &answer);
                                    clean();
                                 }
                                 
                                      if (answer == 'n')
                                 {
                                       printf("My turn:\n\n");
                                       while (scorec < 17)
                                         {
                                           if (cardcount == (DECK_SIZE - 1))
                                           {
                                               ShuffleDeck(deck);
                                           }  
                                           scorec = scorec + DrawCard(deck, cardcount),
                                           printf("Score: %i\n\n", scorec),
                                           cardcount++;
                                         }
                                 }
                                if (answer != 'n' && answer != 'y')
                                 {
                                    printf("That is not a valid answer\n");
                                 }
                                if (scorep > 21)
                                 {
                                    printf("You Bust! I win.\n\n");
                                    ppeanuts --;
                                    cpeanuts ++;
                                    printf("Another Round?");
                                    scanf("%c", &anotheranswer);
                                    clean();
                                 }
                                 if (scorec > 21)
                                 {
                                    printf("I Bust, You Win...This Time\n\n");
                                    ppeanuts++;
                                    cpeanuts--;
                                    printf("Another Round?");
                                    scanf("%c", &anotheranswer);
                                    clean();
                                 }
                                 if (scorep > scorec && scorep < 22 && scorec < 22 )
                                 {
                                    printf("You Win\n\n");
                                    ppeanuts++;
                                    cpeanuts--;
                                    printf("Another Round?");
                                    scanf("%c", &anotheranswer);
                                    clean();
                                 }
                                 if (scorec > scorep && scorec < 22 && scorep < 22)
                                 {
                                    printf("I Win. \n\n");
                                    ppeanuts--;
                                    cpeanuts++;
                                    printf("Another Round?");
                                    scanf("%c", &anotheranswer);
                                    clean();
                                 }
                                 if (scorec == scorep)
                                 {
                                    printf("Push!\n\n");
                                    printf("Another Round?");
                                    scanf("%c", &anotheranswer);
                                    clean();
                                 }
                                 if (cardcount == (DECK_SIZE - 1))
                                 {
                                    ShuffleDeck(deck);
                                 }
                                 if (ppeanuts < cpeanuts)
                                 {
                                    printf("Game Over. I win. \n\n");
                                 }
                                 else if (ppeanuts > cpeanuts)
                                 {
                                    printf("Game Over. You Win. \n\n");
                                 
                                 }
                            }     
      system("PAUSE");
      return 0;
    }
    /********************************************************************* 
     * clean()
     *        Reads all characters from the input buffer until NEWLINE reached 
     * PRE: none
     * POST: input stream is empty 
     *********************************************************************/
    void clean (void)
    {
         char dummy;
         do 
         {
            scanf("%c",&dummy); 
         } 
         while(dummy != '\n'); 
    }
    /*********************************************************************
     * InitializeDeck(int deck[DECK_SIZE]) 
     *		Used to test and make sure the deck is shuffled correctly (if need be)
     * PRE:	 deck array initialized in main.
     * POST: makes the deck array from 0-52. 52 to tell the program to shuffle.
     *********************************************************************/
    void InitializeDeck(int deck[DECK_SIZE])
         {
            int index;
                for (index = 0; index < DECK_SIZE; index++)
                {
                    deck[index] = index;
                }
         }
    /*********************************************************************
     * RandomNumber(int upperlimit) 
     *		Function gets a random number between 0-51, then 0-50...etc.
     * PRE: index is being counted down in the ShuffleDeck function
            also, time is used as a seed, in the main function.
     * POST: returns the random number generated
     *********************************************************************/
    int RandomNumber(int upperlimit)
        {
            int number;
            number = rand()%(upperlimit +1);
            return number;
        }
    /*********************************************************************
     * ShuffleDeck(int deck[DECK_SIZE])
     *		Uses the random number to shuffle the deck(array)
     * PRE:	 random number generated correctly
     * POST: Deck is shuffled
     *********************************************************************/
    void ShuffleDeck(int deck[DECK_SIZE])
         {
            int rand;
            int index;
            int dummy;
                for(index = 51; index >= 0; index--)
                {
                   rand = RandomNumber(index);
                   dummy = deck[rand];
                   deck[rand] =  deck[index];
                   deck[index] = dummy;
                }
         }
    
    /*********************************************************************
     * DisplayCard(int size)
     *		Displays card and suit, in a neat little picture :)
     * PRE:	 only takes the card as a parameter
     * POST: will print the card in it's neat little format
     *********************************************************************/
    void DisplayCard(int size)
    { 
         int num = 0;
         int rank;
         char suit;
         num = (size / 13);
         rank = (size % 13) ;
            
             if (num == 0)
                suit = 6;
             else if (num == 1)
                suit = 3;
             else if (num == 2)
                suit = 4;
             else if (num == 3)
                suit = 5;
         
         switch(size % 13)
         {
                     case 1:
                          printf("______\n"),
                          printf("|A   |\n"),
                          printf("|%c   |\n", suit),
                          printf("| %c%c |\n", suit, suit),
                          printf("| %c%c |\n", suit, suit),
                          printf("|   %c|\n", suit),
                          printf("|   A|\n"),
                          printf("______\n");
                          break;
                     case 2:
                          printf("______\n"),
                          printf("|2   |\n"),
                          printf("|%c   |\n", suit),
                          printf("| %c%c |\n", suit, suit),
                          printf("| %c%c |\n", suit, suit),
                          printf("|   %c|\n", suit),
                          printf("|   2|\n"),
                          printf("______\n");
                          break;
                     case 3:
                          printf("______\n"),
                          printf("|3   |\n"),
                          printf("|%c   |\n", suit),
                          printf("| %c%c |\n", suit, suit),
                          printf("| %c%c |\n", suit, suit),
                          printf("|   %c|\n", suit),
                          printf("|   3|\n"),
                          printf("______\n");
                          break;
                     case 4:
                          printf("______\n"),
                          printf("|4   |\n"),
                          printf("|%c   |\n", suit),
                          printf("| %c%c |\n", suit, suit),
                          printf("| %c%c |\n", suit, suit),
                          printf("|   %c|\n", suit),
                          printf("|   4|\n"),
                          printf("______\n");
                          break;
                     case 5:
                          printf("______\n"),
                          printf("|5   |\n"),
                          printf("|%c   |\n", suit),
                          printf("| %c%c |\n", suit, suit),
                          printf("| %c%c |\n", suit, suit),
                          printf("|   %c|\n", suit),
                          printf("|   5|\n"),
                          printf("______\n");
                          break;
                     case 6:
                          printf("______\n"),
                          printf("|6   |\n"),
                          printf("|%c   |\n", suit),
                          printf("| %c%c |\n", suit, suit),
                          printf("| %c%c |\n", suit, suit),
                          printf("|   %c|\n", suit),
                          printf("|   6|\n"),
                          printf("______\n");
                          break;
                     case 7:
                          printf("______\n"),
                          printf("|7   |\n"),
                          printf("|%c   |\n", suit),
                          printf("| %c%c |\n", suit, suit),
                          printf("| %c%c |\n", suit, suit),
                          printf("|   %c|\n", suit),
                          printf("|   7|\n"),
                          printf("______\n");
                          break;
                     case 8:
                          printf("______\n"),
                          printf("|8   |\n"),
                          printf("|%c   |\n", suit),
                          printf("| %c%c |\n", suit, suit),
                          printf("| %c%c |\n", suit, suit),
                          printf("|   %c|\n", suit),
                          printf("|   8|\n"),
                          printf("______\n");
                          break;
                     case 9:
                          printf("______\n"),
                          printf("|9   |\n"),
                          printf("|%c   |\n", suit),
                          printf("| %c%c |\n", suit, suit),
                          printf("| %c%c |\n", suit, suit),
                          printf("|   %c|\n", suit),
                          printf("|   9|\n"),
                          printf("______\n");
                          break;
                     case 10:
                          printf("______\n"),
                          printf("|10  |\n"),
                          printf("|%c   |\n", suit),
                          printf("| %c%c |\n", suit, suit),
                          printf("| %c%c |\n", suit, suit),
                          printf("|   %c|\n", suit),
                          printf("|  10|\n"),
                          printf("______\n");
                          break;
                     case 11:
                          printf("______\n"),
                          printf("|J   |\n"),
                          printf("|%c   |\n", suit),
                          printf("| %c%c |\n", suit, suit),
                          printf("| %c%c |\n", suit, suit),
                          printf("|   %c|\n", suit),
                          printf("|   J|\n"),
                          printf("______\n");
                          break;
                     case 12:
                          printf("______\n"),
                          printf("|Q   |\n"),
                          printf("|%c   |\n", suit),
                          printf("| %c%c |\n", suit, suit),
                          printf("| %c%c |\n", suit, suit),
                          printf("|   %c|\n", suit),
                          printf("|   Q|\n"),
                          printf("______\n");
                          break;
                     case 13:
                          printf("______\n"),
                          printf("|K   |\n"),
                          printf("|%c   |\n", suit),
                          printf("| %c%c |\n", suit, suit),
                          printf("| %c%c |\n", suit, suit),
                          printf("|   %c|\n", suit),
                          printf("|   K|\n"),
                          printf("______\n");
                          break;
         }
    }
    /*********************************************************************
     * DisplayDeck() 
     *		Used to test and make sure the deck is shuffled correctly (if need be)
     * PRE:	 Deck iniitialized
     * POST: none.
     *********************************************************************/
    void DisplayDeck()
    {
         int index;
         for (index = 0; index < DECK_SIZE; index++)
         {
            DisplayCard(index);
         }
    }
    
    /*********************************************************************
     * ScoreCard(int card) 
     *		Assigns each card it's score, Ace is 1, face cards are 10, 
            the rest are their face value.
     * PRE: Card is valid
     * POST: returns the score of the card
     *********************************************************************/
    int ScoreCard(int card)
    {
        int score = 0;
        if (card%13 == 0)
        score = 1;
        else if (card%13 > 0 && card%13 <= 9)
        score = card%13;
        else score = 10;  
      return score;
    }
    /*********************************************************************
     * DrawCard(int deck[DECK_SIZE], int count) 
     *		Draws a card from the deck
     * PRE:	 takes the cardcount (the counter that counts throughout the game) 
             to draw the card in order
     * POST: returns the the score of the drawn card from the ScoreCard function
             also calls the DisplayCard function so this prints the card as well.
     *********************************************************************/
    int DrawCard(int deck[DECK_SIZE], int count)
    {
         int score;
         int card;   
         card = deck[count];
         DisplayCard(card);
         score = ScoreCard(card);
         return score;
    }

  4. #4
    Professional Chef leeor_net's Avatar
    Join Date
    Apr 2004
    Location
    Charles Town, WV
    Posts
    147
    I hate doing this but: How To Ask Questions The Smart Way
    - Leeor

  5. #5
    Registered User
    Join Date
    Nov 2009
    Posts
    6
    really don't see what's wrong with the way I asked it. I was polite about it, I clearly identified what my problems are.

  6. #6
    Registered User
    Join Date
    Nov 2009
    Posts
    6
    Any help would be incredible. Pleasee

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,290
    Quote Originally Posted by westside222 View Post
    Also, when asked for another card or another game, if enter is pressed twice instead of y or n then enter, it skips that process and just says "you win!"
    That's a result of how scanf("%c", &answer) works. Hit enter twice, and the first call will give answer the character '\n', as will the second call.

    If you look at the various checks your code does (in various places) of answer, you will see it just drops through to printing "You Win". You need to check more cases.

    Quote Originally Posted by westside222 View Post
    really don't see what's wrong with the way I asked it. I was polite about it, I clearly identified what my problems are.
    The reason you're not getting much help is that

    1) You have posted a largish body of code
    2) You have described your problems in a manner that assumes other people can easily understand the logic of your code.
    3) The logic of your code is not that easy to understand: the names of variables are not informative, you have not included comments, and it is necessary for someone else to digest the whole mess to even make partial sense of it.
    4) People here aren't paid for advice. If you only apply a small effort to solving your problem why should others put a larger effort in? Running a few test cases, as you have done, involves much less effort than reading your code and understanding it, in order to find the problems you describe.

    If you want more informative advice, provide a small but complete example of code that illustrates each problem - and nothing else. In the process of creating those illustrative examples, you might solve the problem yourself (an "aha!" moment). If you can't, you will have code in a form that other people can give you useful advice on.
    Right 98% of the time, and don't care about the other 3%.

  8. #8
    Registered User
    Join Date
    Nov 2009
    Posts
    6
    Sorry, I honestly didn't realize how much I was asking for until you blatantly stated it. Thanks for the tip on the You win case. Now that's fixed, I also fixed the ace not displaying issue by adding an if statement and another 13 cases in a separate switch statement. This is because 0 %13 is 13, but 13%13 is 0. But both these cases should be aces. This also required a quick change in the score function, to add an if statement there in the same way.

    My only remaining problem now is that when the deck runs out, I don't think it reshuffles. I think this because the it draws 3 or 4 aces in a row, which would be the 0 % 13. But what's odd is that then it starts to draw cards like normal, but the characters get all messed up, now, I think this call should work for shuffle:
    Code:
    if (cardcount == deck[DECK_SIZE])
                                 {
                                    ShuffleDeck(deck);
                                 }
    This being the Shuffle Function:
    Code:
    void ShuffleDeck(int deck[DECK_SIZE])
         {
            int rand;
            int index;
            int dummy;
                for(index = 51; index >= 0; index--)
                {
                   rand = RandomNumber(index);
                   dummy = deck[rand];
                   deck[rand] =  deck[index];
                   deck[index] = dummy;
                }
         }
    I still don't understand why the characters get messed up, it's very odd to me.

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    2,032
    I think you want if (cardcount == DECK_SIZE).
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  10. #10
    Registered User
    Join Date
    Nov 2009
    Posts
    6
    GOT IT!
    what i did is made it print the deck after the shuffle function.
    It is shuffling it properly. The problem is that I didn't reset the cardcount afterwards.

    Now the only issue I have left is small but odd, about half of the time I run the program, there is an issue that the score will increase by 10 but no card is displayed. But, it doesn't happen every time, even when I go through the whole deck. Any clue?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  2. global namespace errors
    By stubaan in forum C++ Programming
    Replies: 9
    Last Post: 04-02-2008, 03:11 PM
  3. Need help with initialization errors
    By JoelearningC in forum C Programming
    Replies: 1
    Last Post: 03-08-2008, 02:16 PM
  4. executing errors
    By s0ul2squeeze in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2002, 12:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21