# Implicit algorithm in a small game

I was working on a small game that displays some blocks. These blocks need to move. The way to make these blocks move is by changing the `x` and `y` position over time.

The way I did this in the game was by calling a `tick()` method every 1/60th of a second on every block that could be moving. Then the block can change it's position according to some algorithm. I just wrote code that makes these changes to the position.

But if you take a closer look at the code you can see that some lines are connected. These lines are part of the same algorithm, but there is no place where all the parts come together in the same place.

The blocks in the first level have three states.

1. Do nothing. Stay in the same position.
2. Falling. When falling the block makes sure its position returns to the default position.
3. Shaking. Some blocks shake, other don't. When shaking the position is changed randomly around its current position, the position from state 1.

Let's look at shaking. We shake the block by changing the difference from the current position of the block by a small random amount. In the code it looks like this.

``````if (selected) {
this.dx = rnd.nextInt(3) - 1;
this.dy = rnd.nextInt(3) - 1;
}
``````

The fields `dx` and `dy` are the difference in pixel position that this object is drawn on screen.

The Level class contains a small part of this algorithm. It makes sure that the object is drawn (dx,dy) pixels from its normal position. Without this line of code the algorithm wouldn't work.

In a way there is also an implicit datastructure that contains the difference position. The code doesn't make that obvious.

#### Making it obvious

The is a piece of code contained in the game that shakes an Entity. By extracting this into a class we can help other programmers and reuse this piece of code. But we have to look at the concepts that are used in this code.

A few concepts that I think are in this code are: shake, tick, difference in position and entity. There are as always many ways to do the same thing.

``````public class Shake implements Mover {
private static final Random rnd = new Random();
private Movable object;

public Shake(Movable dm)  { this.object=dm; }

public void tick() {
object.moveBy(rnd.nextInt(3)-1, rnd.nextInt(3)-1);
}
}
``````

Now we need to write multiple objects that do a similar thing. This way we can show that the concept is useful.

``````public class Falling implements Mover {
private Movable object;

public Falling(Movable m) { this.object=dm; }

public void tick() {
if (m.y() < 0) {
m.moveBy(0, 3);
}
}
}
``````

We can see here a falling block. This code will make the block fall automatically. Because of the way the game works a block will only fall by a certain amount. Then it needs to move to the next position in the level.

I will write some more about implicit algorithms (and datastructures) later.

### Mentions

#### Welcome

My name is Peter Stuifzand. You're reading my personal website.

#### Profiles

Peter Stuifzand
peter@peterstuifzand.nl
Zwolle, The Netherlands