Selections because of certain conditions are basic structures of each programming language.
Example 1: Left or right?
|
![]() |
// JGameEx5.java import ch.aplu.jgamegrid.*; import java.awt.Color; public class JGameEx5 extends GameGrid { private Actor crab = new Actor("sprites/crab.gif"); public JGameEx5() { super(10, 10, 60, Color.red, "sprites/reef.gif"); addActor(crab, new Location(4, 0)); show(); } public void act() { double randomNumber = Math.random(); if (randomNumber > 0.5) crab.setDirection(45); else crab.setDirection(135); crab.move(); } public static void main(String[] args) { new JGameEx5(); } } |
double randomNumber = Math.random() | Returns a random decimal number between 0 and 1 |
if (randomNumber > 0.5) | The crab moves right or left with a probability of 0.5 |
If there are more than two possibilities in a selection, the switch structure is used.
Example 2: left, right, up or down
|
![]() |
// JGameEx5a.java import ch.aplu.jgamegrid.*; import java.awt.Color; public class JGameEx5a extends GameGrid { private Actor crab = new Actor("sprites/crab.gif"); public JGameEx5a() { super(10, 10, 60, Color.red, "sprites/reef.gif"); addActor(crab, new Location(4, 3)); show(); } public void act() { int randomNumber = (int)(Math.random() * 4); switch (randomNumber) { case 0: crab.setDirection(0); break; case 1: crab.setDirection(90); break; case 2: crab.setDirection(180); break; case 3: crab.setDirection(270); break; } if (!crab.isMoveValid()) crab.turn(180); crab.move(); } public static void main(String[] args) { new JGameEx5a(); } } |
(int)(Math.random() * 4) + 1 | Returns a random decimal number between 1 and 4 |
isMoveValid() | Returns true if the next moving position is still inside the grid |
break | break allows to exit the switch structure after the actions of the selected case statement are executed |
i = 0 : initial value Example 3: Initializing multiple crab objects
|
![]() |
// JGameEx6.java import ch.aplu.jgamegrid.*; import java.awt.Color; public class JGameEx6 extends GameGrid { public JGameEx6() { super(10, 10, 60, Color.red, "sprites/reef.gif"); for (int i = 0; i < 10; i++) addActor(new Crab(), getRandomEmptyLocation()); show(); } public static void main(String[] args) { new JGameEx6(); } } // ------------------- class Crab ----------------------- class Crab extends Actor { public Crab() { super("sprites/crab.gif"); } public void act() { if (isMoveValid()) move(); if (isNearBorder()) turn(90); } } |
int i = 0 |
Declaration and initialization of the numeral variable i |
i < 10 |
Loop condition. As long as the condition is met, the actions inside the loop are executed |
i++ |
Is equal to i = i + 1 The value of the varible i is increased by 1 |
addActor(new Crab(), getRandomEmptyLocation()) |
Initializes a new crab in a random location |
Example 4: Multiple crab objects are initialized with nesting for loops Basic structure:
With two for loops both values of the coordinates (x, y) are looked at. In the folowing example the if structure, which checks if the sum of x + y is even, is droped. This way, a crab will be set into every other cell of the grid.
|
![]() |
// JGameEx6a.java import ch.aplu.jgamegrid.*; import java.awt.Color; public class JGameEx6a extends GameGrid { public JGameEx6a() { super(10, 10, 60, Color.red, "sprites/reef.gif"); for (int x = 0; x < 10; x++) for (int y = 0; y < 10; y++) { if ((x + y) % 2 == 0) addActor(new Crab(), new Location(x, y)); } show(); } public static void main(String[] args) { new JGameEx6a(); } } // --------------------- class Crab ---------------------- class Crab extends Actor { public Crab() { super("sprites/crab.gif"); } public void act() { if (isMoveValid()) move(); if (isNearBorder()) turn(90); } } |
if ((x + y) % 2 == 0) |
% is a modular division which returns the rest of the division (x + y)/2. This function is often used to check if a result is odd or even. If the sum of x + y is even, a crab is set to the location (x, y) |
Basic structure:
int i = 0; while (i < n) { Anweisungen; i++; } |
The while-structure often used by many programming languages to repeat certain program parts (actions). In JGameGrid,after pressing the button Run in the navigation bar of the game window or after calling the method doRun() inside the program code, the method act of all actors is executed preiodically with the help of a while structure. Because of this, the GameGrid tutorials do not use while structures much. |
Example 5: 7 crabs are set randomly into different locations of the lower part of the grid.
|
![]() |
// JGameEx7.java import ch.aplu.jgamegrid.*; import java.awt.Color; public class JGameEx7 extends GameGrid { public JGameEx7() { super(10, 10, 60, Color.red, "sprites/reef.gif"); int nb = 0; while (nb < 7) { Location loc = new Location((int)(10 * Math.random()), 5 + (int)(5 * Math.random())); if (isEmpty(loc)) { addActor(new Crab(), loc); nb++; } } show(); } public static void main(String[] args) { new JGameEx7(); } } //---------------- class Crab ----------------------------- class Crab extends Actor { public Crab() { super("sprites/crab.gif"); } public void act() { if (isMoveValid()) move(); if (isNearBorder()) turn(90); } } |
while (nb < 7) | As long a the set amount of locations is not reached (in this case 7) the actions inside the while structure a repeated |
if (isEmpty(loc)) |
A new crab is only initialized if the location is empty |