Thread: Checking for a straight (poker) in C++?

  1. #1
    Registered User Grae's Avatar
    Join Date
    Sep 2013
    Posts
    20

    Question Checking for a straight (poker) in C++?

    Hi, I'm making a poker game where the user is provided with 5 cards and these cards are given a value. Unfortunately I've been having errors when the program checks for a straight. To check for a straight the program orders the card numbers in ascending order, checks if 5 of them are in chronological order and then gives them a value of 54 if they are.
    For some reason cards which are not in chronological order return the value of 54.

    Each card is valued from 2 - 14 (2 being 2, and 14 being an ace, that is why had to add
    Code:
    maxnum == 14 && minnum == 2 == (mminnum - 1) == (midnum - 2) == (mmaxnum - 3
    )
    Here is the full check:
    Code:
        //CHECK FOR STRAIGHT
        int minnum;
        int mminnum;
        int midnum;
        int mmaxnum;
        int maxnum;
        int noStraight = 0;
        for (int i = 0; i < 15; i++)
        {
            if (i == card1num || i == card2num || i == card3num || i == card4num || i == card5num)
            {
                minnum = i;
                break;
            }
        }
        for (int i = 0; i < 16; i++)
        {
            if (i == card1num || i == card2num || i == card3num || i == card4num || i == card5num && i != minnum)
            {
                mminnum = i;
                break;
            }
            if (i == 15)
            {
                noStraight = 1;
            }
        }
        for (int i = 0; i < 16; i++)
        {
            if (i == card1num || i == card2num || i == card3num || i == card4num || i == card5num && i != mminnum && noStraight == 0)
            {
                midnum = i;
                break;
            }
            if (i == 15)
            {
                noStraight = 1;
            }
        }
        for (int i = 0; i < 16; i++)
        {
            if (i == card1num || i == card2num || i == card3num || i == card4num || i == card5num && i != midnum && noStraight == 0)
            {
                mmaxnum = i;
                break;
            }
            if (i == 15)
            {
                noStraight = 1;
            }
        }
         for (int i = 0; i < 16; i++)
        {
            if (i == card1num || i == card2num || i == card3num || i == card4num || i == card5num && i != mmaxnum && noStraight == 0)
            {
                maxnum = i;
                break;
            }
            if (i == 15)
            {
                noStraight = 1;
            }
        }
    
    
        if (minnum == (mminnum - 1) == (midnum - 2) == (mmaxnum - 3) == (maxnum - 4) || maxnum == 14 && minnum == 2 == (mminnum - 1) == (midnum - 2) == (mmaxnum - 3))
        {
            cardsval = 54;
        }
    Could someone please tell me what I have done wrong here (why some card numbers not in chronological order are being counted as a straight?

    If possible please don't tell me to use any tools which I have not already used in this program (arrays, pointers etc) as I am getting through a book on C++ and this is one of the challenges at the end of a chapter (you are expected to be able to write the program using only the knowledge of C++ which I already have).

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > if (i == card1num || i == card2num || i == card3num || i == card4num || i == card5num && i != minnum)
    The logic on this reads as
    if (i == card1num || i == card2num || i == card3num || i == card4num || ( i == card5num && i != minnum ) )

    This might not be what you want.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User Grae's Avatar
    Join Date
    Sep 2013
    Posts
    20
    thanks for the reply but no that does not fix the problem

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Since I don't have time to guess which combination of cards which are NOT a straight, but manage to get through your code as being a straight, perhaps you could post that information.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Finding straight draws in poker
    By n1mda in forum C Programming
    Replies: 1
    Last Post: 03-19-2010, 11:56 AM
  2. Finding a 'straight' in poker.
    By esbo in forum C Programming
    Replies: 58
    Last Post: 01-18-2008, 05:16 AM
  3. so let me get this straight...
    By Sea Monster in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 01-11-2005, 02:48 AM
  4. Let's Get Something Straight
    By Troll_King in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 10-18-2002, 02:09 AM

Tags for this Thread