Thread: General Question

  1. #1
    Registered User
    Join Date
    Mar 2017
    Posts
    13

    General Question

    Let's say function 1 and function 2 are called in main(void).
    I have also created function 3.

    Function 1 and function 2 give separate print statements whenever the loops within their respective functions meet certain qualifications.

    I want function 3 to print a statement whenever both function 1 and function 2 give their results/outputs. How would I go about this? What would I include in function 3? What statements would I use? I hope this makes sense.

    I have the actual code, I just wanna try to figure this one out myself haha.

    More background: Function 1 prints if a card shuffle is a pair, while function 2 prints if a card shuffle is a 3 of ___. I want function 3 to print if there is a full house, which in poker is when both a 3 of ____ and a pair of something occur simultaneously.

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Since my answer to your last post got no response (why?), I have little motivation to answer this one.

  3. #3
    Registered User
    Join Date
    Mar 2017
    Posts
    13
    I don't get notifications to new replies! I'm sorry, I'll reply right now, I've been busy with work and finals...

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    We wouldn't normally write separate functions for each of the hand categories since they are so interrelated. It's best to gather information about the number of pairs, trips, quads, and whether there is a flush and/or straight, then use that data to determine the category.

    This is untested (partly pseudocode) but shows the idea.
    Code:
    int value(Card card) {
        return value of card from 2 to 14 (ace)
    }
    
    int suit(Card card) {
        return suit of card from 0 to 3
    }
    
    int analyze_hand(Card *hand) {
        int v[NUM_VALUES] = { 0 }, s[NUM_SUITS] = { 0 };
        int pairs = 0, trips = 0, quads = 0, flush = 0, straight = 0;
        int num_in_a_row = 0;
    
        for (int i = 0; i < NUM_CARDS_IN_HAND; i++) {
            ++v[value(hand[i]) - 2];
            if (++s[suit(hand[i])] == NUM_CARDS_IN_HAND)
                flush = 1;
        }
    
        for (int i = 0; i < NUM_VALUES; i++) {
            if (v[i] > 0) {
                if (++num_in_a_row == NUM_CARDS_IN_HAND)
                    straight = 1;
                if      (v[i] == 2) pairs++;
                else if (v[i] == 3) trips++;
                else if (v[i] == 4) quads++;
            }
            else
                num_in_a_row = 0;
        }
    
        if      (flush && straight)        printf("straight flush\n");
        else if (quads == 1)               printf("four of a kind\n");
        else if (trips == 1 && pairs == 1) printf("full house\n");
        else if (flush)                    printf("flush\n");
        else if (straight)                 printf("straight\n");
        else if (trips == 1)               printf("three of a kind\n");
        else if (pairs == 2)               printf("two pair\n");
        else if (pairs == 1)               printf("one pair\n");
        else                               printf("no pair\n");
    }
    This assumes that ace-low straights (and straight flushes) aren't allowed (i.e., aces always have a value of 14 and never 1).

    And it doesn't distinguish between straight flush and royal flush (which is just the highest straight flush). You could do that by first checking if it's a straight flush and then checking that it contains an ace.
    Last edited by algorism; 04-17-2017 at 07:06 PM.

  5. #5
    Registered User
    Join Date
    Mar 2017
    Posts
    13
    Ahhh thank you so much algorism!!!!!! You always go the extra mile, I figured out what was wrong with my functions, that you included - value. What I did was give pairs and three of a kind's a different value, so that I could add them up or call the values in order to manipulate them to print out a Full House.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scp general question!
    By yani987 in forum Tech Board
    Replies: 1
    Last Post: 09-02-2013, 10:22 PM
  2. general question
    By a.mlw.walker in forum C Programming
    Replies: 25
    Last Post: 04-04-2009, 09:32 AM
  3. A general question
    By vampire in forum C Programming
    Replies: 7
    Last Post: 11-06-2005, 09:53 PM
  4. General Question
    By GSLR in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 05-02-2002, 12:15 PM
  5. General GUI question in C
    By Music_Man in forum Game Programming
    Replies: 3
    Last Post: 11-16-2001, 11:45 AM

Tags for this Thread