GameGrid: Game programming with Java

Research project PHBern  
HomePrintJava-Online

Intelligent kobold

It is easy to program the actors with game strategies which makes them intelligent - meaning that they are able to learn from their actions. In the following example a PacMan character moves around in a one dimensional grid. The player can place power-pills inside the cells by clicking them. The kobold eats them as soon as it reaches the filled cells. The pills rot after a predefined amount of time, loose their power and become poisonous. If the kobold eats a rotten pill, it falls ill. The next time it could eat a rotten pill, it knows about its bad power and does not eat it. The pills can have three different colors. At the start of the game, one of the color is set randomly to be the healthy pill-color. The rotten pills get the next possible color. This procedure ensures that it is impossible for the kobold to know the healthy pills from the rotten ones at the start of the game

Run this example

Edit this example in the Online-Editor

Program code

// KoboldApp.java

import ch.aplu.jgamegrid.*;
import java.awt.*;

public class KoboldApp extends GameGrid implements GGMouseListener
{
  private final int idStart = (int)(Math.random() * 3);

  public KoboldApp()
  {
    super(10160Color.red, false);
    setSimulationPeriod(600);
    addActor(new Kobold()new Location(00));
    addMouseListener(this, GGMouse.lPress);
    show();
    doRun();
  }

  public boolean mouseEvent(GGMouse mouse)
  {
    Location location = toLocationInGrid(mouse.getX(), mouse.getY());
    if (getOneActorAt(location) == null) // only empty cell
    {
       Pill pill = new Pill(idStart);
       addActor(pill, location);
       pill.show(idStart);
       setPaintOrder(Kobold.class);
       setActOrder(Kobold.class);
       refresh();
    }
    return true;
  }

  public static void main(String[] args)
  {
    new KoboldApp();
  }
}

// ---------------- class Kobold ----------------
class Kobold extends Actor
{
  private Color BadPillColor = Color.black;

  public Kobold()
  {
    super("sprites/kobold.gif"3);
  }

  public void act()
  {
    show(0);  // Kobold with open Mouth
    Pill Pill = (Pill)(gameGrid.getOneActorAt(getLocation()Pill.class));
    if (Pill != null && !isPillBad(Pill))
      eatPill(Pill);
    else
      step();
  }

  private void eatPill(Pill Pill)
  {
    // Remember the PillColor
    Color PillColor = Pill.getPixelColor(new Point(2020));
    // Let pill work
    Pill.works(this);
    // test the pill
    if (getIdVisible() == 0) // healthy
      show(1)// Kobold with the mouth closed
    else  // ill
      BadPillColor = PillColor; // save the color of bad pill:
                                // "learn", which is bad
  }

  private void step()
  {
    if (!isMoveValid())
    {
      turn(180);
      setHorzMirror(!isHorzMirror());
    }
    move();
  }

  private boolean isPillBad(Pill Pill)
  {
    return Pill.getPixelColor(new Point(2020)).equals(BadPillColor);
  }
}

// ------- class Pill --------------------------
class Pill extends Actor
{
  private int startId;
  private int period = 0;
  private int maxPeriod = 5;

  public Pill(int startId)
  {
    super("sprites/pille.gif"3);
    this.startId = startId;
  }

  public void act()
  {
    if (!istBad() && period == maxPeriod)
      showNextSprite();
    period++;
  }

  protected void works(Kobold man)
  // Let the Pill work
  {
    if (istBad())
      man.show(2);  // Kobold red (ill)
    removeSelf();
  }

  private boolean istBad()
  {
    return getIdVisible() != startId;
  }
}


Explaining the program code:
int idStart = (int)(Math.random() * 3) the color (Sprite ID) of the pill is set randomly
setPaintOrder(Kobold.class);
setActOrder(Kobold.class);
the kobold appears in front of the pill ( the pill disappears behind the kobold)
Pill.getPixelColor(new Point(20, 20) returns the color of the pill