Friday, 9 December 2011

Java: Texas Hold Em (16) - HandEvaluator Finished.

Okay, so, today we finish this class up, finally. That's the good news. The bad news is, there's still a HandValue class to get through, which will put together the HandValueType and the HandEvaluator, so we're not totally out of the woods.

The HandValue class should be finished in 2-3 days though, so it's not so bad. Enough talk of the future, let's get done with the now!

First on the menu is to finish our boolean methods with isRoyalFlush. Since this is just a straight flush with Ace-High, we won't have much trouble, just as long as both straight and flush ranks are aces, like so:

Lines 274-275: If the rank of the straight and the rank of the flush are both Aces...
Lines 276-280: And then right back into what we've seen with everything else. Set the types, return true or false.

See? Not so bad. All that's left at this point is the constructor, which we want to do all the work for us, so it's a little lengthy. I included the getType method at the bottom. Ignore this, we already did it in...part 10? I just missed it when cropping the screenshot, and I'm lazy, so it stays!

Line 36: We need to know the cards we're dealing with if we want to evaluate them, so we need to extract the cards from the given hand.
Lines 38-41: Here's where we use all of our earlier methods to fill in details in the class variables, such as ranks of pairs or straights or whatever. Refer back to the methods if you don't know what's going on here!
Lines 43-52: Whoa, this looks like quite a bit more than it really is. Here, we use all our boolean methods that we just spent 4 or so days on, and ask the computer if the hand contains any potential hand.
Lines 53-54: If NOT, just calculate the hand's high card for use, because that's all you've got left!

Lines 57-58: Here's the kinda tricky part, and might take a bit to explain ("If you can't explain something simply, you don't understand it yourself" - Some Guy, Paraphrased), even though I sort of get it.

Okay, you remember the ranking factors? That was the array that was as long as the numRanking int and contained powers of 13. So iterating over each power of 13, we go through the ranking array.

Remember when we put values in the ranking array in all our boolean methods? The hand value type was ALWAYS position 0? That's because the hand value type is way more important. A straight from 2-6 beats a high card of kings. To make sure this carried through in the scoring, we needed a higher power to work with, otherwise the score from a high king (rankings = [0, 10 (I think)....] ) would beat a pair of 2's ([1, 0...]) when added up.

So instead we make sure that each part of the rankings array is multiplied by the next highest power of 13 to the next part in the ranking array. ( 1*(13^1) ) > (0*1), meaning the pair of 2's will always beat a single ace high when added together.

Did I make that clear enough? I have no idea. I'\m horrendous at explanations, but I do hope you at least get the gist, now. I didn't at first, and if I were doing this from scratch, my scoring system would be ass. So anyway, Line 58 is saying add to value (our final score): whatever the ranking array number is * the required power of 13, which gives us the hand score.

Boom, hand evaluated! I think it's time I got some sleep now, see you tomorrow! Questions/comments welcome.


  1. Beasted this one, bring on the HandValue!

  2. "Did I make that clear enough?"

    You're doing it great, my way to coding-tortoise will start in a few months from now and I'm blessing you everyday for the step by step lessons :D