Pro Java 9 Games Development Leveraging the JavaFX APIs

(Michael S) #1

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


The quadrantLanding variable will always contain one of four values: 45 (pink or other quadrant), 135
(blue or mineral quadrant), 225 (green or vegetable quadrant), or 315 (orange or animal quadrant). We’ll
create a method called calculateQuadrantLanding() that we will call at the end of the MouseEvent handler
that implements a random spin.


Implementing Spin Tracker Functionality: Create Empty Methods


Let’s create the two integer variables and five new methods’ infrastructure that we will need to hold the Java
code we will be writing during this chapter. This code will track what quadrant the game board is resting
on after each spin and will then handle the “population” of the game board squares with random content,
which we partially (25 percent) developed in the previous chapter. I will use the quadrant 1 (orange) content
to test the logic we are crafting during this chapter as I have not yet created the hundreds of image assets (six
per game board square, or 120 to start) that will be needed to develop the initial code. More assets can be
added later simply by incrementing the random number generator’s upper bounds value and updating the
populateQuadrant() method’s logic. What we will be doing in this chapter will amount to a couple hundred
lines of code nevertheless, so we will be making a lot of coding progress during this chapter regarding having
the game randomly select content for the players to solve.
Declare an int named spinDeg at the top of your JavaFXGame class and set it equal to the 45 degrees that the
game board is rotated to on startup. Also, declare a quadrantLanding variable initialized at zero (the default, so
no = 0 is needed) to hold the quadrant rotation delta (45, 135, 225, or 315). Create five empty public void methods
at the bottom of your class (you don’t always have to force NetBeans to create your Java code for you). This should
look like the Java statements and method constructs shown here and in light blue and yellow in Figure 19-1:


int spinDeg = 45; // Gameboard is always rotated to point/corner; initialize to 45 degrees
int quadrantLanding;
...
private void calculateQuadrantLanding() {...} // Empty Method Constructs will compile clean
private void populateQuadrantOne() {...}
private void populateQuadrantTwo() {...}
private void populateQuadrantThree() {...}
private void populateQuadrantFour() {...}


Figure 19-1. Declare int spinDeg and quadrantLanding variables; create empty quadrant content population
methods

Free download pdf