Pro Java 9 Games Development Leveraging the JavaFX APIs

(Michael S) #1

Chapter 19 ■ Game Content enGine: ai LoGiC with random Content SeLeCtion methodS


Next, open your empty calculateQuadrantLanding and add the quadrantLanding variable and an
equals sign to set up the equation we are about to define on the right side of the equals operator.
Since the spinDeg accumulator is what we’re going to break down into full spins plus the quarter spin
offset, type the spinDeg variable next, which will always hold an accumulated “record” of every spin the
players have made.
To find the latest quadrant that has been landed on, simply remove all full spins from this accumulated
total value by dividing it by 360 (the number of degrees in one full rotation) to keep (extract) just the
incremental amount beyond the full spins, which will indicate the quadrant that the latest spin has landed
the player on.
Fortunately, the Java language has an operator called a remainder operator that will do exactly this
for you, saving you from having to construct any complex equations. This remainder operator uses a %
(percentage) sign after a variable that you want to extract the remainder from, and after the % sign goes the
number you want to divide into the (in this case, accumulator) variable, which in this case is the number of
degrees in a full rotation (360). If you use pseudocode, this would be TotalSpinDegreesAccumulated %
OneFullSpin = DegreesRemaining. The Java code for your calculateQuadrantLanding() method should
look like the following and is shown at the bottom of Figure 19-3:


private void calculateQuadrantLanding() {
quadrantLanding = spinDeg % 360; // Remainder of spinDeg accumulator after all 360 spins
System.out.println(quadrantLanding); // Print Angle Offset to Output Pane for Debugging Use
}


Figure 19-2. Add a spinDeg accumulator to your spinner UI mouse click conditional if() logic to track where
the quadrant is

Free download pdf