Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.HashMap;
- public abstract class Lifeform {
- /**
- * Creates a new Lifeform
- *
- * @param name
- * @effect getName == name
- * @effect getMode == mode.NORMAL
- * @effect setMaxHitpoints(101)
- * @effect setHitpoints(101)
- * @post getStrength() == 10.00
- *
- * @throws IllegalArgumentException
- */
- public Lifeform(String name) throws IllegalArgumentException {
- if (isValidName(name)) this.name = name; // throws possible an IllegalArgumentException
- else throw new IllegalArgumentException();
- mode = Mode.NORMAL;
- setMaxHitpoints(101);
- setHitpoints(101);
- this.strength = 10.00D; // default value of a hero
- }
- private String name;
- private int hitpoints;
- private int maxHitpoints;
- private double strength;
- private static final int strengthPrecision = 2;
- private Mode mode;
- private HashMap<String,Entity> anchor;
- /**
- * Sets the hero's hitpoints.
- *
- * @pre ...
- * | isValidHitpoints(hitpoints, getMaxHitpoints(), getMode())
- * @post ...
- * | new.getHitpoints()==hitpoints
- * @param hitpoints
- */
- public void setHitpoints(int value) throws AssertionError
- {
- assert isValidHitpoints(hitpoints, getMaxHitpoints(), getMode()): "Wrong value for hitpoints!";
- this.hitpoints = hitpoints;
- }
- /**
- * Checks whether a given amount of hitpoints is valid.
- * @param hitpoints
- * @param mode
- * @return ...
- * | hitpoints>0 && (mode==NORMAL)?isPrime(hitpoints):true
- */
- private static boolean isValidHitpoints(int hitpoints, int maxHitpoints, Mode mode) {
- return ((hitpoints<=maxHitpoints) && (hitpoints>0) && ((mode==Mode.NORMAL)?ExtraMath.isPrime(hitpoints):true));
- }
- /**
- * Returns the hero's hitpoints.
- * @return hitpoints
- */
- @Basic
- public int getHitpoints() {
- return hitpoints;
- }
- /**
- * Sets the hero's maximum hitpoints.
- *
- * @pre the hitpoints must be valid (see isValidHitpoints)
- * | isValidHitpoints(getmaxHitpoints(), Integer.MAX_VALUE, Mode.COMBAT)
- * @post the maximum of the hitpoints will be changed if there was no assert thrown.
- * | new.getMaxHitpoints()==maxHitpoints
- * @param maxHitpoints
- */
- public void setMaxHitpoints(int maxHitpoints) {
- assert isValidHitpoints(maxHitpoints, Integer.MAX_VALUE, Mode.COMBAT): "Wrong value for maxHitpoints!";
- this.maxHitpoints = maxHitpoints;
- }
- /**
- * Returns the hero's maximum hitpoints.
- * @return maxHitpoints
- */
- @Basic
- public int getMaxHitpoints() {
- return maxHitpoints;
- }
- /**
- * @return returns the Lifeforms strength
- * | return strength
- */
- public double getStrength() {return strength;}
- /**
- *
- * @post Multiplies the hero's strength by factor and ensures the result is positive.
- * | new.getStrength() == ExtraMath.round(strength*(double)Math.abs(factor), strengthPrecision)
- * @param value
- */
- public void multiplyStrength(int value) {
- this.strength = ExtraMath.round(strength*(double)Math.abs(value), strengthPrecision);
- }
- /**
- *
- * @post Divides the hero's strength by factor and ensures the result is positive..
- * | if( factor!=0 ) then
- * | new.getStrength() == ExtraMath.round(strength/(double)Math.abs(factor), strengthPrecision)
- * | else
- * | new.getStrength() == getStrength()
- * @param factor
- */
- public void divideStrength(int value) {
- if(value!=0) {
- this.strength = ExtraMath.round(strength/(double)Math.abs(value), strengthPrecision);
- }
- }
- /**
- * function which create a key into the hashmap with a given key as name,
- * but only if the key didn't exist before..
- * @param key
- * @throws IllegalArgumentException
- * @post the anchormap will have a key with the name of the parrameter.
- * | getItem(key) == null
- */
- public void addAnchor(String key) throws IllegalArgumentException
- {
- if(key.equals("") || key == null) throw new IllegalArgumentException(); // keys die niet mogen bestaan !
- if(anchor.containsKey(key)) throw new IllegalArgumentException(); // bestaat al een key met die naam !!!
- anchor.put(key, null);
- }
- /**
- * funchtion which delete the given key and with this also the reference to the content.
- * @param key
- */
- public void deleteAnchor(String key) {
- anchor.remove(key);
- }
- /**
- * function which change the content of the given key,
- * or makes a key with the given content
- * @param key
- * @param item
- * @throws IllegalArgumentException
- * | (key.equals("") || key == null)
- * @post the anchormap will contain the entry with the given key and item
- * | new.getItem(key) == item
- */
- public void joinItem(String key,Entity item) {
- if(key.equals("")) throw new IllegalArgumentException(); // keys die niet mogen bestaan !
- if(item != null && (getCapacity()-getLoad()-item.getWeight()<0))
- return;
- anchor.put(key, item);
- }
- /**
- *
- * @param key (the name of the anchor)
- * @return returns the item that is into the anchor with the given key
- * | returns Entity;
- * @throws IllegalArgumentException
- * throws an illegal argument exception if you give a empty string or a null refference
- * | (key == null || key.equals(""))
- */
- public Entity getItem(String key) throws IllegalArgumentException {
- if(key.equals("") || key == null) throw new IllegalArgumentException();
- return anchor.get(key);
- }
- /**
- * function for getting the hole hashmap as an array of content
- * @return Entity[] = {foreach(getItem())}
- */
- public Entity[] getList() {
- return (Entity[]) anchor.values().toArray();
- }
- /**
- * getter for the name
- * @return name
- */
- public String getName() {
- return name;
- }
- /**
- * gives the current load
- * @return return the weight of all the items the hero carry
- */
- public double getLoad() {
- Entity[] list = getList();
- double load = 0;
- for(Entity item:list)
- load+=item.getWeight();
- return load;
- }
- public abstract boolean isValidName(String name);
- public abstract int getProtection();
- public abstract double getCapacity();
- /**
- * private enumeration for use of the Lifeforms hitpoint system
- * @author Joris De Rijck
- */
- private enum Mode {
- COMBAT,
- NORMAL
- }
- protected void setBattle() {mode = Mode.COMBAT;}
- protected void setPeace() {mode = Mode.NORMAL;}
- /**
- * Sets the hero's current mode.
- * @pre ...
- * | mode != null
- * @post ...
- * | new.getMode()==mode
- * @param mode
- */
- public void setMode(Mode mode) {
- assert mode != null: "Null mode given!";
- this.mode = mode;
- }
- /**
- *
- * @return returns the Lifeforms mode (can either be combat or normal mode)
- * | return mode
- */
- public Mode getMode() { return mode;}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement