Friday, 11 November 2011

Java: Texas Hold Em (2)

I COULD find a fully working game that's open source (probably), but that would be too easy! I did, however, decide to add the comparison method to Card, since that means we can compare them to decide which hand is worth more (if they're both the same hand. Triples or something), which we definitely need!

An important thing to note is the order in which I did the enumerations yesterday. I wrote them out in order from lowest to highest, both for Rank and Suit. The suit one was frankly luck, but apparently the ranking of those from bottom to top is:


This is important since a method we've used before which you might remember is .ordinal(), which returns the value of the enumeration in the order it was given. So ACE is worth 12, and thus trumps everything else!

So tack this baby onto the end of the Card class:

Cool. Mull that over for a bit, 'cause I'm just about to explain it!

Lines 58-59: Get's the rating of the card's Rank and Suit (Suit plays a part in determining value of the card if both have the same Rank, apparently. I never knew this before, but there you go).
Lines 61-62: Same thing, but with the Card being compared to!
Note we're using the ordinal, this means it's important as fuck that we got them in the right order in the enumeration! If you copied the enumerations from yesterday's post, you're golden.

Lines 64-71: This is our actual comparison!
    Lines 64-65: Simple enough, if this card's rank is higher, return 1
    NOTE: In comparison, returning 1 means this item is worth more, -1 means less and 0 means equality.
    Lines 66-67: Not getting any harder! If this card's rank is lower, give us a -1
    Lines 68-69: This will only be reached if the ranks are the same. But if the suit of this card is worth more, let us know with a 1
    Line 70: This doesn't need to explicitly state if the rank is worth less, since that's the only option available at this point, so if the statement has reached this far, we're just going to return -1.

Note there's no return 0, since that implies 2 cards have the exact same value. That would require the same rank AND suit though, and that doesn't happen in a deck of cards!

Well, now that we have our cards complete, we should start on building a deck out of them. I'll only go as far as the constructor today:

This is just the beginning of our deck, so bear with me if it (rightfully) seems incomplete! Before the constructor itself is up, we need a couple of variables, starting with something to hold the number of cards in the deck. This isn't really required, but it makes it look nicer.

Most importantly, we'll need an array of Cards. I've also go t a variable to hold the current position in the deck, for dealing purposes, and a Random, which will be used for shuffling, as you might imagine!

The constructor itself isn't as daunting as it looks. Damn comments inflate it.
Line 19: This is where we give our array a size. Go us.
Line 20: This will hold the current position of our card placement, we don't want to keep putting cards on the same cell, that'll end up with us having a deck of nothing but 1 card. Not great.

Now we come to the interesting part. Populating the deck. Since we want to iterate over every suit AND rank, we're going to need nested iterators (for loops, in this case). Obligatory Inception joke. It doesn't really matter which one is nested in the other, it just determines how the deck is initially populated.

Lines 23-25: Read (sort of): For each rank in each suit....
Lines 26-27: Get me the rank and suit based on which iteration we're on...
Line 30: Then make me a card of them, and place it in the deck!

That's plenty long for today, I'm off to sleep now! See ya tomorrow!