0

I'm going crazy. I'm writing a source that selects 5 poker cards from a shuffled deck and recognizes if there is a couple, double couple, tris and so on. However, the functions that deals with finding a Straight is giving me a hard time. I had to rewrite this because i haven't considered before the fact that the Ace can value 1 as well as 11. This is the source:

int hasStraight(const int wFirstFace[],int size)
{
    int *firstfacePtr,i,j;
    firstfacePtr=wFirstFace;

    for(i=0;i<=1;i++)
    {
        for(j=0;j<=size-2;j++)
        {
            if (firstfacePtr[j]!=firstfacePtr[j+1]-1 && i==0)
                break;
            else if (firstfacePtr[j]!=firstfacePtr[j+1]-1 && i==1)
                return 0;
            else if (j==3 &&firstfacePtr[j]==firstfacePtr[j+1]-1)
                printf("Scala!\n");
                return 1;
        }
        if (firstfacePtr[0]==0)
            firstfacePtr[0]=13;
            bubble(firstfacePtr,size); /*bubble goes through an array 
                                         and put it in ascending order*/
    }

}

Notes: wFirstFace is an array containing 5 integers; size is an integer, and it's equal to 5. I have tried to declare wFirstFace={0,1,2,3,4} in order to check if the code worked or not. Then, the internal 'for' cycle ends after one cycle, and the function returns a 0 and I don't even know why or where. Where did I make a mistake in this messy source code?

Ken White
  • 123,280
  • 14
  • 225
  • 444
user1068051
  • 217
  • 1
  • 4
  • 12
  • 1
    Did you retype this here? Please copy and paste when possible. Looks like you're missing braces on that last "else if". Otherwise, return 1 will always happen. – Bob Kaufman Dec 29 '11 at 21:24
  • 1
    Learn how to use a debugger: you aren't going to get very far without it anyway. – zvrba Dec 29 '11 at 21:23

1 Answers1

2

There are couple of braces mismatch I could see.

1 - This will always return 1;. Do you want to put a { } for the last else if?

    for(j=0;j<=size-2;j++)
    {
        if (firstfacePtr[j]!=firstfacePtr[j+1]-1 && i==0)
            break;
        else if (firstfacePtr[j]!=firstfacePtr[j+1]-1 && i==1)
            return 0;
        else if (j==3 &&firstfacePtr[j]==firstfacePtr[j+1]-1)
            printf("Scala!\n");
            return 1;
    }

2 - Do you want the bubble() to execute for all the iterations of for(i= loop? or only when if (firstfacePtr[0]==0) is true?

    if (firstfacePtr[0]==0)
        firstfacePtr[0]=13;
        bubble(firstfacePtr,size); /*bubble goes through an array and put it in ascending order*/
Sangeeth Saravanaraj
  • 16,027
  • 21
  • 69
  • 98
  • @user1068051 If you find the above answer useful and information and helped you solve your problem, please confirm so by clicking the tick mark next to it. Thanks! – Sangeeth Saravanaraj Dec 30 '11 at 04:23