Drucken

Intelligenter Kobold

Es ist einfach, den Akteuren eine Spielstrategie mitzugeben, die sie sogar lernfähig macht. Im folgenden Beispiel bewegt sich eine Pacman-ähnliche Spielfigur in einem eindimensionalen Gitter. Der Benutzer kann mit einem Mausklick Kraftpillen in die Zellen legen, die Kobold beim Zusammentreffen schluckt. Auch Pillen haben ein Eigenleben, indem sie nach einer vorgebbaren Zeit „verfallen“, dabei ihre positive Wirkung verlieren und giftig werden. Schluckt der Kobold eine giftige Pille, so wird er krank. Es gibt drei Pillenfarben: Beim Start des Spiels wird die Farbe der „gesunden“ Pille zufällig gewählt. Beim Verfall der Pille erhält die Pille die nächstfolgende Pillenfarbe. Damit sind die Farben der gesunden und verfallenen Pille zufällig und dem Kobold beim Programmstart unbekannt.

Programmcode

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


Erklärungen zum Programmcode:
int idStart = (int)(Math.random() * 3) Die Pillenfarbe (Sprite ID) wird zu Beginn zufällig gewählt
setPaintOrder(Kobold.class);
setActOrder(Kobold.class);
Der Kobolt erscheit vor der Pille (Pille verschwindet hinter dem Kobold
Pill.getPixelColor(new Point(20, 20) Gibt der Farbe der Pille zurück