Thursday, 17 November 2011

Java: Texas Hold Em (5)

Uh, so it's now Friday. I have no idea what happened. I apparently passed out or something, and had been down and out for a full day. I am a bastion of physical health, I know. So after a while at the doctor it looks like the verdict is "Man up, bitch", so to celebrate, I'm going to write more code. And also mention here that I'll be dropping my posts to 5/week instead of 6!

So we were on the Hand class, and here`s how the rest of that goes:


First off, I have no idea why I used a Collection<Card> instead of an array for the constructor, so I've changed that. Was just blindly following some code I saw and didn't think about how it would be used in this specific program.
Next, we should have 2 methods to add cards. One, which we'll use in the constructor, which will add all cards in the array, and another, for after construction, this will only add 1 card to the hand.


Here's the single card adder thing. You should be able to read most/all of the code I post unless I make a special note of it (Java libraries are HUGE, no one can know what everything does), but let's go through it anyway!

Lines 42-44: This is just error checking.
Lines 46-52: Since we want the hand to remain ordered, in the interest of easier score-keeping on the computer's part, we need this whole hullabaloo to keep track of where exactly we're inserting our card. We have our variable holding the position to put the card, then we have a statement that iterates over every card in the Hand, using the compare method. If a suitable spot is found, we tell the computer that's our insertion index, and then break out of the loop.
Lines 53-62: This little if-statement first checks to see that we have an actual insertion index. If not, we'll place the card at the end of the Hand.
OTHERWISE, since we're using the basic arrays instead of higher-level Lists with proper insertion (too much to change now, so I'm keeping it that way! You can change it if you want, its not hard!), we need to iterate over the array, backwards and place every card ahead by one spot. This'll free up a spot in the middle where we can finally place our card. When done, don't forget to tell the computer the hand has increased in size!


Here, we've got 2 methods in one, goodie! First off, we've got a method to add multiple cards, taking an array of cards...then adding them. Most of that method is error checking, and the meat of it is in Lines 72-73, which makes use of the method we just had, and applies it to each card in the array.

Our second method is a getter, so the cards can be retrieved without actually allowing the viewer to modify them. NOTE: This is one of them new fangled methods we've not talked about before. arraycopy. It takes 5 parameters: Original Array, starting position, destination array, starting position and finally, size. Easy.I


Last up for the Hand class, we've got a remove method, just setting the number of cards in the Hand to 0, and then a toString using the ever-popular StringBuilder, which is fairly straightforward, we've had one of these quite often.


So that's it for tonight, and tomorrow/Sunday are off, so I'll see you on Monday! Time to catch up on missed blogs!

3 comments:

  1. Oh no, how'd I already get this far behind... going back and reading the first parts now.

    ReplyDelete
  2. That's fine, I know what you mean about poor health.

    Take as much time as you need to recover and post what's manageable for you.

    ReplyDelete