Game apps for smartphones and tablets |
|
Research project PHBern |
Program branches because of certain conditions belong to the basic structure of each programming language.
Example 1: Left or right?
|
![]() |
// AndroidEx5.java package app.ex5; import ch.aplu.android.*; public class AndroidEx5 extends GameGrid { public AndroidEx5() { super(8, 8, cellZoom(62), GRAY, "reef", true); } public void main() { Crab crab = new Crab(); addActor(crab, new Location(3, 0)); doRun(); } } class Crab extends Actor { public Crab() { super("crab"); } public void act() { double randomNumber = Math.random(); if (randomNumber > 0.5) setDirection(45); else setDirection(135); if (isMoveValid()) move(); } } |
Explanations to the program code:
double randomNumber = Math.random() | The method Math.random() offers a random decimal number |
if (randomNumber > 0.5) | With the probability of 0.5, the crab moves to the right or to the left |
if (isMoveValid()) move() |
The method isMoveValid() gives back true when the next movement step is within the next window |
If there are several possibilities, the switch structure is used.
Example 2: Left, right, up or down
|
![]() |
// AndroidEx5a.java package app.ex5a; import ch.aplu.android.*; public class AndroidEx5a extends GameGrid { public AndroidEx5a() { super(8, 8, cellZoom(62), GRAY, "reef", false, false); } public void main() { Crab crab = new Crab(); addActor(crab, new Location(3, 0)); doRun(); } } class Crab extends Actor { public Crab() { super("crab"); } public void act() { int randomNumber = (int)(Math.random() * 4); switch (randomNumber) { case 0: setDirection(0); break; case 1: setDirection(90); break; case 2: setDirection(180); break; case 3: setDirection(270); break; } if (!isMoveValid()) turn(180); move(); } } |
Explanations to the program code:
(int)(Math.random() * 4) | Generates randomly integers 0, 1, 2 or 3 |
isMoveValid() | The methodisMoveValid() gives back true if the next cell position is within the visible grid |
break | break causes in every case that the program exits the switch structure as soon as the suitable case happens |
i = 0 : starting value Example 3: Generate several crabs in a for structure
|
![]() |
// AndroidEx6.java package app.ex6; import ch.aplu.android.*; public class AndroidEx6 extends GameGrid { public AndroidEx6() { super(8, 8, cellZoom(62), GRAY, "reef", false); } public void main() { for (int i = 0; i < 8; i++) addActor(new Crab(), getRandomEmptyLocation()); doRun(); } } class Crab extends Actor { public Crab() { super("crab"); } public void act() { if (isMoveValid()) move(); if (isNearBorder()) turn(90); } } |
Instructions to the program code:
int i = 0 |
Declaration and initialization of the counter variable i |
i < 10 |
Loop condition. As long as the condition is met, the instructions in the loop block are executed repeatedly |
i++ |
Is equivalent to i = i + 1 The value of the variable i is enlarged by 1 |
addActor(new Crab(), getRandomEmptyLocation()) |
Generates a new crab instance of a randomly chosen free cell |
Example 4: Create several crab object with the aid of nested for loops General form:
With two for loops, you can go through all value pair (x, y). If you omit the if structure in the following example, that checks whether the sum of x + y is an even number, a new crab is created in every cell.
|
![]() |
// AndroidEx7.java package app.ex7; import ch.aplu.android.*; public class AndroidEx7 extends GameGrid { public AndroidEx7() { super(8, 8, cellZoom(62), GRAY, "reef", false); } public void main() { for (int x = 0; x < 8; x++) for (int y = 0; y < 8; y++) { if ((x + y) % 2 == 0) addActor(new Crab(), new Location(x, y)); } doRun(); } } class Crab extends Actor { public Crab() { super("crab"); } public void act() { if (isMoveValid()) move(); if (isNearBorder()) turn(90); } } |
Explanations to the program code:
if ((x + y) % 2 == 0) |
% is a module division that equals the rest of the division (x + y)/2. This function is used often to test if a number is odd or even. If the sum of x and y is an even number, a crab is created at the location (x, y). |
General form:
int i = 0; while (i < n) { Instructions; i++; } |
The while structure is used in all proramming language for the repetitions of program blocks. In JDroidLib, the act- methods of all actors are selected periodically in a while structure after pressing Run or selecting doRun(). Therefore, the while structure is only seldomly used in this course. |