Pro Java 9 Games Development Leveraging the JavaFX APIs

(Michael S) #1
Chapter 5 ■ a Java primer: introduCtion to Java ConCepts and prinCiples

create a type of evaluation structure where things are evaluated in precisely the order and in the fashion that
you want them to be evaluated.
Let’s start by looking at the Java switch statement, which uses the Java switch keyword and an
expression at the top of this decision tree. Inside of the decision tree the switch construct uses a Java case
keyword to provide Java statement blocks for each outcome for the switch statement expression’s evaluation.
If none of these cases inside the switch statement’s structure (that is, inside the curly {} braces) is used by the
expression evaluation, you can provide a Java default keyword and a Java statement code block for what you
want done if none of these cases is invoked.
The general format for your switch-case decision tree programming construct would look like the
following:


switch(expression) {
case value1 :
programming statement one;
programming statement two;
break;
case value2 :
programming statement one;
programming statement two;
break;
default :
programming statement one;
programming statement two;
}


The variable used in case statements can be one of five Java data types: char (character), byte, short,
string, or int (integer). You will generally want to provide the Java break keyword, at the end of each of your
case statement code blocks, at least in the use case where the values being switched between need to be
“exclusive” and only one is viable (or permissible) for each invocation of the switch statement.
The default statement does not need to use any break keyword.
If you do not provide a Java break keyword in each of your case logic blocks, more than one case
statement can be evaluated, in the same pass, through your switch statement. This would be done as your
expression evaluation tree progresses from top (the first case code block) to bottom (last case code block or
default keyword code block).
The significance of this is that you can create some fairly complex decision trees based upon case
statement evaluation order and whether you put the break keyword at the end of any given case statement’s
code block.
Let’s say you want to have a decision in your game as to what GamePiece moving animation is called
when the GamePiece is moved (walk, jump, dance, etc.). The GamePiece animation routine (method) would
be called based on what the GamePiece is doing when he (or she) is moved, such as Walking (W), Jumping
(J), Dancing (D), or Idle (I). Let’s say these “states” are held in the data field called gpState of type char that
holds a single character. Your switch-case code construct for using these game piece state indicators to call a
correct method, once a turn has been taken, and movement needs to occur. This should look something like
the following Java pseudocode (prototyping code):


switch(gpState) { // Evaluate gpState char, execute case code blocks accordingly
case 'W' :
gamePieceWalking(); // Java method controlling Walk sequence if GamePiece is walking
break;
case 'J' :
gamePieceJumping(); // Java method controlling Jump sequence if GamePiece is jumping
break;

Free download pdf