Thread: I need help, I'm desperate

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    1

    Unhappy I need help, I'm desperate

    I've got this program due the end of next week, which is finals. I've got cramming for other finals which are more important (as in this code will not make/break my grade other finals will.) This code crashes sometimes and I know the cause (handindex & value) but I am so swammped my brain can't process a good solution.

    Could I please get some help.

    The premise of the code is a simulated game of Blackjack, the dealer is automatic, deals two cards to itself, and if it doesn't win/bust then it deals until it hits 17 then it stops. Then the player hits if until satisfied (unless they win / bust @ 2) then a winner is determined.

    I normally don't like asking for help because I try and figure it out for myself but with finals next week I am stressed out and trying to cram so I can't think of a solution.

    thank you in advanced.


    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #define SIZE 52
    #define LIMIT 21 
    
    enum faces{Ace = 0, Jack = 10, Queen, King};
    char * facecheck(int d); 
    void shuffle( int deck[]);
    int draw(int deck[SIZE]); 
    void printcards(int hand[], int numCards);
    int dealer(int deck[]);  
    int player(int deck[]);
    int value(int yourhand[]); 
    int victory(int d, int p);
    int i, numCards = 1;
    int top = 52;
    int preValue = 0; 
    int count = 2; 
    int main() 
    { 
        int deck[SIZE], i, a;
        int d, p; 
        char suits[4][9] = 
        {
            "Hearts",
            "Diamonds",
            "Clubs",
            "Spades"};
    
    
        srand( time( NULL ) ) ;
    
        for(i = 0; i<SIZE; i++)
        {
            deck[i] = i;
        };
    
        shuffle(deck);
        d = dealer(deck);
        p = player(deck);
        victory(d, p);
    
    
    
        return 0; 
    }  
    
    char * facecheck(int d)
    {
        static char * face[] = 
        {
            "Ace",
            "Jack",
            "Queen",
            "King" };
    
        if(d == Ace)
            return face[0];
        else
        {
            if(d == Jack) 
                return face[1];
            else
            {
                if(d == Queen)
                    return face[2];
                else 
                { 
                    if(d == King)
                        return face[3];
                }
            }
        }
    }
    
    
    
    void shuffle( int deck[]) 
    {
         int i, j, temp; 
    
         for(i = 0; i < SIZE; i++)
         {
               j = rand() % SIZE; 
               temp = deck[i];
               deck[i] = deck[j];
               deck[j] = temp;
               }
         printf("The deck has been shuffled \n"); 
    } 
    
    int draw(int deck[SIZE])
    {
        int numCards = 1;
        int i;  
        int hand[numCards];
        int card;
        for(i = 0; i < numCards && top > 0; i++)
        {
            card = deck[top-1];     
            hand[i] = card; 
            top--;   
        }
    
        return card;
    
    }
    
    void printcards(int hand[], int numCard)
    {   
        char suits[4][9] = 
        {
            "Hearts",
            "Diamonds",
            "Clubs",
            "Spades"};
    
        for(i = 0; i < numCard; i++) 
        {
         int card = hand[i];     
        if(card%13 == 0 || card%13 == 10 || card%13 == 11 || card%13 == 12)
            printf("%s ", facecheck(card%13) );
        else 
            printf("%d ", card%13+1);
        printf("of %s \n", suits[card/13]);
        }
    }
    
    int dealer(int deck[])
    {
        int x;
        int a; 
        int yourhand[10];
        int handIndex;
        int cardLimit;
        int dealerValue;
    
            yourhand[handIndex] = draw(deck);
            yourhand[handIndex] = draw(deck);
            printf("The Dealers second card is:"); 
            printcards(yourhand, handIndex+1);
    
            cardLimit = value(yourhand);
    
            do
            {
             if(cardLimit == LIMIT)
             {
                 printcards(yourhand, handIndex+1);
                 dealerValue = cardLimit;
                 return dealerValue;
             }
    
             if(cardLimit > LIMIT)
             {
                 printcards(yourhand, handIndex+1);
                 dealerValue = cardLimit;
                 return dealerValue;
             }
             if(cardLimit == 17)
             {
                 printcards(yourhand, handIndex+1);
                 dealerValue = cardLimit;
                 return dealerValue;
             }
             if(cardLimit <= 16)
             {
                yourhand[handIndex] = draw(deck);
                cardLimit = value(yourhand);
             }
            }
             while(cardLimit <= LIMIT); 
             handIndex++;
    
    
    }
    
    int player(int deck[])
    {
        int x;
        int a; 
        int yourhand[10];
        int cardLimit;
        int playerValue;
        int handIndex = 2;
    
        yourhand[handIndex] = draw(deck);
        yourhand[handIndex] = draw(deck);
        cardLimit = value(yourhand);
        printf("Your hand is: /n"); 
        printcards(yourhand, handIndex+1);
        printf("%d /n" , cardLimit); 
    
        do
            {
             if(cardLimit == LIMIT)
             {
                 printcards(yourhand, handIndex+1);
                 playerValue = cardLimit;
                 return playerValue;
             }
    
             if(cardLimit > LIMIT)
             {
                 printcards(yourhand, handIndex+1);
                 playerValue = cardLimit;
                 return playerValue;
             }
             else
             {
                 printf("What would you like to do: Press 1 to Hit. 2 to Stay. \n"); 
                 scanf("%d" , &x);
                 if(x == 1)
             {
                yourhand[handIndex] = draw(deck);
                cardLimit == value(yourhand);
                }
                else
                {
                    printf("Player choses to stay \n");
                    return playerValue;
                }
            }
            }
            while(cardLimit <= LIMIT); 
    
            handIndex++;
    }
    
    int value(int yourhand[])
    {
        int faceValue = 10;
        int cardValue[count]; 
        int aceValue = 11;
        int card[count]; 
        int value; 
        int curvalue; 
    
    
          for(i = 0; i < count; i++) 
          {
          card[i] = yourhand[i];
          }
    
         for(i = 0; i < count; i++) 
         { 
             cardValue[i] = card[i]%13; 
         } 
    
         if(cardValue[0] >= 10 && cardValue[1] >= 10)
         {
             value = 20;
         }
         if(cardValue[0] < 10 && cardValue[1] < 10) 
         {
             value = cardValue[0] + cardValue[1];
         }
         if(cardValue[0] >= 10 && cardValue[1] < 10) 
         {
             value = faceValue + cardValue[1];
         }
         if(cardValue[0] < 10 && cardValue[1] >= 10) 
         {
             value = faceValue + cardValue[0];
         }
         if(cardValue[0] == 0 && cardValue[1] == 0) 
         {
             value = 12; 
         }       
         if(cardValue[0] == 0 && cardValue[1] >= 10) 
         {
             value = 21;
         }
         if(cardValue[1] == 0 && cardValue[0] >= 10) 
         {
             value = 21;
         }        
         if(cardValue[0] == 0 && cardValue[1] < 10) 
         {
             value = aceValue + cardValue[1];
         } 
         if(cardValue[1] == 0 && cardValue[0] < 10) 
         {
             value = aceValue + cardValue[0];
         } 
    
         preValue = value;
    
         if(count > 2) 
         { 
            if(cardValue[count] != 0) 
            {
                   value = curvalue; 
                   value = preValue + curvalue;
            }   
            else
            {
                if(cardValue[count] + preValue > LIMIT) 
                {
                     value = preValue + 1; 
                }
                else
                {
                    value = cardValue[count] + aceValue;
                }
            }
         }
         count++; 
         return value; 
    }
    
    int victory(int d, int p)
    {
        if(d > p) 
        printf("Dealer Wins \n"); 
        else
        printf("Player Wins"); 
    }

  2. #2
    Registered User
    Join Date
    Feb 2013
    Location
    Buea Cameroon
    Posts
    64
    There is never time for anything we all always have to create time for the things that matter. With an uncommented code like this! i guess you wasted time even posting it on this forum

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    307
    Quote Originally Posted by acho.arnold View Post
    There is never time for anything we all always have to create time for the things that matter. With an uncommented code like this! i guess you wasted time even posting it on this forum
    It helps to A) know enough before posting and of course B) not being a total ass!!!

    no where have i seen ANYONE say "your code needs commenting"

    ok now to the prob, here are the warnings at compile time for you to fix ( if you dont get these warnings, you need to set your warning level higher in your compilier)

    (21): warning #2117: Old-style function definition for 'main'.
    (32): warning #2018: Undeclared function 'time'; assuming 'extern' returning 'int'.
    (24): warning #2115: Local 'suits' is initialized but never used.
    (22): warning #2114: Local 'a' is not referenced.
    (49): warning #2235: Not all control paths return a value.
    (98): warning #2229: Local 'card' is potentially used without being initialized.
    (133): warning #2114: Local 'a' is not referenced.
    (132): warning #2114: Local 'x' is not referenced.
    (130): warning #2235: Not all control paths return a value.
    (135): warning #2116: Local 'handIndex' is used without being initialized.
    (182): warning #2114: Local 'a' is not referenced.
    (179): warning #2235: Not all control paths return a value.
    (185): warning #2116: Local 'playerValue' is used without being initialized.
    (237): warning #2229: Local 'value' is potentially used without being initialized.
    (238): warning #2116: Local 'curvalue' is used without being initialized.
    (313): warning #2096: Missing return value.

  4. #4
    Registered User
    Join Date
    Jan 2013
    Posts
    24
    Since it doesn't seem to matter, and it won't affect your grade according to you, goodbye.

  5. #5
    Registered User
    Join Date
    Feb 2013
    Location
    Buea Cameroon
    Posts
    64
    Quote Originally Posted by Cody Rogers View Post
    Could I please get some help.
    I wish to help but i dont even know how the blackjack game works can you please explain?

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Google "blackjack" and you'll see it's a card game where the object is to draw cards until you have the highest card values (having added them up), but your value, all added up, must be 21 or less. Everything above a ten is equal to ten, but aces can be either a value of one, or eleven.

    This is a relatively simple card game, and is a fairly common programming assignment for students. We don't need comments for such a simple program. The description was enough to answer questions we'd have.

    Cody has been helped - let's say he's been helped if he knows how to correct the problems that were pointed out to him.

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    There are distinct differences between the following kinds of responses:

    1) Tactful directness, with or without a touch of sass
    2) Warranted rudeness
    3) Unwarranted rudeness

    Most of the regulars, when necessary, opt for the first two categories. Some of the newcomers on this thread fall into the third. Perhaps they should consider more positive contributions before resorting to rude and unhelpful responses.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. desperate help ...
    By devilrulez in forum C Programming
    Replies: 29
    Last Post: 12-10-2010, 06:46 PM
  2. in desperate need of help
    By Lost_programmer in forum C Programming
    Replies: 19
    Last Post: 10-31-2006, 04:39 AM
  3. Help for the desperate ...?
    By kmr6655 in forum C Programming
    Replies: 2
    Last Post: 11-03-2002, 01:54 PM
  4. I don't like doing this but i'm desperate
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 07-21-2002, 06:44 PM
  5. Desperate
    By sweets in forum C Programming
    Replies: 5
    Last Post: 04-11-2002, 03:48 PM

Tags for this Thread