Example: An Adventure Game Framework

Interfaces

interface Character
{
   public int getHealth();
   public void setHealth(int h);
   public void flee();
   public String getName();
}

 

interface Lover
{
   public void kiss(Character c);
}

 

interface Hero extends Character, Lover
{
   public void fight(Attacker atk);
}

 

interface Biter
{
   public void bite(Hero h);
}

 

interface Stomper
{
   public void stomp(Hero h);
}

 

interface Attacker extends Character, Biter, Stomper
{
   public void attack(Hero h);
}

Places

class Place
{
   public Place()
   {
      maxAttackers = 10;
      numAttackers = 0;
      attacker = new Attacker[maxAttackers];
   }

   public void enter(Hero hero) // a template method
   {
      for (int i = 0; i < numAttackers; i++)
      {
         attacker[i].attack(hero);
         hero.fight(attacker[i]);
         if (hero.getHealth() < attacker[i].getHealth())
            attacker[i].flee();
         else
         {
            hero.flee();
            break;
         }
      }
    }

   public void addAttacker(Attacker atk)
   {
      if (numAttackers < maxAttackers)
         attacker[numAttackers++] = atk;
      else
         System.err.println("Room is full!");
   }

   private int numAttackers, maxAttackers;
   private Attacker[] attacker;
}

A Medieval Implementation

Monsters

class Monster implements Attacker
{

   private int health;
   private Random generator;
   private String name;

   public Monster(String nm)
   {
      name = nm;
      health = 50;
      generator = new Random();
   }

   public int getHealth() { return health; }
   public void setHealth(int h) { health = h; }
   public String getName() { return name; }
   public void flee()
   {
      System.out.println(name + " is limping from the room!");
   }

   public void stomp(Hero h)
   {
      System.out.println(name + " is stomping " + h.getName());
   }

   public void bite(Hero h)
   {
      System.out.println(name + " is biting " + h.getName());
   }

   public void attack(Hero h)
   {
      stomp(h);
      bite(h);
      stomp(h);
      bite(h);
      h.setHealth(generator.nextInt() % h.getHealth());
   }
}

Knights

class Knight implements Hero
{

   private int health;
   private Random generator;
   private String name;

   public Knight(String nm)
   {
      name = nm;
      health = 50;
      generator = new Random();
   }

   public int getHealth() { return health; }
   public void setHealth(int h) { health = h; }
   public String getName() { return name; }
   public void flee()
   {
      System.out.println(name + " is limping from the room!");
   }
   public void kiss(Character c)
   {
      System.out.println(name + " is kissing " + c.getName());
   }
   public void fight(Attacker atk)
   {
      System.out.println(name + " is fighting " + atk.getName());
      atk.setHealth(generator.nextInt() % atk.getHealth());
   }
}

The Game

class Game
{

   public static void main(String[] args)
   {
      Place dungeon = new Place();
      dungeon.addAttacker(new Monster("Godzilla"));
      dungeon.addAttacker(new Monster("Mothra"));
   
      Knight xena = new Knight("Xena");
      Knight connan = new Knight("Connan");

      dungeon.enter(xena);
      dungeon.enter(connan);

      if (connan.getHealth() > 0 && xena.getHealth() > 0)
         connan.kiss(xena);
   }
}

Program Output

C:\Pearce\Java\Projects>javac Game.java

C:\Pearce\Java\Projects>java Game
Godzilla is stomping Xena
Godzilla is biting Xena
Godzilla is stomping Xena
Godzilla is biting Xena
Xena is fighting Godzilla
Godzilla is limping from the room!
Mothra is stomping Xena
Mothra is biting Xena
Mothra is stomping Xena
Mothra is biting Xena
Xena is fighting Mothra
Mothra is limping from the room!
Godzilla is stomping Connan
Godzilla is biting Connan
Godzilla is stomping Connan
Godzilla is biting Connan
Connan is fighting Godzilla
Connan is limping from the room!

 

C:\Pearce\Java\Projects>java Game
Godzilla is stomping Xena
Godzilla is biting Xena
Godzilla is stomping Xena
Godzilla is biting Xena
Xena is fighting Godzilla
Xena is limping from the room!
Godzilla is stomping Connan
Godzilla is biting Connan
Godzilla is stomping Connan
Godzilla is biting Connan
Connan is fighting Godzilla
Godzilla is limping from the room!
Mothra is stomping Connan
Mothra is biting Connan
Mothra is stomping Connan
Mothra is biting Connan
Connan is fighting Mothra
Mothra is limping from the room!
Connan is kissing Xena

C:\Pearce\Java\Projects>

A UML Class Diagram

A Space Age Implementation

Killer Robots

class Robot implements Attacker
{

   private int health;
   private Random generator;
   private String name;

   public Robot(String nm)
   {
      name = nm;
      health = 100;
      generator = new Random();
   }

   public int getHealth() { return health; }
   public void setHealth(int h) { health = h; }
   public String getName() { return name; }
   public void flee()
   {
      System.out.println(name + " has blown a fuse!");
   }

   public void stomp(Hero h)
   {
      System.out.println(name + " is crushing " + h.getName());
   }

   public void bite(Hero h)
   {
      System.out.println(name + " is lasering " + h.getName());
   }

   public void attack(Hero h)
   {
      stomp(h);
      bite(h);
      bite(h);
      bite(h);
      h.setHealth(generator.nextInt() % h.getHealth());
   }
}

Space Men

class SpaceMan implements Hero
{

   private int health;
   private Random generator;
   private String name;

   public SpaceMan(String nm)
   {
      name = nm;
      health = 50;
      generator = new Random();
   }

   public int getHealth() { return health; }
   public void setHealth(int h) { health = h; }
   public String getName() { return name; }
   public void flee()
   {
      System.out.println(name + " is limping to the escape pod!");
   }
   public void kiss(Character c)
   {
      System.out.println(name + " is kissing " + c.getName());
   }
   public void fight(Attacker atk)
   {
      System.out.print(name + " is shooting beams at ");
      System.out.println(atk.getName());
      atk.setHealth(generator.nextInt() % atk.getHealth());
   }
}

The Game

class Game
{

   public static void main(String[] args)
   {
      Place spaceShip = new Place();
      spaceShip.addAttacker(new Robot("R2D2"));
      spaceShip.addAttacker(new Robot("Robbie"));
   
      SpaceMan buzz = new SpaceMan("Buzz");
      SpaceMan barb = new SpaceMan("Barbarella");

      spaceShip.enter(buzz);
      spaceShip.enter(barb);

      if (buzz.getHealth() > 0 && barb.getHealth() > 0)
         buzz.kiss(barb);
   }
}

Program Output

C:\Pearce\Java\Projects>javac Game.java

C:\Pearce\Java\Projects>javac Game.java

C:\Pearce\Java\Projects>java Game
R2D2 is crushing Buzz
R2D2 is lasering Buzz
R2D2 is lasering Buzz
R2D2 is lasering Buzz
Buzz is shooting beams at R2D2
Buzz is limping to the escape pod!
R2D2 is crushing Barbarella
R2D2 is lasering Barbarella
R2D2 is lasering Barbarella
R2D2 is lasering Barbarella
Barbarella is shooting beams at R2D2
Barbarella is limping to the escape pod!

 

C:\Pearce\Java\Projects>java Game
R2D2 is crushing Buzz
R2D2 is lasering Buzz
R2D2 is lasering Buzz
R2D2 is lasering Buzz
Buzz is shooting beams at R2D2
R2D2 has blown a fuse!
Robbie is crushing Buzz
Robbie is lasering Buzz
Robbie is lasering Buzz
Robbie is lasering Buzz
Buzz is shooting beams at Robbie
Buzz is limping to the escape pod!
R2D2 is crushing Barbarella
R2D2 is lasering Barbarella
R2D2 is lasering Barbarella
R2D2 is lasering Barbarella
Barbarella is shooting beams at R2D2
Barbarella is limping to the escape pod!
Buzz is kissing Barbarella

C:\Pearce\Java\Projects>