Saturday, 12 November 2011

Java: Texas Hold Em (3)

Hi, today, we're going to (I think) finish off the Deck class, for happy fun times. Might need to change the toString() of Card since it looks kinda ugly, BUT it does work, as does the shuffling., so yeah. It's okay, though, I suppose, since the end result will hopefully be graphically represented.

Here's the Deck beyond the constructor:



Alright, part 2 of 3 of the deck class! Let's work our way down the screenshot:
Lines 36-44: A method to shuffle the deck.
    Line 37: This iterates over each card in the deck
    Line 38: This sets the random position of the card to be switched with the current iteration.
    Line 39: This'll temporarily hold the value of the current card being iterated over so we can
        flip it around with the randomized position we just calculated
    Lines 40-41: This actually flips the cards around, using the temporary card.
    Line 43: This just sets the next card to 0. Remember from the constructor this determines
        where we are in the deck, so it just makes sure that when we deal, we're doing so from
        the start of our newly shuffled deck.

Lines 47-49: This just resets the current deck. It doesn't re-order the cards, it just starts the
    dealing index over. Useful if you want to restart the current game or something, I guess.

Lines 52-57: This is the deal() method. One of the deal methods, that is. This one will just
    deal a single card. It could be condensed to one line, but let's have a little error checking
    to make it look more professional (looking).

Phew! Still got another screenshot to go, since this post is already very late, I figure wasting a bit more time can't hurt too much. Besides, I'd like to get the deck more or less done tonight :D



I wasn't sure whether to use a List<Card> or Card[] for this next method. It's a deal method, but deals a set number of cards. I ended up with using the array, just 'cause it's a simpler object type. Would probably be easier to add values to a list, but whatever.

Line 59-71: The deal method in it's entirety. Half of it it just minor error checking.
    Lines 60-62: This is just if for some reason the method is called with 0 (or fewer) cards
        asking to be dealt. Shouldn't really be a problem, though.
    Lines 63-65: This is if you try to deal more cards than are left in the deck. Also
        problematic, so errors ahoy!
    Line 66: Finally getting to the meat of the method. Here we just create our array with as
        many spaces as we need.
    Line 67-69: Here is where we actually populate our array. It occurs to me right now that
        we could have used the previous deal method on line 68, but I'll fix that later. Either way,
        it iterates over the array and adds a card from the deck to it.
    Line 70: This just returns our array. You should know that by now, though :P

Next method it just a string representation of the deck. The way I have it now puts one card on each line. Probably not optimal. Also, the last element will still end with a comma of its own. But, again, I'm not too bothered by this, and can fix it later.

So that's it. The deck is done, and tomorrow I'm off. See you then, I'm tired now!

2 comments:

  1. I'd be tired too if I was messing around with how nice my coding looks. Who cares as long as it works!

    Then again, maybe that's why you're the one with the coding blog. :P

    ReplyDelete