28.04.2019 Views

[JAVA][Beginning Java 8 Games Development]

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 3 ■ A <strong>Java</strong> 8 Primer: An Introduction to <strong>Java</strong> 8 Concepts and Principles<br />

(I). Let’s say these states are held in a data field called ibState, of the type char, which holds a single character. The<br />

switch-case code construct for using these game-piece state indicators to call the correct method, once a hit has<br />

occurred, would be:<br />

switch(ibState) {<br />

// Evaluate ibState char and execute case code blocks accordingly<br />

case 'F' :<br />

deathLogicFlying(); // <strong>Java</strong> method controlling death sequence if InvinciBagel flying<br />

break;<br />

case 'J' :<br />

deathLogicJumping(); // <strong>Java</strong> method controlling death sequence if InvinciBagel jumping<br />

break;<br />

case 'R' :<br />

deathLogicRunning(); // <strong>Java</strong> method controlling death sequence if InvinciBagel running<br />

break;<br />

default :<br />

deathLogicIdle(); // <strong>Java</strong> method controlling death sequence if InvinciBagel is idle<br />

This switch-case logic construct evaluates the ibState char variable inside the evaluation portion of the switch()<br />

statement (note that this is a <strong>Java</strong> method) and then provides a case logic block for each of the game-piece states<br />

(flying, jumping, running) and a default logic block for the idle state (which is a logical way to set this up).<br />

Because a game piece cannot be idle, running, flying, and jumping at the same time, you need to use the break<br />

keyword to make each of the branches of this decision tree unique (exclusive).<br />

The switch-case decision-making construct is generally considered more efficient, and faster, than the if-else<br />

decision-making structure, which can use just the if keyword for simple evaluations, like this:<br />

if(expression = true) {<br />

programming statement one;<br />

programming statement two;<br />

}<br />

You can also add an else keyword to make this decision-making structure evaluate statements that would need to<br />

execute if the boolean variable (true or false condition) evaluates to false rather than true, which makes this structure<br />

more powerful (and useful). This general programming construct would then look like this:<br />

if(expression = true) {<br />

programming statement one;<br />

programming statement two;<br />

} else { // Execute this code block if (expression = false)<br />

programming statement one;<br />

programming statement two;<br />

}<br />

In addition, you can nest if-else structures, thereby creating if{}-{else if}-{else if}-else{} structures. If these<br />

structures get nested too deeply, then you would want to switch (no pun intended) over to the switch-case structure,<br />

which will become more and more efficient, relative to a nested if-case structure, the deeper the if-else nesting goes.<br />

For example, the switch-case statement that you coded earlier for the InvinciBagel game, if translated into a nested<br />

if-else decision-making construct, would look like the following <strong>Java</strong> programming structure:<br />

if(ibState = 'F') {<br />

deathLogicFlying();<br />

} else if(ibState = 'J') {<br />

deathLogicJumping();<br />

67

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!