SHARE
TWEET

Untitled

a guest May 22nd, 2019 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package creature;
  2.  
  3. import core.Cave;
  4. import core.Game;
  5. import core.PlayObject;
  6. import creature.Inhabitant;
  7. import trap.Trap;
  8.  
  9.  
  10.  
  11. /**
  12.  * This is the root class of the Inhabitant/creature subtree.
  13.  *
  14.  * Some methods/attributes are inherited from PlayObject.
  15.  * Most notably the Game object "game".  This provides access to a Game
  16.  * and its Maze and Caves.
  17.  *
  18.  * All of your Inhabitant subclasses must supply definitions for all of the
  19.  * methods listed in this class - either directly, or through inheritance from
  20.  * classes above.  You will want to edit this file to change some of the abstract
  21.  * methods into concrete methods (i.e. supply code at this level).
  22.  *
  23.  */
  24. public abstract class Inhabitant extends PlayObject {
  25.            
  26.     // inherited:  game, setLocation(G,cave/ID)
  27.    
  28.     /**
  29.      * the name of this Inhabitant
  30.      */
  31.     private String name;
  32.    
  33.     /**
  34.      * @return the Inhabitant's name
  35.      */
  36.     public String getName() { return name;}
  37.  
  38.     /**
  39.      * Create an Inhabitant and name it.
  40.      * @param nom  the name.
  41.      */
  42.     public Inhabitant(String nom) {
  43.         name = nom;
  44.     }
  45.    
  46.     /**
  47.      * Determine the Cave in which the Inhabitant currently resides.
  48.      * This will be a Cave object, or null if the Inhabitant has no location / is dead.
  49.      * @return the Inhabitant's location (Cave)
  50.      */
  51.     public Cave getLocation() {
  52.         return game.getMaze().findCave(this);
  53.     }
  54.    
  55.     // abstract definition from PlayObject supplied here:
  56.     /**
  57.      * Set the location of this Inhabitant to be the Cave with given ID,
  58.      * and set the local game instance variable to the supplied Game.
  59.      * The operation fails if there is no such Cave in the supplied Game.
  60.      * The operation will also fail if the Game is not in setup mode.
  61.      * @param theGame  Game object
  62.      * @param caveID   Cave object
  63.      * @return true on success
  64.      */
  65.     public boolean setLocation(Game theGame, Cave theCave){
  66.         boolean OK = theCave.setOccupant(this, theGame);  // Cave will do necessary checks.
  67.         if (OK) game = theGame;
  68.         return OK;
  69.     }
  70.    
  71.     /* (non-Javadoc)
  72.      * @see java.lang.Object#toString()
  73.      */
  74.     public String toString() {
  75.         String str = "";
  76.         str += name;
  77.         Cave location = getLocation();
  78.         if (location!=null)
  79.             str += "  in cave " + location.getID();
  80.         return str;
  81.     }
  82.    
  83.     //  //  //  //  //  //  //  //  //  //  //  //  //  //  //
  84.     // life functionality
  85.    
  86.     /**
  87.      * @return number of damage points remaining
  88.      */
  89.     abstract public int getDamagePoints();
  90.  
  91.    
  92.     /**
  93.      * whether or not this Inhabitant is alive.
  94.      * @return true if alive, false if dead.
  95.      */
  96.     abstract public boolean isAlive();
  97.    
  98.     /**
  99.      * Kill this Inhabitant immediately and remove it from its location.
  100.      * After this method completes, isAlive() should return FALSE
  101.      * and getLocation() should return NULL.
  102.      */
  103.     abstract public void putToDeath();
  104.    
  105.     /**
  106.      * Remove a damage point from this Inhabitant.
  107.      * If this causes the damage points to become zero then print the message
  108.      *  "<name> succumbs to battle wounds" and kill the Inhabitant immediately.
  109.      * otherwise print "<name> takes damage, points now <damage points>".
  110.      * @return true if Inhabitant is still alive at completion.  False if Inhabitant is dead.
  111.      */
  112.     abstract public boolean takeDamage();
  113.  
  114.     //  //  //  //  
  115.     // sleep functionality
  116.    
  117.     /**
  118.      * whether or not the Inhabitant is awake.
  119.      * @return true if awake, false if asleep.
  120.      */
  121.     abstract public boolean isAwake();
  122.  
  123.     /**
  124.      * Wake the Inhabitant immediately, regardless of how many turns of sleep are left.
  125.      * Print the message
  126.      *  "<name> is jolted awake."
  127.      */
  128.     abstract protected void wakeUpNow();
  129.    
  130.     /**
  131.      * Put Inhabitant to sleep for a fixed number of turns.
  132.      * @param nturn # turns to sleep for.
  133.      */
  134.     abstract public void goToSleep(int nturn);
  135.    
  136.     /**
  137.      * Put Inhabitant to sleep forever (or until woken).
  138.      */
  139.     abstract public void goToSleep();
  140.    
  141.     //  //  //  //
  142.     // warning functionality
  143.     // warningMessage is abstract and inherited from PlayObject.
  144.     // You _might_ want to define an implementation here ;)
  145.    
  146.     //  //  //  //  
  147.     // move-location functionality
  148.    
  149.     /**
  150.      * Attempt a move to the specified Cave.
  151.      * If the specified Cave is null then the move fails.
  152.      * The move is deemed successful if the Inhabitant moves to ANY Cave,
  153.      * whether it be the desired Cave or a different one (as might happen when
  154.      * relocated by a SuperBat).
  155.      *
  156.      * If the Cave is not null then print the message
  157.      *  "<name> attempts move to Cave #<id>"
  158.      * The move is attempted by requesting occupancy of the Cave.
  159.      * If the request succeeds, print
  160.      *  "<name> moved, ended up in cave <id>"
  161.      * If the request failed and Inhabitant this is still alive then print
  162.      *  "<name> failed to move."
  163.      * If the Inhabitant died in the attempt, do nothing more.
  164.      * @param dest the Cave to which a move is attempted
  165.      * @return true if Inhabitant moved, false if remained in same Cave.
  166.      */
  167.     abstract boolean moveToNewCave(Cave dest);
  168.    
  169.     /**
  170.      * choose a Cave at random from the neighbours of the Inhabitant's location,
  171.      * and attempt a move to it.
  172.      * If this succeeds then this.getLocation() will be the newly occupied Cave
  173.      * and this.isAlive() will be true (i.e. this didn't die)
  174.      * @return true if move was successful
  175.      */
  176.     abstract protected boolean moveToRandomExit();
  177.    
  178.     /**
  179.      * Attempt a move to a random Cave chosen from the neighbours of the parameter Cave source.
  180.      * If this succeeds then this.getLocation() will be the newly occupied Cave
  181.      * and this.isAlive() will be true (i.e. this didn't die).
  182.      * @param source  
  183.      * @return
  184.      */
  185.     abstract public boolean moveToRandomExit(Cave source);
  186.        
  187.     /**
  188.      * Let Inhabitant take his/her turn.
  189.      * This might involve any of:  attempt a move, throw a spear, spend turn asleep.
  190.      * Dead Inhabitants should not do anything.
  191.      *
  192.      * If an Inhabitant is asleep then print
  193.      *  "<name> sleeps peacefully."
  194.      * unless this is its final turn sleeping, in which case print
  195.      *  "<name> yawns and stretches..."
  196.      *
  197.      * If the Inhabitant moves or throws a spear then appropriate messages
  198.      * (described elsewhere) must be printed.
  199.      *
  200.      * SUGGESTION: you might like to decompose this into several component methods,
  201.      * then call each one in turn / as needed.  This will allow you to override
  202.      * individual components in subclasses as required.
  203.      *
  204.      */
  205.     abstract public void takeATurn();
  206.    
  207.     /**
  208.      * How this Inhabitant reacts when another creature attempts to move into its Cave.
  209.      * What happens depends very much upon the type of Inhabitant.
  210.      * At the end of this method, any of the following is possible:
  211.      * - the inhabitant chooses to remain, and is still alive (return true)
  212.      * - the inhabitant dies (return false)
  213.      * - the inhabitant chooses to flee the cave (return false)
  214.      * NOTE that fleeing the Cave should not be done in this method.
  215.      *      Simply return false, and the Cave will move the Inhabitant.
  216.      * @param intruder other Inhabitant attempting to displace this Inhabitant from its current location (Cave).
  217.      * @return true if this Inhabitant is alive and chooses to remain in its Cave, false if this Inhabitant either dies or chooses to move.
  218.      */
  219.     abstract public boolean handleIntruder(Inhabitant intruder);
  220.  
  221.     /**
  222.      * This method is called when the Inhabitant enters a Cave, to affect any other Inhabitant which is
  223.      * resident in that Cave.  In general this will wake the resident; further effects depend upon
  224.      * the actual type of this Inhabitant.
  225.      * NOTE this method should not remove resident from the Cave;
  226.      *      Movement is organised by the Cave, based on return values of this method and handleIntruder().
  227.      * @param resident  the other Inhabitant whose location is the Cave to which this Inhabitant wants entry.
  228.      * @return true if resident is still alive and able to handle the intrusion, false otherwise.
  229.      */
  230.     abstract public boolean disturbResident(Inhabitant resident);
  231.  
  232.    
  233.     //  //  //  //  //  //  //  //  
  234.     // Trappy functionality
  235.    
  236.     /**
  237.      * Whether or not the Inhabitant is immune to a given Trap.
  238.      * By default Inhabitants will not be immune to any Traps.
  239.      * @param T  the Trap
  240.      * @return true if Inhabitant is unaffected by Trap, false otherwise.
  241.      */
  242.     abstract public boolean hasImmunity(Trap T);
  243.    
  244. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top