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.

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!

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.

i get it now, thank you kindly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.