Programs
Dnafight Source Code
 previous   up   next 

Dnafight is a programming game in which bacteria fight against each other.

In Dnafight there are different types of bacteria named with colors. Each bacterium is controlled by a DNA program written in Seed7. The plate on which the bacteria live is a rectangular grid of cells. A cell can be empty or contain one bacterium. A cell can also contain some food. In every turn a bacterium can inspect its own cell and the eight neighbor cells. Then the bacterium can decide between eating, killing or splitting in one of the four cardinal directions.

  • Eating food includes moving and is also allowed if the bacterium stays in place.
  • Killing another bacterium is allowed if the other bacterium is of the same size or smaller.
  • Splitting a bacterium produces two half-sized bacteria. The first stays in place while the second moves to another place. Both new bacteria can eat food.

There are two ways to play this game:

  1. Place some bacteria on the plate and let them fight against each other.
  2. Write a DNA program for a new bacterium and measure your programming skills.

Interfaces usable by a DNA program

The "dna_base.s7i" library contains the interfaces for a DNA program. It contains the following definitions:

The enumeration type bactColor identifies the color of a bacterium:

EDGE (edge of the plate),
CLEAR (empty field),
WHITE, VIOLET, INDIGO, BLUE, CYAN, GREEN, YELLOW, AMBER, ORANGE, RED, SCARLET, TAN, LILIAC, PINK

The enumeration type direction identifies the neighbor cells of a bacterium:

HERE, NORTH, SOUTH, WEST, EAST, NW, NE, SW, SE.

The following functions can be used to read properties of cells:

  • view(direction) returns the content (bactColor) of a cell.
  • strength(direction) returns the strength/size of a bacterium or 0 for 'CLEAR' and 'EDGE' fields.
  • food(direction) returns the amount of food at a field or 0 for 'EDGE' fields.

The color of a bacterium can be computed with view(HERE) and it's size can be computed with strength(HERE). The following functions can be used to determine the action of a bacterium:

  • eat(direction, integer) moves a bacterium and it eats the given quantity of food. If the direction is not 'HERE' the destination field must be 'CLEAR'.
  • kill(direction) moves a bacterium and it kills/eats another bacterium at the new position. The other bacterium must be of the same size or smaller.
  • split(direction, integer, integer) splits a bacterium in two (one moves in the given direction). The two bacteria eat the given amounts of food. The direction must not be 'HERE' and the destination field must be 'CLEAR'.

Eating, killing and splitting in a diagonal direction (NW, NE, SW, SE) is not allowed. A bacterium can use eat(HERE, 0) to wait and kill(HERE) to commit suicide. In every move a bacterium shrinks by a certain percentage (to simulate the metabolism).

  • shrinkSize(integer) computes the size by which a bacterium with the given size would shrink (if the bacterium does nothing).

The food ingested or the size of a killed bacterium (which also counts as food) are added to the size of the bacterium. If the size of a bacterium reaches 0 it dies due to hunger. If a bacterium shrinks although it ingests food (there is not enough food to keep the current size) it still has hunger. After being hungry for more than 3 successive moves a bacterium dies also due to hunger.

  • hunger computes how many moves a bacterium survives in a hunger situation.

There are some constants defining the basic conditions at the start of the simulation:

  • initSize defines the initial size of the bacteria.
  • foodReserve is the initial amount of food at all fields (except 'EDGE').
  • shrinkage is the percentage by which bacteria shrink in every move.

With these interfaces a DNA program can be written. Additionally there are some convenience definitions to make the job of writing a DNA program easier.

  • The constant 'ALL' can be used with 'eat' and 'split' to determine that all food from the given field should be ingested.
  • The type colorSet is defined as set of bactColor.
  • The constant 'ALL_COLORS' defines a set of all bacterium colors.
  • The type directSet is defined as set of direction.
  • The constant 'MAIN_DIRECTIONS' defines a set of the directions NORTH, SOUTH, WEST and EAST.
  • The constant 'DIAGONAL_DIRECTIONS' defines a set of the directions NW, NE, SW and SE.
  • The arrays 'left' and 'right' can be used to get a neighboring direction. E.g.: left[NORTH] is NW, right[SE] is SOUTH, left[HERE] is HERE.
  • nextSize(integer, integer, integer) Computes the new size of a bacterium. As input it has the current size, the food it plans to ingest and the current hunger.
  • min(integer, integer) returns the minimum of the two integers.
  • max(integer, integer) returns the maximum of the two integers.
  • ranDir(directSet) returns a random direction from the given directSet. If the directSet is empty it returns HERE.


Dnafight

Dnafight

 previous   up   next