I'm attempting to make a flush in a classic poker/card game type program. The property is the suit (hearts, diamonds, etc) and the array is the hand. I used the IEquatable and implemented the Equals method
public bool Equals(SuperCard otherCard)
{
if (otherCard == null)
return false;
return (this.cardSuit.Equals(otherCard.cardSuit));
}
from there in my Program.cs I am writing a bool method for Flush, using the .Equals. it was suggested to me that I use a for loop, but I'm having trouble understanding if I'm doing this right. I need to compare the cardSuit property, card by card. But I'm unsure of how to go about it. Any help would be appreciated. Here's what I have in the method so far.
private static bool Flush(SuperCard[] hand)
{
for (int i = 0; i < hand.Length; i++)
{
if (hand[i].Equals(hand[i + 1]) == false)
{
return false;
}
}
return hand[hand.Length].Equals(hand[hand.Length - 1]);
}
in my mind, the for loop compares each card looking for any false's, then returns false if so. outside/after the for loop (supposing theyre all true), I return the true/false of the comparison of the last 2 cards in the the hand. am i over complicating it? is it wrong?
EDIT: I can see this: "if (hand[i].Equals(hand[i + 1]) == false)" will go out of bounds exception, so I need a new way of comparing card to card. Any ideas?
First, beware of (possible) stack overflow:
Another issue is RangeCheckError (see at "Length - 1" in the loop condition)