Game apps for smartphones and tablets

Research project PHBern  
HomeStart online editorAndroid TurtlegraphicsPrintJava-Online

Programme structures


1. if-else-structure (selection)

Program branches because of certain conditions belong to the basic structure of each programming language.

    if(condition)
  {
    instructions  
  } 
  else
  {
    instructions
  }   
  The instructions after if are only to be followed if the condition is correct, otherwise, the instructions according to else are to be followed. often only the if block is followed to check the conditions

Example 1: Left or right?
The crab randomly decides after every step if he wants to swim to the right or the left side. By clicking Reset, the program can be re-run.

By using the method move(), another object moves in one of the 8 neighbouring cells.

setDirection() newly determines the direction

In our example, the direction is chosen randomly among 45° and 135.

 

Run this example on smartphone or emulator

Edit this example in the Online Editor

App installieren auf Smartphone oder Tablet

Download sources (AndroidEx5.zip)

 

// 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

 

2. switch structure (multiple selection)

If there are several possibilities, the switch structure is used.

    switch (variable)
  {
    case wert1:
      Instructions;
      break;
    case wert2:
      Instructions;
      break;
    .....
    default:
      Instructions;  
  }

Example 2: Left, right, up or down
The crab randomly changes his direction of movement after each step. If he has reached one of the border cells, he continues swimming in the opposite direction.

Run this example on smartphone or emulator r

Edit this example in the Online Editor

App installieren auf Smartphone oder Tablet

Download sources (AndroidEx5a.zip)

 

// AndroidEx5a.java

package app.ex5a;

import ch.aplu.android.*;

public class AndroidEx5a extends GameGrid
{
  public AndroidEx5a()
  {
    super(8, 8, cellZoom(62), GRAY, "reef"falsefalse);
  }

  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

 

3. for structure (iteration / multiple execution)


i = 0 : starting value
i < n : cancel condition
i++ : change of value

Example 3: Generate several crabs in a for structure
With the aid of a for loop, 10 crabs are generated at random positions. Since the method act() is implemented in the class crab, the instructions are exectued in the method act() for all crab objects- all crabs know how they have to move.

If a crab gets in a border cell, he turns by 90°. Since he then still is in a border cell, he has to turn by 90° again.  

Run this example on smartphone or emulator

Edit this example in the Online Editor

App installieren auf Smartphone oder Tablet

Download sources (AndroidEx6.zip)

 

// 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 = 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

 

4. Nested for loops


Example 4: Create several crab object with the aid of nested for loops

General form:

    for (int x = 0; x < n; x++)
  {
    for (int y = 0; y < m; y++)
    {
      Instructions;
    }
  }  

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.

Run this example on smartphone or emulator

Edit this example in the Online Editor

App installieren auf Smartphone oder Tablet

Download sources (AndroidEx7.zip)

 

// 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 = 0; x < 8; x++)
      for (int = 0; y < 8; y++)
      {
        if ((x + y% == 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).

 

5. While structure (iteration)

General form:

    int i = 0;           
    while (< 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.