Friday 30 September 2011

Java: Enumerated Types ( The Table )


Let's dive right into it, tonight! This is what I have for my table class:






Okay, this isn't all of it, but there's some new info in this chunk! Let's start with the enumeration. We have us our positions, but to ensure we have the right rotation later on, instead of having a buttload of conditional statements (if current position is south, next position is west, if current position is west....), we can just set them here with a function, using the return type of the enumeration. Oh god, what's this outlined in red?!? Well, that's (as you can see) an abstract method.


Recall that when we used functions on enumerated types previously, we had to specify which enumeration we were using. Without abstract, we'd still be there, and we'd have to specify everywhere, position.NORTH.next() or whatever. With this abstract, as long as we have a variable of type Position, we can use next() directly on it.


Position p = Position.SOUTH;
p.next();


^ Becomes possible, instead of:


p.SOUTH.next();


Next, we need to set an array to hold all our players, as well as tell the game where to place our dealer. A new pack is just a given.


The method after that is to actually place the players at the table. An array of null-types isn't much use, so we have to actually initialize the players. This will give the player a name and a position on the table. The method I use within, .ordinal(), is build into the enumerated types. It returns a numerical value of where in the enumeration the (in this case) position is.


Position p = Position.NORTH;
System.out.println( p.ordinal() );


....would return 0 (Remember, programmers count from 0!). We use this since the array needs a numerical position to set the players at.


Now we get into the old stuff again! Dealing the cards. First, we need to shuffle the deck, at which point the dealing position is set to start from the left of the dealer. The for loop is a little messy. For every card, pass one to the current player, then move to the next player. We're almost done! We just need a toString() method for convenience.






We just have a stringbuilder here,  and it creates a string for us with the players position, followed by more stuff.
The line:


p == dealer ? "* " : "  "


is a compressed conditional statement. It's saying: "Is p (current position) the dealer? If so, give me an asterisk. Otherwise, give me a space". In regular if-statements, this could be represented as:


if ( p == dealer ) {
    return "* ";
} else {
    return "  ";
}


And uh....that's it! Now all we need to do is create a class to join everything together and actually get to dealing cards. Or, what I did was put that functionality within the main method of table, but you should have a main class to actually do things with, normally. I'm just lazy. This you can do on your own, whichever way you decide to do it. Just create an instance of a Table, make sure to seat your players, then deal the cards. At this point, you can print the table and you can see the players' hands.


Fucking...finally. For the next couple of days, I'll be doing some quick review from start to here, very compressed, but just as a recap. If you've got any specific questions, let me know, otherwise:


That's it for now..Questions welcome! Comment, follow, subscribe, share etc, and see you tomorrow!


    And as part of a shameless plug for a friend, if you're interested in classic movies/books/music, visit his site here (fixed), and feel free to throw loads of criticism at us. 

5 comments:

  1. Great stuff, saving this once again as I'm taking a break from learning, so I won't do this. Good job though, keep it up!.

    ReplyDelete
  2. Really well explained, even for some1 like me (not much experience in Java haha).

    ReplyDelete
  3. I'm pretty sure there will be one day where i'm gonna need this and i will have to come back and read every post again.

    ReplyDelete