i'm almost done with my video poker game. i need help finding the if the winning hand is a straight(a straight is five continous cards e.g. 3,4,5,6,7, ive made J,Q,K,A 11-14) HERE'S

A CODE SNIPPET:

int[] arraydeck = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13","14"};
string[] arraysuit = { "♠ ", "♦ ", "♣ ", "♥ " };

 hasstraight(arraydeal);

//I PLACED RANDOM CARD GENERATOR CODE HERE

 void hasstraight(int[] straight)
        {
            txtHand.Text = "";
            int[] straight2 = new int[straight.Length];

 for (int i = 0; i < straight2.Length; i++)
                {
                    Array.Sort(straight2);
                    //txtHand.Text += straight2[i].ToString() + ", ";
                }
                int recrank = straight2[0];
               
                for (int i = 0; i < straight2.Length; i++)
                {
                    int recrank2 = straight2[i];
                    //textBox1.Text += recrank2.ToString();
                    if (recrank != recrank2++)
                    {
                        txtStraight.Text= " ";
                    }
                    else
                    {
                        txtStraight.Text = "STRAIGHT!";
                      
                    }
                    recrank++;

                }
}

this works on (4,5,6,7,8) but unfortunately it also calls (4,5,7,7,8) a straight. Also, how do i move the form around? When i drag the form title it does not move (i'm new to c#)

thanks.

Edited 6 Years Ago by __avd: Added [code] tags.

balrogf,

the underlying issue here is two fold. One is the way that you are updating your variables recrank and recrank2, recrank may work okay with the ++ method, since a straight is more or les i, i++, (i++)++ ... etc, but recrank2 has to start on a different number number than recrank, which id does not. Start your for loop with i = 1; and you should have a functioning algorithm to compare array[0] to array[1].

The other issue is that there is no exit structure in your for loop. I would image in if you printed out

if (recrank != recrank2++)
{
txtStraight.Text= " ";
}
else
{
txtStraight.Text = "STRAIGHT!";

}

each time you iterated through, most of the time it would print "". However, in the final case, the conditions make it "STRAIGHT!". What you should look at doing is using a while loop and a break instead, or add logic, such as a boolean, to make sure you are keeping track of a continuous straight. IE:

bool straight = true;

for (int i = 1; i < straight2.Length; i++)
{
  if(straight)
  {
    int recrank2 = straight2[i];
   
    if (recrank != recrank2++)
       straight = true;
    else
      straight = false;

    recrank = recrank2;
  }
}

if(striaght)
  txtStraight.Text = "STRAIGHT!";
else
  txtStraight.Text = "";

Hope that helps!

Edited 6 Years Ago by croker10: n/a

croker10,

thanks for the reply, your code makes sense but for some reason it doesnt work. It jumps to straight = false the second loop(i=2). given straight2 {4,5,6,7,8}

maybe im just tired. ill try again later.

Indeed it will not, I'm not sure what I was thinking when I wrote that, the test statement should be

if ( (recrank + 1) == recrank2)
  straight = true;
else
  straight = false;

as opposed to

if (recrank != recrank2++)
  straight = true;
else
  straight = false;

Sorry for the flawed logic on that last post.

This question has already been answered. Start a new discussion instead.