Thread: my blackjack prog

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    41

    my blackjack prog

    so i am very new to this. I have such an inpartial code. I have no idea how to print out the hands for the player and the dealer, they are supposed to be random but i am just lost can anyone help me with some ideas, advice, or snippets PLEASE help
    Code:
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    #include<time.h>
    #define DECKSIZE 52
    #define VALUE 9
    #define FACE 4
    
    typedef struct {
            int value;
            char* suit;
            char* name;
    }Card;
    
    Card cards[DECKSIZE];
    char *faceName[]={"two","three","four","five","six","seven","eight","nine",
                      "ten","jack","queen","king","ace"};
    char *suitName[]={"spades","diamonds","hearts","clubs"};
    void printDeck(){
    int i;
    for (i=0;i<6000;i++){
    int j = i+ rand()%(6000-i);
            this=rand()%DECKSIZE;
            that=rand()%DECKSIZE;
    while(this==that)that=rand()%(52+1);
    //printf("shuffle  card%d with card %d\n", this, that);
    temp=cards[this];
    cards[this]=cards[that];
    cards[that]=temp;
    }
    }
    int main(){
            int suitCount=0;
            int faceCount=0;
            int i;
            for(i=0;i<DECKSIZE;i++){
                    if(faceCount<9){
                            cards[i].value=faceCount+2;
                    }else{
                            cards[i].value=10;
                    }
                    cards[i].suit=suitName[suitCount];
                    cards[i].name=faceName[faceCount++];
                    if(faceCount==13){
                            cards[i].value=11;
                            suitCount++;
                            faceCount=0;
                    }
            }
    {
    
    //printDeck();
    //shuffleDeck();
    //printDeck();
            }
    int P_hand, D_hand;
    P_hand=rand() %52+1;
    D_hand=rand() %52+1;
    shuffleDeck();
    printf("%s of %s,----%s of %s\n ",cards[1].name,cards[3].suit,cards[4].name,cards[2].suit );
    shuffleDeck();
    printf("%s of %s,----%s of %s\n",cards[1].name,cards[3].suit,cards[4].name,cards[2].suit  );
    shuffleDeck();
            return 0;
    }
    Last edited by gloworm; 04-12-2010 at 04:20 PM. Reason: updated code!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So first, shuffle the deck.

    Then, deal from the deck, so you will get card 0, the dealer will get card 1, you will get card 2, the dealer will get card 3. So if you are printing the dealer hand, print out cards 1 and 3; when you are printing your hard, print out cards 0 and 2.

    (EDIT: Ignore the bit that was here -- you've got the suit and the face. So print
    Code:
    cards[1].suit, cards[1].name
    )
    Last edited by tabstop; 04-12-2010 at 01:16 PM.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    41
    Quote Originally Posted by tabstop View Post
    So first, shuffle the deck.

    Then, deal from the deck, so you will get card 0, the dealer will get card 1, you will get card 2, the dealer will get card 3. So if you are printing the dealer hand, print out cards 1 and 3; when you are printing your hard, print out cards 0 and 2.

    (EDIT: Ignore the bit that was here -- you've got the suit and the face. So print
    Code:
    cards[1].suit, cards[1].name
    )
    i did that and yes it printed out the parts of the array but for some odd reason the shuffle function is not changing the arrangment of my arrays so cards[1].name is not changing when it prints out the second set?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You definitely need to shuffle more than 10 cards in your deck. You should shuffle the whole deck; something like
    Code:
    for (i = 0; i < 52; i++) {
        int j = i + rand()%(52-i); //pick a card to swap with
        //swap i and j
    }

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    41
    Quote Originally Posted by tabstop View Post
    You definitely need to shuffle more than 10 cards in your deck. You should shuffle the whole deck; something like
    Code:
    for (i = 0; i < 52; i++) {
        int j = i + rand()%(52-i); //pick a card to swap with
        //swap i and j
    }
    i did update the code and you were right i wasnt shuffling enough so now i am getting random cards and thats great and i see what that code was doing only shuffling ten cards increased the chances of me getting the same cards so thanks for that. but maybe now you can help me out a little bit more with how to store the hands to the P_hand and D_hand function that i already have declared so that i can start assigning values of the hands to the functions P_hand and D_hand to start my math equations

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Are P_hand supposed to represent the actual hand or just the value? Since right now, they're ints, and obviously you can't store a bunch of cards to an int.

  7. #7
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329
    how to store the hands to the P_hand and D_hand function that i already have declared so that i can start assigning values of the hands to the functions P_hand and D_hand to start my math equations
    Maybe I'm not understanding this correctly, but you mean something like the following...

    Code:
    #include <stdio.h>
    
    struct person 
    {
      int age;
      char *status;
    };
    
    void married(struct person male) 
    {
      male.status = "Gets laid.";
      printf("%s\n", male.status);
    }
    
    void divorced(struct person male)
    {
      male.status = "Using his right hand for a while.";
      printf("%s\n", male.status);
    }
    
    int main(void)
    {
      struct person info;
    
      info.age = 26;
      info.status = "limbo";
    
      printf("Status before: %s\n", info.status);
      printf("------------------------------\n");
    
      married(info);
      divorced(info);
    
      printf("------------------------------\n");
      printf("Status after: %s\n", info.status);
    
      return 0;
    }
    [cd@localhost oakland]$ gcc -Wall -Wextra -Wshadow bj.c -o bj
    [cd@localhost oakland]$ ./bj
    Status before: limbo
    ------------------------------
    Gets laid.
    Using his right hand for a while.
    ------------------------------
    Status after: limbo
    I just basically filled in the structure with some initial values and passed this structure to both the functions married() and divorced(). Then both of these functions updated the 'status' field.

  8. #8
    Registered User
    Join Date
    Mar 2010
    Posts
    41
    Quote Originally Posted by Overworked_PhD View Post
    Maybe I'm not understanding this correctly, but you mean something like the following...

    Code:
    #include <stdio.h>
    
    struct person 
    {
      int age;
      char *status;
    };
    
    void married(struct person male) 
    {
      male.status = "Gets laid.";
      printf("%s\n", male.status);
    }
    
    void divorced(struct person male)
    {
      male.status = "Using his right hand for a while.";
      printf("%s\n", male.status);
    }
    
    int main(void)
    {
      struct person info;
    
      info.age = 26;
      info.status = "limbo";
    
      printf("Status before: %s\n", info.status);
      printf("------------------------------\n");
    
      married(info);
      divorced(info);
    
      printf("------------------------------\n");
      printf("Status after: %s\n", info.status);
    
      return 0;
    }


    I just basically filled in the structure with some initial values and passed this structure to both the functions married() and divorced(). Then both of these functions updated the 'status' field.
    i guess my question is because i assigned values to the cards originally, when i print them out at the bottom how can i find out the total between those two cards and if it is still storing them. and i updated my code to what i have so far
    Last edited by gloworm; 04-12-2010 at 04:20 PM.

  9. #9
    Registered User
    Join Date
    Mar 2010
    Posts
    41
    Quote Originally Posted by tabstop View Post
    Are P_hand supposed to represent the actual hand or just the value? Since right now, they're ints, and obviously you can't store a bunch of cards to an int.
    P_hand and D_hand are supposed to be used just to identify the dealers and players hand but i was hoping to assign the total values of each hand to just those prototypes and use just those to start working on the math function of the blackjack program which is another problem all together, HELP where ever you can, so far your the most helpful person to me. Im learning so much and actually progressing on my personal project.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So a hand is a bunch of cards. Therefore you need P_hand and D_hand to be things that can hold a bunch of cards, like say an array of cards. (Just like the deck is an array of cards.)

  11. #11
    Registered User
    Join Date
    Mar 2010
    Posts
    41
    so i need to initialize like: P_hand[] D_hand[]? and i need to put these as a define statement yes?
    please know that i am really nieve. so this is what i added:
    Code:
    int P_hand[6], D_hand[6];
    P_hand[6]=rand() %52+1;
    D_hand[6]=rand() %52+1;
    shuffleDeck();
    printf("%s of %s,  %s of %s\n ",cards[1].name,cards[3].suit,cards[4].name,cards[2].suit );
    shuffleDeck();
    printf("%s of %s,  %s of %s\n",cards[1].name,cards[3].suit,cards[4].name,cards[2].suit  );
    shuffleDeck();
    is this what you were talking about? and if so explain how i can get the values added into these arrays and so on and so forth
    Last edited by gloworm; 04-12-2010 at 05:58 PM.

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I don't know how well acting helpless works for you IRL; it's getting old here. How have you gotten your program to where it is if you don't know how to declare variables?

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Pick one thread, stop posting in the other. You don't need fifteen threads on the same topic, all started by you.


    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by gloworm View Post
    so i need to initialize like: P_hand[] D_hand[]? and i need to put these as a define statement yes?
    please know that i am really nieve. so this is what i added:
    Code:
    int P_hand[6], D_hand[6];
    P_hand[6]=rand() %52+1;
    D_hand[6]=rand() %52+1;
    shuffleDeck();
    printf("%s of %s,  %s of %s\n ",cards[1].name,cards[3].suit,cards[4].name,cards[2].suit );
    shuffleDeck();
    printf("%s of %s,  %s of %s\n",cards[1].name,cards[3].suit,cards[4].name,cards[2].suit  );
    shuffleDeck();
    is this what you were talking about? and if so explain how i can get the values added into these arrays and so on and so forth
    I'll take the first line. And, okay, the first "shuffleDeck()" line. But the rest ... no. You don't shuffle the cards after every time you deal a card! And why are you giving the dealer the name of the first card with the suit of the third card?! And naturally the dealer and the player will get different cards.

    And how do you assign any value to any variable?

  15. #15
    Registered User
    Join Date
    Aug 2007
    Location
    MD, USA
    Posts
    71
    I think that Overworked_PhD was illuding that a small struct might work well for P_hand and D_handjob.
    You could hold the hand score and the value of each card.

    I guess it doesn't matter for blackjack (does it?)
    but a 2 element int object might work well to hold a card suit and value.
    eg: char card[2] = {12, 0} for the queen of clubs. (or whatever the lowest suit is)
    Last edited by HowardL; 04-12-2010 at 09:09 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple Blackjack Program
    By saber1357 in forum C Programming
    Replies: 1
    Last Post: 03-28-2009, 03:19 PM
  2. Help with Blackjack program
    By sugie in forum C++ Programming
    Replies: 1
    Last Post: 04-30-2005, 12:30 AM
  3. Getting input from another prog?
    By Badman3k in forum C Programming
    Replies: 4
    Last Post: 11-11-2004, 02:58 AM
  4. Blackjack!
    By Dr. Bebop in forum Game Programming
    Replies: 1
    Last Post: 10-03-2002, 08:58 PM
  5. Try my prog...
    By Commander in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 05-09-2002, 07:43 AM

Tags for this Thread