-2

I've put together a program that deals out a hand poker perfectly. Now I want the program to realize when the hand that is dealt is straight, flush, pair, 3 of a kind, and 4 of kind. The program runs but never prints the right condition when needed to, I believe I have some placement or logic error that I can't find. Here's what I have so far.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define SUITS 4
#define FACES 13
#define CARDS 52
#define HAND 5//draw only 5
#define TRUE 1//positive print condition
#define FALSE 0//negative print condition

//prototypes
shuffle( unsigned int wDeck[][FACES]);//shuffling modifies wDeck
deal(unsigned int wDeck[][FACES], const char *wFace[],
  const char *wSuit[] );//dealing doesn't modify the arrays


//true/false conditions
typedef int bool;
bool straight, flush, four, three;
int pairs; //0,1, or 2



int main()
{
//initialize suit array
const char *suit[ SUITS ] =
    {
        "Hearts", "Diamonds", "Clubs", "Spades"
    };

//initialize face array
const char *face[ FACES ] =
    {
        "Ace", "Deuce", "Three", "Four",
        "Five", "Six", "Seven", "Eight",
        "Nine", "Ten", "Jack", "Queen", "King"
    };

int suitInHand[SUITS], facesInHand[FACES];

analyzeHand(suitInHand, facesInHand);

//initialize deck array
unsigned int deck[SUITS][FACES] = { 0 };

srand( time( NULL ) );//seed random-number generator

shuffle( deck );//shuffle the deck
deal( deck, face, suit );//deal the deck

}//end main

//shuffle cards in deck
shuffle( unsigned int wDeck[][FACES])
{
    size_t row;//row number
    size_t column;//column number
    size_t card;//counter

    //for each of the cards, choose slot of deck randomly
   for( card = 1; card <= CARDS; ++card) {

    //choose new random location until unoccupied slot found
    do {
        row = rand() % SUITS;
        column = rand() % FACES;
    }
    while( wDeck[ row ][ column ] !=0);
    //end do-while

    //pace card number in chosen slot of deck
    wDeck[ row ][ column ] = card;
   }//end for
}//end function shuffle

//deal cards in deck
deal(unsigned int wDeck[][FACES], const char *wFace[],
     const char *wSuit[] )
{
    size_t card;//card counter
    size_t row;//row counter
    size_t column;//column counter

    //deal each of the cards
    for( card = 1; card <= HAND; ++card) {

        //loop through rows of wDeck
        for( row = 0; row < SUITS; ++row) {

            //loop through column of wDeck for current row
            for( column = 0; column < FACES; ++column) {

                //if slot contains current card, display card
                if( wDeck[ row ][ column ] == card ) {
                    printf("%5s of %-8s%c", wFace[ column ], wSuit[ row ],
                           card % 2 == 0 ? '\n' : '\t' );//2 column format
                }//end if
            }//end for
        }//end for
    }//end for
}//end function deal

analyzeHand(int suitsInHand[], int facesInHand[])
{
    int num_consec = 0;
    int rank, suit;

    straight = FALSE;
    flush = FALSE;
    four = FALSE;
    three = FALSE;
    pairs = 0;

    for (suit = 0; suit < SUITS; suit++)
        if ( suitsInHand[suit] == 5)
                    flush = TRUE;

    rank = 0;
    while ( facesInHand[rank] == 0)
        rank++;

    for (; rank < FACES && facesInHand[rank]; rank++)
        num_consec++;

    if(num_consec == 5){
        straight = TRUE;
        return;
    }

    for(rank = 0; rank < FACES; rank++) {
        if(facesInHand[rank] == 4)
            four = TRUE;
        if(facesInHand[rank] == 3)
            three = TRUE;
        if(facesInHand[rank] == 2)
            pairs++;
    }

    if(four)
        printf("Four of a kind\n");
    else if(straight)
        printf("Straight\n");
    else if(pairs == 2)
        printf("Two Pairs\n");
    else if(pairs == 1)
        printf("Pair\n");
    else
        printf("Better Luck Next Time\n");
}
Cidricc
  • 115
  • 1
  • 3
  • Your program needs many more functions, you have like 10 levels of indentation in just one function, it's really hard to understand the logic, which makes it reasonable that it does not work. – Iharob Al Asimi Apr 19 '15 at 13:52
  • You don't check for full houses or straightflushes, and you don't check for high straights T-J-Q-K-A, only the low straights A-2-3-4-5. Beyond that, it doesn't look like you included how you compute the parameters you pass into analyzeHand. By the way, you don't have to reinvent the wheel. – Douglas Zare Apr 19 '15 at 14:09
  • 1
    None of your functions except for `main` declare a return type... and for the one that does (`main`), you don't provide one. Check your compiler settings; it should have warned you of all of these. – Jongware Apr 19 '15 at 17:28

1 Answers1

0

There seems to be a problem with the logic in your main() function :

int suitInHand[SUITS], facesInHand[FACES];

analyzeHand(suitInHand, facesInHand);

You are declaring two arrays of ints without initializing them, and them you use them in your analyzeHand() function while they are empty.

You must populate those 2 arrays first if you want to get any kind of valid result.

EDIT : Depending on what kind of infos will be stored in those 2 arrays, their may be some problems with the logic of your analyzeHand() function.

Corb3nik
  • 1,177
  • 7
  • 26