mazeengine.java

来自「简单的一个maze系统」· Java 代码 · 共 148 行

JAVA
148
字号
package mazeImplementation;// -----------------------------------------------// Programming 2 -- Semester 2, 2007// Maze Engine implementation// Peter Tilmanis, 3 September 2007// -----------------------------------------------// Maze engine class// -----------------------------------------------import java.util.*;public class MazeEngine implements Buildable, Traversable{   private Player player;   private MazeSpace maze;   private int initialHealth;   public MazeEngine(MazeRef size)   {      this(size, "Unknown", 50);   }   public MazeEngine(MazeRef size, String name, int initialHealth)   {      this.initialHealth = initialHealth;      player = new Player(name, initialHealth);      maze = new MazeSpace(size);   }   public MazeRef getMazeSize()   {      return maze.getMazeSize();   }   public Room getRoom(MazeRef loc)   {      return maze.getRoom(loc);   }         public Room[] getAllRooms()   {      return maze.getAllRooms();   }   public boolean setStartPoint(MazeRef loc)   {      return maze.setStartPoint(loc);   }   public boolean setFinishPoint(MazeRef loc)   {      return maze.setFinishPoint(loc);   }   public boolean createRoom(MazeRef loc, String name)   {      if (!maze.isValidReference(loc))         return false;      Room temp = new Room(loc, name);       return maze.addRoom(temp);   }   public boolean addItem(MazeRef loc, String name, int health)   {      if (!maze.roomExists(loc))         return false;      Item temp = new Item(name, health);      maze.getRoom(loc).addItem(temp);      return true;   }   public boolean removeItem(MazeRef loc, String name)   {      if (!maze.roomExists(loc))         return false;      return maze.getRoom(loc).removeItem(name);   }   public boolean addExitPoint(MazeRef loc, String name, MazeRef destination)   {      if (!maze.roomExists(loc) || !maze.roomExists(destination))         return false;      ExitPoint temp = new ExitPoint(name, destination);      maze.getRoom(loc).addExit(temp);      return true;   }   public boolean removeExitPoint(MazeRef loc, String name)   {      if (!maze.roomExists(loc))         return false;      return maze.getRoom(loc).removeExit(name);   }   public boolean reset()   {      // check start and end points      if (!maze.roomExists(maze.getStartPoint()))         return false;      if (!maze.roomExists(maze.getFinishPoint()))         return false;      // reset player      String playerName = player.getName();      player = new Player(playerName, initialHealth);      player.setLocation(maze.getStartPoint());      return true;      }   public Player getPlayer()   {      return player;   }   public boolean move (String exitPointName) throws GameException   {      // get exit point      ExitPoint ep = maze.getRoom(player.getLocation()).getExit(exitPointName);      // move to new room      if (ep == null)         return false;      else         player.setLocation(ep.getDestination());      // apply health, die if necessary      Item[] items = maze.getRoom(player.getLocation()).getItemList();      for (int i = 0; i < items.length; i++)      {         player.applyHealth(items[i].getHealth());      }            // check for finish point      if (player.getLocation().equals(maze.getFinishPoint()))         throw new SolvedException();      return true;   }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?