So my professor decided to be clever and made up a hand called an EVEN STRAIGHT for my Poker project. It's just like a straight except the cards need to be consecutive even numbers. For example- 8,6,2,4,10 is an even straight. Also, an ace (value of 1) can either be used as a "high end" or a "low end," meaning if it can either be 1 or 14 depending on the other cards. I'm having trouble sorting the value of the cards in an array in ascending order because we can't use the .sort method in this project. Can someone help me? Also it's an introductory Java class so please try to make it as simple as possible. Thanks. This is what I have so far....
//can assume only 5 cards are being passed in as parameter.
public static boolean hasEvenStraight(Card [] cards) {
boolean evenCard = false;
int [] value = new int[cards.length];
for(int i = 0; i<cards.length; i++){
Card myCard = cards[i];
value[i] = myCard.getValue();
if(value[i]%2 == 0){
evenCard = true;
}
else
evenCard = false;
}
if(evenCard){
//This is where I am stuck
}
return false;
}
First, there is a logical error in your code. In your
for
loop, you are checking whether the current card is even or odd. But when the loop finishes, what you are left with is whether the last card you looked at. You need to check whether they are all even.This loop, if it finds a card that's not even, it immediately returns false, because even one odd card means you don't have an even straight. So after it finishes you know that you have all evens.
But this, of course, is not enough. You want to know whether the cards are consecutive. Now here is the trick: You don't actually have to save all the cards values in that
value
array. You need to keep only the highest one and the lowest one. After you finish the loop, if you know that they were all even, and that your highest - your lowest = 8, it means you have consecutive evens, that is, an even straight.Why? Because if they weren't consecutive, then there's a missing even in the middle, right? But if the lowest is 8 less than the top, there is no way you can push 3 cards between them so that they would all be even.
But we need to be watchful for cards of the same number, such as 2 of spades and 2 of hearts. They will ruin this principle. And if they exist, it's not an even straight anyway.
To check this, we have to keep a flag for each card value that we processed, saying "did we already process this card value"? We could use something like a
Set
for this. Upon examining each number, we ask: "Is this number in the set of numbers we have already checked?". If so, then of course, this is not an even straight. If not, we add the current number to the set, so that the following numbers can be checked against it.For a range of small
integers
, we can do this without an actual JavaSet
. We use an array of booleans calledalreadyThere
or something like that. The element at indexi
means "Have we checked the card whose value isi
". It's a bit wasteful of space because we'll never use the odd indexes or the zero and one indexes, but it's very easy to implement. Just check ifalreadyThere[cardValue]
istrue
. And of course, after we checked and it is a unique number, we setalreadyThere[cardValue]
totrue
for the next iterations's check.So let's modify your method: