Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Enumeration;
- import java.util.HashMap;
- import java.util.NoSuchElementException;
- import be.kuleuven.cs.som.annotate.*;
- /**
- * class which represents a lifeform, this can be a Hero or a Monster
- *
- * @author Joris De Rijck & Jeroen De Coninck
- *
- */
- 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;
- else throw new IllegalArgumentException();
- mode = Mode.NORMAL;
- setMaxHitpoints(101);
- setHitpoints(101);
- this.strength = 10.00D; // default value of a hero
- }
- /**
- * Creates a new Lifeform
- *
- * @param name
- * @param strength
- * @post getName == name
- * @post getMode == mode.NORMAL
- * @post getStrength() == strength
- *
- * @throws IllegalArgumentException
- */
- public Lifeform(String name,double strength) throws IllegalArgumentException {
- if (isValidName(name)) this.name = name;
- else throw new IllegalArgumentException();
- mode = Mode.NORMAL;
- this.strength = strength;
- }
- private String name;
- private int hitpoints;
- private int maxHitpoints;
- private double strength;
- private static final int strengthPrecision = 2;
- private Mode mode;
- private HashMap<String,Item> anchor;
- /**
- * Sets the hero's hitpoints.
- *
- * @pre comment in isValidHitpoints
- * | isValidHitpoints(hitpoints, getMaxHitpoints(), getMode())
- * @post the hitpoints will now be the value given by the parameter
- * | new.getHitpoints()==hitpoints
- * @param hitpoints
- */
- public void setHitpoints(int value) throws AssertionError
- {
- assert isValidHitpoints(value, getMaxHitpoints(), mode): "Wrong value for hitpoints!";
- this.hitpoints = value;
- }
- /**
- * Checks whether a given amount of hitpoints is valid.
- * @param hitpoints
- * @param mode
- * @return true if the hitpoints are between 0 and the maximum hitpoints
- * and are also be a prime number if the lifeform is in peace mode.
- * | 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
- * when an null key or an empty key string is given
- * @throws InvalidKeyException
- * when key already exists
- * @post the anchormap will have a key with the name of the parrameter.
- * | getItem(key) == null
- */
- public void addAnchor(String key) throws IllegalArgumentException, InvalidKeyException
- {
- if(key.equals("") || key == null) throw new IllegalArgumentException(); // keys die niet mogen bestaan !
- if(anchor.containsKey(key)) throw new InvalidKeyException();
- anchor.put(key, null);
- }
- /**
- * funchtion which delete the given key and with this also the reference to the content.
- * @param key
- * @throws InvalidKeyException
- * throw when the key doen't exists in the hero.
- */
- public void deleteAnchor(String key) throws InvalidKeyException {
- if (!anchor.containsKey(key))
- throw new InvalidKeyException();
- 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,Item item) {
- if(key.equals("") || key == null) 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(""))
- * @throws InvalidKeyException
- * throws an invalid key exception if the key didn't exist into the hero's anchors.
- */
- public Item getItem(String key) throws IllegalArgumentException,InvalidKeyException {
- if(key.equals("") || key == null) throw new IllegalArgumentException();
- if (!anchor.containsKey(key))
- throw new InvalidKeyException();
- return anchor.get(key);
- }
- /**
- * function for getting an key when you know only a value of an item
- * @param value
- * a value of dukats
- * @return returns a key when there is an item which have the same value;
- * @return returns null if there was no item with the given value;
- */
- public String getKeyByValue(int value){
- for(String i:anchor.keySet()) {
- try {
- if(getItem(i).getValue() == value)
- return i;
- } catch (IllegalArgumentException e) {
- // will not be throw because only valid keys will be used
- } catch (InvalidKeyException e) {
- // will not be throw because only valid keys will be used
- }
- }
- return null;
- }
- protected String getKeyWithMaxValue() {
- String currentMaxKey = null;
- int maxValue=0;
- for(String i:anchor.keySet()) {
- try {
- if(getItem(i).getValue()> maxValue) {
- maxValue = getItem(i).getValue();
- currentMaxKey = new String(i);
- }
- } catch (IllegalArgumentException e) {/*will never be throw*/
- } catch (InvalidKeyException e) {/*will never be throw*/}
- }
- return currentMaxKey;
- }
- /**
- * returns a key of an object if the objects are the same.
- * @param item
- * @return key of the item, or null if the item isn't in the anchor
- */
- public String getKeyByItem(Item item) {
- for(String i:anchor.keySet()) {
- if (anchor.get(i) == item)
- return i;
- }
- return null;
- }
- /**
- * function for getting the hole hashmap as an array of content
- * @return Item[]
- */
- public Item[] getList() {
- return (Item[]) anchor.values().toArray();
- }
- /**
- * getter for the name
- * @return name
- */
- public String getName() {
- return name;
- }
- /**
- * gives the current load
- * @return weight
- * return the weight of all the items the hero carry
- */
- public double getLoad() {
- Item[] list = getList();
- double load = 0;
- for(Item item:list)
- load+=item.getWeight();
- return load;
- }
- /**
- * checks if the Lifeform can carry that weight extra.
- * @param weight
- * @return getCapacity()-getLoad()-weight>=0?true:false;
- */
- public boolean canCarryWeight(double weight) {
- if (getCapacity() - getLoad() - weight >=0)
- return true;
- return false;
- }
- abstract public boolean isValidName(String name);
- abstract public int getProtection() throws IllegalArgumentException, InvalidKeyException;
- abstract public double getCapacity();
- /**
- * private enumeration for use of the Lifeforms hitpoint system
- */
- private static enum Mode {
- COMBAT,
- NORMAL
- }
- /**
- * sets the lifeform into combat mode
- */
- public void setBattle() {mode = Mode.COMBAT;}
- /**
- * set the lifeform into peace mode
- */
- public void setPeace() {mode = Mode.NORMAL;}
- public class AnchorIterator {
- String[] keys;
- int count;
- public AnchorIterator() {
- keys = (String[]) anchor.keySet().toArray();
- count = 0;
- }
- public boolean hasNextElement() {
- for(int c = count;c<keys.length;c++){
- if (anchor.get(keys[c]) != null)
- return true;
- }
- return false;
- }
- public Item getNext() {
- for(int c = count;c<keys.length;c++){
- if (anchor.get(keys[c]) != null) {
- count = c+1;
- return anchor.get(keys[c]);
- }
- }
- throw new NoSuchElementException();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement