Skip to main content
edited body; edited title
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

small poker Small Poker program in C

I made a small pokerPoker program with C but I believe I could've written it better.

small poker program in C

I made a small poker program with C but I believe I could've written it better.

Small Poker program in C

I made a small Poker program with C but I believe I could've written it better.

Post Reopened by Mathieu Guindon
added 408 characters in body; edited tags
Source Link
Mathieu Guindon
  • 75.1k
  • 18
  • 191
  • 463

I made a small poker program with C but I believe I could've wrotewritten it better.

the src -> https://bitbucketFull source on bitbucket.org/assyrian/c-small-poker-program/src

int checkhand (struct Card_s * hand)
{
    assert (hand) ;
    //struct Card_s *suithand = sorthand(hand, true); // Unnecessary
    struct Card_s *sorted = sorthand(hand, false);

    /* check for flushes */
    {
        bool flushsuit = false;
        int suits[4] = { [0 ... 3] = 0 };
        for (int i = 0; i < MAXHAND; ++i)
        {
            switch (hand[i].nSuit)
            {
                case spades:    suits[0]++; break;
                case clubs: suits[1]++; break;
                case hearts:    suits[2]++; break;
                case diamonds:  suits[3]++; break;
            }
        }
        for ( int f = 0 ; f < 4 ; ++f )
        {
            if (suits[f] == 5) { flushsuit = true ; } /* found a flush */
        }

        if ( flushsuit )
        {
            if ( sorted[0].nType == (sorted[1].nType-1) && sorted[1].nType == (sorted[2].nType-1) && sorted[2].nType == (sorted[3].nType-1) && sorted[3].nType == (sorted[4].nType-1) )
            {
                if ( sorted[4].nType == ace ) return 10; /* found royal flush */
                else return 9; /* found straight flush */
            }
            return 6; /* it was a normal flush */
        }
    }

    /* check for 4 of a kind */
    {
        int type [MAXTYPES ] = { [0 ... 12] = 0 }, *types ;
        types = count_types( sorted, type ) ;
        for ( int f = 0 ; f < MAXTYPES ; ++f )
        {
            if ( types[f] == 4 ) { return 8 ; } /* found 4 same types in a row */
        }
    }

    /* check for full house */
    {
        bool threeKind = false, Twopair = false ;
        int type[ MAXTYPES ] = { [0 ... 12] = 0 }, *types ;

        types = count_types( sorted, type ) ;
        for ( int f = 0 ; f < MAXTYPES ; ++f )
        {
            if ( types[f] == 3 ) { threeKind = true ; } /* found three of a kind */
        }

        int pair[MAXTYPES] = { [0 ... 12] = 0 }, *pairs ;
        pairs = count_types( sorted, pair );
        for ( int f = 0 ; f < MAXTYPES ; ++f )
        {
            if ( pairs[f] == 2 ) { Twopair = true ; } /* found normal pair */
        }
        if ( threeKind && Twopair ) return 7; /* found full house */
    }

    if ( sorted[0].nType == (sorted[1].nType-1) && sorted[1].nType == (sorted[2].nType-1) && sorted[2].nType == (sorted[3].nType-1) && sorted[3].nType == (sorted[4].nType-1) )
    {
        return 5; /* found straight */
    }

    /* check for three of a kind */
    {
        int type[ MAXTYPES ] = { [0 ... 12] = 0 }, *types ;
        types = count_types( sorted, type ) ;
        for ( int f = 0 ; f < MAXTYPES ; ++f )
        {
            if ( types[f] == 3 ) { return 4 ; } /* found three of a kind */
        }
    }

    /* check for two pair */
    {
        int type[MAXTYPES] = { [0 ... 12] = 0 }, *types ;
        types = count_types( sorted, type );
        int isTwoPair = 0 ;
        for ( int f = 0 ; f < MAXTYPES ; ++f )
        {
            if (types[f] == 2) isTwoPair++;
            if (isTwoPair == 2) { return 3 ; } /* found two pair */
        }
    }

    /* check for normal pair */
    {
        int type[MAXTYPES] = { [0 ... 12] = 0 }, *types ;
        types = count_types( sorted, type ) ;
        for ( int f = 0 ; f < MAXTYPES ; ++f )
        {
            if ( types[f] == 2 ) { return 2; } /* found normal pair */
        }
    }
 
    return 1 ; /* player only has a high card value */
}

}

I made a small poker program with C but I believe I could've wrote it better.

the src -> https://bitbucket.org/assyrian/c-small-poker-program/src

int checkhand (struct Card_s * hand)
{
assert (hand) ;
//struct Card_s *suithand = sorthand(hand, true); // Unnecessary
struct Card_s *sorted = sorthand(hand, false);

/* check for flushes */
{
    bool flushsuit = false;
    int suits[4] = { [0 ... 3] = 0 };
    for (int i = 0; i < MAXHAND; ++i)
    {
        switch (hand[i].nSuit)
        {
            case spades:    suits[0]++; break;
            case clubs: suits[1]++; break;
            case hearts:    suits[2]++; break;
            case diamonds:  suits[3]++; break;
        }
    }
    for ( int f = 0 ; f < 4 ; ++f )
    {
        if (suits[f] == 5) { flushsuit = true ; } /* found a flush */
    }

    if ( flushsuit )
    {
        if ( sorted[0].nType == (sorted[1].nType-1) && sorted[1].nType == (sorted[2].nType-1) && sorted[2].nType == (sorted[3].nType-1) && sorted[3].nType == (sorted[4].nType-1) )
        {
            if ( sorted[4].nType == ace ) return 10; /* found royal flush */
            else return 9; /* found straight flush */
        }
        return 6; /* it was a normal flush */
    }
}

/* check for 4 of a kind */
{
    int type [MAXTYPES ] = { [0 ... 12] = 0 }, *types ;
    types = count_types( sorted, type ) ;
    for ( int f = 0 ; f < MAXTYPES ; ++f )
    {
        if ( types[f] == 4 ) { return 8 ; } /* found 4 same types in a row */
    }
}

/* check for full house */
{
    bool threeKind = false, Twopair = false ;
    int type[ MAXTYPES ] = { [0 ... 12] = 0 }, *types ;

    types = count_types( sorted, type ) ;
    for ( int f = 0 ; f < MAXTYPES ; ++f )
    {
        if ( types[f] == 3 ) { threeKind = true ; } /* found three of a kind */
    }

    int pair[MAXTYPES] = { [0 ... 12] = 0 }, *pairs ;
    pairs = count_types( sorted, pair );
    for ( int f = 0 ; f < MAXTYPES ; ++f )
    {
        if ( pairs[f] == 2 ) { Twopair = true ; } /* found normal pair */
    }
    if ( threeKind && Twopair ) return 7; /* found full house */
}

if ( sorted[0].nType == (sorted[1].nType-1) && sorted[1].nType == (sorted[2].nType-1) && sorted[2].nType == (sorted[3].nType-1) && sorted[3].nType == (sorted[4].nType-1) )
{
    return 5; /* found straight */
}

/* check for three of a kind */
{
    int type[ MAXTYPES ] = { [0 ... 12] = 0 }, *types ;
    types = count_types( sorted, type ) ;
    for ( int f = 0 ; f < MAXTYPES ; ++f )
    {
        if ( types[f] == 3 ) { return 4 ; } /* found three of a kind */
    }
}

/* check for two pair */
{
    int type[MAXTYPES] = { [0 ... 12] = 0 }, *types ;
    types = count_types( sorted, type );
    int isTwoPair = 0 ;
    for ( int f = 0 ; f < MAXTYPES ; ++f )
    {
        if (types[f] == 2) isTwoPair++;
        if (isTwoPair == 2) { return 3 ; } /* found two pair */
    }
}

/* check for normal pair */
{
    int type[MAXTYPES] = { [0 ... 12] = 0 }, *types ;
    types = count_types( sorted, type ) ;
    for ( int f = 0 ; f < MAXTYPES ; ++f )
    {
        if ( types[f] == 2 ) { return 2; } /* found normal pair */
    }
}
 
return 1 ; /* player only has a high card value */

}

I made a small poker program with C but I believe I could've written it better.

Full source on bitbucket.org

int checkhand (struct Card_s * hand)
{
    assert (hand) ;
    //struct Card_s *suithand = sorthand(hand, true); // Unnecessary
    struct Card_s *sorted = sorthand(hand, false);

    /* check for flushes */
    {
        bool flushsuit = false;
        int suits[4] = { [0 ... 3] = 0 };
        for (int i = 0; i < MAXHAND; ++i)
        {
            switch (hand[i].nSuit)
            {
                case spades:    suits[0]++; break;
                case clubs: suits[1]++; break;
                case hearts:    suits[2]++; break;
                case diamonds:  suits[3]++; break;
            }
        }
        for ( int f = 0 ; f < 4 ; ++f )
        {
            if (suits[f] == 5) { flushsuit = true ; } /* found a flush */
        }

        if ( flushsuit )
        {
            if ( sorted[0].nType == (sorted[1].nType-1) && sorted[1].nType == (sorted[2].nType-1) && sorted[2].nType == (sorted[3].nType-1) && sorted[3].nType == (sorted[4].nType-1) )
            {
                if ( sorted[4].nType == ace ) return 10; /* found royal flush */
                else return 9; /* found straight flush */
            }
            return 6; /* it was a normal flush */
        }
    }

    /* check for 4 of a kind */
    {
        int type [MAXTYPES ] = { [0 ... 12] = 0 }, *types ;
        types = count_types( sorted, type ) ;
        for ( int f = 0 ; f < MAXTYPES ; ++f )
        {
            if ( types[f] == 4 ) { return 8 ; } /* found 4 same types in a row */
        }
    }

    /* check for full house */
    {
        bool threeKind = false, Twopair = false ;
        int type[ MAXTYPES ] = { [0 ... 12] = 0 }, *types ;

        types = count_types( sorted, type ) ;
        for ( int f = 0 ; f < MAXTYPES ; ++f )
        {
            if ( types[f] == 3 ) { threeKind = true ; } /* found three of a kind */
        }

        int pair[MAXTYPES] = { [0 ... 12] = 0 }, *pairs ;
        pairs = count_types( sorted, pair );
        for ( int f = 0 ; f < MAXTYPES ; ++f )
        {
            if ( pairs[f] == 2 ) { Twopair = true ; } /* found normal pair */
        }
        if ( threeKind && Twopair ) return 7; /* found full house */
    }

    if ( sorted[0].nType == (sorted[1].nType-1) && sorted[1].nType == (sorted[2].nType-1) && sorted[2].nType == (sorted[3].nType-1) && sorted[3].nType == (sorted[4].nType-1) )
    {
        return 5; /* found straight */
    }

    /* check for three of a kind */
    {
        int type[ MAXTYPES ] = { [0 ... 12] = 0 }, *types ;
        types = count_types( sorted, type ) ;
        for ( int f = 0 ; f < MAXTYPES ; ++f )
        {
            if ( types[f] == 3 ) { return 4 ; } /* found three of a kind */
        }
    }

    /* check for two pair */
    {
        int type[MAXTYPES] = { [0 ... 12] = 0 }, *types ;
        types = count_types( sorted, type );
        int isTwoPair = 0 ;
        for ( int f = 0 ; f < MAXTYPES ; ++f )
        {
            if (types[f] == 2) isTwoPair++;
            if (isTwoPair == 2) { return 3 ; } /* found two pair */
        }
    }

    /* check for normal pair */
    {
        int type[MAXTYPES] = { [0 ... 12] = 0 }, *types ;
        types = count_types( sorted, type ) ;
        for ( int f = 0 ; f < MAXTYPES ; ++f )
        {
            if ( types[f] == 2 ) { return 2; } /* found normal pair */
        }
    }
    return 1 ; /* player only has a high card value */
}
edited title
Source Link
Nergal
  • 513
  • 4
  • 11

small poker program in C, needs improving

int checkhand (struct Card_s * hand)
{
assert (hand) ;
//struct Card_s *suithand = sorthand(hand, true); // Unnecessary
struct Card_s *sorted = sorthand(hand, false);

/* check for flushes */
{
    bool flushsuit = false;
    int suits[4] = { [0 ... 3] = 0 };
    for (int i = 0; i < MAXHAND; ++i)
    {
        switch (hand[i].nSuit)
        {
            case spades:    suits[0]++; break;
            case clubs: suits[1]++; break;
            case hearts:    suits[2]++; break;
            case diamonds:  suits[3]++; break;
        }
    }
    for ( int f = 0 ; f < 4 ; ++f )
    {
        if (suits[f] == 5) { flushsuit = true ; } /* found a flush */
    }

    if ( flushsuit )
    {
        if ( sorted[0].nType == (sorted[1].nType-1) && sorted[1].nType == (sorted[2].nType-1) && sorted[2].nType == (sorted[3].nType-1) && sorted[3].nType == (sorted[4].nType-1) )
        {
            if ( sorted[4].nType == ace ) return 10; /* found royal flush */
            else return 9; /* found straight flush */
        }
        return 6; /* it was a normal flush */
    }
}

/* check for 4 of a kind */
{
    int type [MAXTYPES ] = { [0 ... 12] = 0 }, *types ;
    types = count_types( sorted, type ) ;
    for ( int f = 0 ; f < MAXTYPES ; ++f )
    {
        if ( types[f] == 4 ) { return 8 ; } /* found 4 same types in a row */
    }
}

/* check for full house */
{
    bool threeKind = false, Twopair = false ;
    int type[ MAXTYPES ] = { [0 ... 12] = 0 }, *types ;

    types = count_types( sorted, type ) ;
    for ( int f = 0 ; f < MAXTYPES ; ++f )
    {
        if ( types[f] == 3 ) { threeKind = true ; } /* found three of a kind */
    }

    int pair[MAXTYPES] = { [0 ... 12] = 0 }, *pairs ;
    pairs = count_types( sorted, pair );
    for ( int f = 0 ; f < MAXTYPES ; ++f )
    {
        if ( pairs[f] == 2 ) { Twopair = true ; } /* found normal pair */
    }
    if ( threeKind && Twopair ) return 7; /* found full house */
}

if ( sorted[0].nType == (sorted[1].nType-1) && sorted[1].nType == (sorted[2].nType-1) && sorted[2].nType == (sorted[3].nType-1) && sorted[3].nType == (sorted[4].nType-1) )
{
    return 5; /* found straight */
}

/* check for three of a kind */
{
    int type[ MAXTYPES ] = { [0 ... 12] = 0 }, *types ;
    types = count_types( sorted, type ) ;
    for ( int f = 0 ; f < MAXTYPES ; ++f )
    {
        if ( types[f] == 3 ) { return 4 ; } /* found three of a kind */
    }
}

/* check for two pair */
{
    int type[MAXTYPES] = { [0 ... 12] = 0 }, *types ;
    types = count_types( sorted, type );
    int isTwoPair = 0 ;
    for ( int f = 0 ; f < MAXTYPES ; ++f )
    {
        if (types[f] == 2) isTwoPair++;
        if (isTwoPair == 2) { return 3 ; } /* found two pair */
    }
}

/* check for normal pair */
{
    int type[MAXTYPES] = { [0 ... 12] = 0 }, *types ;
    types = count_types( sorted, type ) ;
    for ( int f = 0 ; f < MAXTYPES ; ++f )
    {
        if ( types[f] == 2 ) { return 2; } /* found normal pair */
    }
}

return 1 ; /* player only has a high card value */

}

small poker program in C, needs improving

small poker program in C

int checkhand (struct Card_s * hand)
{
assert (hand) ;
//struct Card_s *suithand = sorthand(hand, true); // Unnecessary
struct Card_s *sorted = sorthand(hand, false);

/* check for flushes */
{
    bool flushsuit = false;
    int suits[4] = { [0 ... 3] = 0 };
    for (int i = 0; i < MAXHAND; ++i)
    {
        switch (hand[i].nSuit)
        {
            case spades:    suits[0]++; break;
            case clubs: suits[1]++; break;
            case hearts:    suits[2]++; break;
            case diamonds:  suits[3]++; break;
        }
    }
    for ( int f = 0 ; f < 4 ; ++f )
    {
        if (suits[f] == 5) { flushsuit = true ; } /* found a flush */
    }

    if ( flushsuit )
    {
        if ( sorted[0].nType == (sorted[1].nType-1) && sorted[1].nType == (sorted[2].nType-1) && sorted[2].nType == (sorted[3].nType-1) && sorted[3].nType == (sorted[4].nType-1) )
        {
            if ( sorted[4].nType == ace ) return 10; /* found royal flush */
            else return 9; /* found straight flush */
        }
        return 6; /* it was a normal flush */
    }
}

/* check for 4 of a kind */
{
    int type [MAXTYPES ] = { [0 ... 12] = 0 }, *types ;
    types = count_types( sorted, type ) ;
    for ( int f = 0 ; f < MAXTYPES ; ++f )
    {
        if ( types[f] == 4 ) { return 8 ; } /* found 4 same types in a row */
    }
}

/* check for full house */
{
    bool threeKind = false, Twopair = false ;
    int type[ MAXTYPES ] = { [0 ... 12] = 0 }, *types ;

    types = count_types( sorted, type ) ;
    for ( int f = 0 ; f < MAXTYPES ; ++f )
    {
        if ( types[f] == 3 ) { threeKind = true ; } /* found three of a kind */
    }

    int pair[MAXTYPES] = { [0 ... 12] = 0 }, *pairs ;
    pairs = count_types( sorted, pair );
    for ( int f = 0 ; f < MAXTYPES ; ++f )
    {
        if ( pairs[f] == 2 ) { Twopair = true ; } /* found normal pair */
    }
    if ( threeKind && Twopair ) return 7; /* found full house */
}

if ( sorted[0].nType == (sorted[1].nType-1) && sorted[1].nType == (sorted[2].nType-1) && sorted[2].nType == (sorted[3].nType-1) && sorted[3].nType == (sorted[4].nType-1) )
{
    return 5; /* found straight */
}

/* check for three of a kind */
{
    int type[ MAXTYPES ] = { [0 ... 12] = 0 }, *types ;
    types = count_types( sorted, type ) ;
    for ( int f = 0 ; f < MAXTYPES ; ++f )
    {
        if ( types[f] == 3 ) { return 4 ; } /* found three of a kind */
    }
}

/* check for two pair */
{
    int type[MAXTYPES] = { [0 ... 12] = 0 }, *types ;
    types = count_types( sorted, type );
    int isTwoPair = 0 ;
    for ( int f = 0 ; f < MAXTYPES ; ++f )
    {
        if (types[f] == 2) isTwoPair++;
        if (isTwoPair == 2) { return 3 ; } /* found two pair */
    }
}

/* check for normal pair */
{
    int type[MAXTYPES] = { [0 ... 12] = 0 }, *types ;
    types = count_types( sorted, type ) ;
    for ( int f = 0 ; f < MAXTYPES ; ++f )
    {
        if ( types[f] == 2 ) { return 2; } /* found normal pair */
    }
}

return 1 ; /* player only has a high card value */

}

Post Closed as "Not suitable for this site" by janos
Source Link
Nergal
  • 513
  • 4
  • 11
Loading