Advertisement
Guest User

Untitled

a guest
May 27th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.54 KB | None | 0 0
  1. import java.util.Enumeration;
  2.  
  3. import java.util.HashMap;
  4.  
  5. import java.util.NoSuchElementException;
  6.  
  7.  
  8.  
  9. import be.kuleuven.cs.som.annotate.*;
  10.  
  11.  
  12.  
  13. /**
  14.  
  15.  * class which represents a lifeform, this can be a Hero or a Monster
  16.  
  17.  *
  18.  
  19.  * @author Joris De Rijck & Jeroen De Coninck
  20.  
  21.  *
  22.  
  23.  */
  24.  
  25. public abstract class Lifeform {
  26.  
  27.  
  28.  
  29.     /**
  30.  
  31.      * Creates a new Lifeform
  32.  
  33.      *
  34.  
  35.      * @param name
  36.  
  37.      * @effect  getName == name
  38.  
  39.      * @effect  getMode == mode.NORMAL
  40.  
  41.      * @effect  setMaxHitpoints(101)
  42.  
  43.      * @effect  setHitpoints(101)
  44.  
  45.      * @post    getStrength() == 10.00
  46.  
  47.  
  48.  
  49.      *
  50.  
  51.      * @throws IllegalArgumentException
  52.  
  53.      */
  54.  
  55.     public Lifeform(String name) throws IllegalArgumentException {
  56.  
  57.         if (isValidName(name)) this.name = name;
  58.  
  59.         else throw new IllegalArgumentException();
  60.  
  61.         mode = Mode.NORMAL;
  62.  
  63.         setMaxHitpoints(101);
  64.  
  65.         setHitpoints(101);
  66.  
  67.         this.strength = 10.00D; // default value of a hero
  68.  
  69.     }
  70.  
  71.    
  72.  
  73.     /**
  74.  
  75.      * Creates a new Lifeform
  76.  
  77.      *
  78.  
  79.      * @param   name
  80.  
  81.      * @param   strength
  82.  
  83.      * @post    getName == name
  84.  
  85.      * @post    getMode == mode.NORMAL
  86.  
  87.      * @post    getStrength() == strength
  88.  
  89.      *
  90.  
  91.      * @throws IllegalArgumentException
  92.  
  93.      */
  94.  
  95.     public Lifeform(String name,double strength) throws IllegalArgumentException {
  96.  
  97.         if (isValidName(name)) this.name = name;
  98.  
  99.         else throw new IllegalArgumentException();
  100.  
  101.         mode = Mode.NORMAL;
  102.  
  103.         this.strength = strength;
  104.  
  105.     }
  106.  
  107.    
  108.  
  109.     private String name;
  110.  
  111.     private int hitpoints;
  112.  
  113.     private int maxHitpoints;
  114.  
  115.     private double strength;
  116.  
  117.     private static final int strengthPrecision = 2;
  118.  
  119.     private Mode mode;
  120.  
  121.     private HashMap<String,Item> anchor;
  122.  
  123.     /**
  124.  
  125.      * Sets the hero's hitpoints.
  126.  
  127.      *
  128.  
  129.      * @pre     comment in isValidHitpoints
  130.  
  131.      *          | isValidHitpoints(hitpoints, getMaxHitpoints(), getMode())
  132.  
  133.      * @post    the hitpoints will now be the value given by the parameter
  134.  
  135.      *          | new.getHitpoints()==hitpoints
  136.  
  137.      * @param hitpoints
  138.  
  139.      */
  140.  
  141.     public void setHitpoints(int value) throws AssertionError
  142.  
  143.     {
  144.  
  145.     assert isValidHitpoints(value, getMaxHitpoints(), mode): "Wrong value for hitpoints!";
  146.  
  147.     this.hitpoints = value;
  148.  
  149.     }
  150.  
  151.    
  152.  
  153.     /**
  154.  
  155.      * Checks whether a given amount of hitpoints is valid.
  156.  
  157.      * @param   hitpoints
  158.  
  159.      * @param   mode
  160.  
  161.      * @return  true if the hitpoints are between 0 and the maximum hitpoints
  162.  
  163.      *          and are also be a prime number if the lifeform is in peace mode.
  164.  
  165.      *          | hitpoints>0 && (mode==NORMAL)?isPrime(hitpoints):true
  166.  
  167.      */
  168.  
  169.     private static boolean isValidHitpoints(int hitpoints, int maxHitpoints, Mode mode) {
  170.  
  171.         return ((hitpoints<=maxHitpoints) && (hitpoints>0) && ((mode==Mode.NORMAL)?ExtraMath.isPrime(hitpoints):true));
  172.  
  173.     }
  174.  
  175.    
  176.  
  177.     /**
  178.  
  179.      * Returns the hero's hitpoints.
  180.  
  181.      * @return hitpoints
  182.  
  183.      */
  184.  
  185.     @Basic
  186.  
  187.     public int getHitpoints() {
  188.  
  189.         return hitpoints;
  190.  
  191.     }
  192.  
  193.     /**
  194.  
  195.      * Sets the hero's maximum hitpoints.
  196.  
  197.      *
  198.  
  199.      * @pre     the hitpoints must be valid (see isValidHitpoints)
  200.  
  201.      *          | isValidHitpoints(getmaxHitpoints(), Integer.MAX_VALUE, Mode.COMBAT)
  202.  
  203.      * @post    the maximum of the hitpoints will be changed if there was no assert thrown.
  204.  
  205.      *          | new.getMaxHitpoints()==maxHitpoints
  206.  
  207.      * @param maxHitpoints
  208.  
  209.      */
  210.  
  211.     public void setMaxHitpoints(int maxHitpoints) {
  212.  
  213.         assert isValidHitpoints(maxHitpoints, Integer.MAX_VALUE, Mode.COMBAT): "Wrong value for maxHitpoints!";
  214.  
  215.         this.maxHitpoints = maxHitpoints;
  216.  
  217.     }
  218.  
  219.    
  220.  
  221.     /**
  222.  
  223.      * Returns the hero's maximum hitpoints.
  224.  
  225.      * @return maxHitpoints
  226.  
  227.      */
  228.  
  229.     @Basic
  230.  
  231.     public int getMaxHitpoints() {
  232.  
  233.         return maxHitpoints;
  234.  
  235.     }
  236.  
  237.    
  238.  
  239.     /**
  240.  
  241.      * @return  returns the Lifeforms strength
  242.  
  243.      *          | return strength
  244.  
  245.      */
  246.  
  247.     public double getStrength() {return strength;}
  248.  
  249.    
  250.  
  251.     /**
  252.  
  253.      *
  254.  
  255.      * @post    Multiplies the hero's strength by factor and ensures the result is positive.
  256.  
  257.      *          | new.getStrength() == ExtraMath.round(strength*(double)Math.abs(factor), strengthPrecision)
  258.  
  259.      * @param value
  260.  
  261.      */
  262.  
  263.     public void multiplyStrength(int value) {
  264.  
  265.         this.strength = ExtraMath.round(strength*(double)Math.abs(value), strengthPrecision);
  266.  
  267.     }
  268.  
  269.    
  270.  
  271.     /**
  272.  
  273.      *
  274.  
  275.      * @post    Divides the hero's strength by factor and ensures the result is positive..
  276.  
  277.      *          | if( factor!=0 ) then
  278.  
  279.      *          |   new.getStrength() == ExtraMath.round(strength/(double)Math.abs(factor), strengthPrecision)
  280.  
  281.      *          | else
  282.  
  283.      *          |   new.getStrength() == getStrength()
  284.  
  285.      * @param factor
  286.  
  287.      */
  288.  
  289.     public void divideStrength(int value) {
  290.  
  291.         if(value!=0) {
  292.  
  293.             this.strength = ExtraMath.round(strength/(double)Math.abs(value), strengthPrecision);
  294.  
  295.         }
  296.  
  297.     }
  298.  
  299.    
  300.  
  301.     /**
  302.  
  303.      * function which create a key into the hashmap with a given key as name,
  304.  
  305.      * but only if the key didn't exist before..
  306.  
  307.      * @param   key
  308.  
  309.      * @throws  IllegalArgumentException
  310.  
  311.      *          when an null key or an empty key string is given
  312.  
  313.      * @throws  InvalidKeyException
  314.  
  315.      *          when key already exists
  316.  
  317.      * @post    the anchormap will have a key with the name of the parrameter.
  318.  
  319.      *          | getItem(key) == null
  320.  
  321.      */
  322.  
  323.     public void addAnchor(String key) throws IllegalArgumentException, InvalidKeyException
  324.  
  325.     {
  326.  
  327.         if(key.equals("") || key == null) throw new IllegalArgumentException(); // keys die niet mogen bestaan !
  328.  
  329.         if(anchor.containsKey(key)) throw new InvalidKeyException();
  330.  
  331.         anchor.put(key, null);
  332.  
  333.     }
  334.  
  335.    
  336.  
  337.     /**
  338.  
  339.      * funchtion which delete the given key and with this also the reference to the content.
  340.  
  341.      * @param key
  342.  
  343.      * @throws InvalidKeyException
  344.  
  345.      *          throw when the key doen't exists in the hero.
  346.  
  347.      */
  348.  
  349.     public void deleteAnchor(String key) throws InvalidKeyException {
  350.  
  351.         if (!anchor.containsKey(key))
  352.  
  353.             throw new InvalidKeyException();
  354.  
  355.         anchor.remove(key);
  356.  
  357.     }
  358.  
  359.    
  360.  
  361.     /**
  362.  
  363.      * function which change the content of the given key,
  364.  
  365.      * or makes a key with the given content
  366.  
  367.      * @param key
  368.  
  369.      * @param item
  370.  
  371.      * @throws  IllegalArgumentException
  372.  
  373.      *          | (key.equals("") || key == null)
  374.  
  375.      * @post    the anchormap will contain the entry with the given key and item
  376.  
  377.      *          | new.getItem(key) == item
  378.  
  379.      */
  380.  
  381.     public void joinItem(String key,Item item) {
  382.  
  383.         if(key.equals("") || key == null) throw new IllegalArgumentException(); // keys die niet mogen bestaan !
  384.  
  385.         if(item != null && (getCapacity()-getLoad()-item.getWeight()<0))
  386.  
  387.                 return;
  388.  
  389.         anchor.put(key, item);
  390.  
  391.     }
  392.  
  393.    
  394.  
  395.     /**
  396.  
  397.      *
  398.  
  399.      * @param   key (the name of the anchor)
  400.  
  401.      * @return  returns the item that is into the anchor with the given key
  402.  
  403.      *          | returns Entity;
  404.  
  405.      * @throws  IllegalArgumentException
  406.  
  407.      *          throws an illegal argument exception if you give a empty string or a null refference
  408.  
  409.      *          | (key == null || key.equals(""))
  410.  
  411.      * @throws  InvalidKeyException
  412.  
  413.      *          throws an invalid key exception if the key didn't exist into the hero's anchors.
  414.  
  415.      */
  416.  
  417.     public Item getItem(String key) throws IllegalArgumentException,InvalidKeyException {
  418.  
  419.         if(key.equals("") || key == null) throw new IllegalArgumentException();
  420.  
  421.         if (!anchor.containsKey(key))
  422.  
  423.             throw new InvalidKeyException();
  424.  
  425.         return anchor.get(key);
  426.  
  427.     }
  428.  
  429.    
  430.  
  431.     /**
  432.  
  433.      * function for getting an key when you know only a value of an item
  434.  
  435.      * @param value
  436.  
  437.      *          a value of dukats
  438.  
  439.      * @return  returns a key when there is an item which have the same value;
  440.  
  441.      * @return  returns null if there was no item with the given value;
  442.  
  443.      */
  444.  
  445.     public String getKeyByValue(int value){
  446.  
  447.         for(String i:anchor.keySet()) {
  448.  
  449.             try {
  450.  
  451.                 if(getItem(i).getValue() == value)
  452.  
  453.                     return i;
  454.  
  455.             } catch (IllegalArgumentException e) {
  456.  
  457.                 // will not be throw because only valid keys will be used
  458.  
  459.             } catch (InvalidKeyException e) {
  460.  
  461.                 // will not be throw because only valid keys will be used
  462.  
  463.             }
  464.  
  465.         }
  466.  
  467.         return null;
  468.  
  469.     }
  470.  
  471.    
  472.  
  473.     protected String getKeyWithMaxValue() {
  474.  
  475.         String currentMaxKey = null;
  476.  
  477.         int maxValue=0;
  478.  
  479.         for(String i:anchor.keySet()) {
  480.  
  481.             try {
  482.  
  483.                 if(getItem(i).getValue()> maxValue) {
  484.  
  485.                     maxValue = getItem(i).getValue();
  486.  
  487.                     currentMaxKey = new String(i);
  488.  
  489.                 }
  490.  
  491.             } catch (IllegalArgumentException e) {/*will never be throw*/
  492.  
  493.             } catch (InvalidKeyException e) {/*will never be throw*/}
  494.  
  495.         }
  496.  
  497.         return currentMaxKey;
  498.  
  499.     }
  500.  
  501.    
  502.  
  503.     /**
  504.  
  505.      * returns a key of an object if the objects are the same.
  506.  
  507.      * @param   item
  508.  
  509.      * @return  key of the item, or null if the item isn't in the anchor
  510.  
  511.      */
  512.  
  513.     public String getKeyByItem(Item item) {
  514.  
  515.         for(String i:anchor.keySet()) {
  516.  
  517.             if (anchor.get(i) == item)
  518.  
  519.                 return i;
  520.  
  521.         }
  522.  
  523.         return null;
  524.  
  525.     }
  526.  
  527.     /**
  528.  
  529.      * function for getting the hole hashmap as an array of content
  530.  
  531.      * @return Item[]
  532.  
  533.      */
  534.  
  535.     public Item[] getList() {
  536.  
  537.        
  538.  
  539.         return (Item[]) anchor.values().toArray();
  540.  
  541.     }
  542.  
  543.    
  544.  
  545.     /**
  546.  
  547.      * getter for the name
  548.  
  549.      * @return  name
  550.  
  551.      */
  552.  
  553.     public String getName() {
  554.  
  555.         return name;
  556.  
  557.     }
  558.  
  559.    
  560.  
  561.     /**
  562.  
  563.      * gives the current load
  564.  
  565.      * @return  weight
  566.  
  567.      *          return the weight of all the items the hero carry
  568.  
  569.      */
  570.  
  571.     public double getLoad() {
  572.  
  573.         Item[] list = getList();
  574.  
  575.         double load = 0;
  576.  
  577.         for(Item item:list)
  578.  
  579.             load+=item.getWeight();
  580.  
  581.         return load;
  582.  
  583.     }
  584.  
  585.    
  586.  
  587.     /**
  588.  
  589.      * checks if the Lifeform can carry that weight extra.
  590.  
  591.      * @param   weight
  592.  
  593.      * @return  getCapacity()-getLoad()-weight>=0?true:false;
  594.  
  595.      */
  596.  
  597.     public boolean canCarryWeight(double weight) {
  598.  
  599.         if (getCapacity() - getLoad() - weight >=0)
  600.  
  601.             return true;
  602.  
  603.         return false;
  604.  
  605.     }
  606.  
  607.    
  608.  
  609.     abstract public boolean isValidName(String name);
  610.  
  611.     abstract public int getProtection() throws IllegalArgumentException, InvalidKeyException;
  612.  
  613.     abstract public double getCapacity();
  614.  
  615.     /**
  616.  
  617.      * private enumeration for use of the Lifeforms hitpoint system
  618.  
  619.      */
  620.  
  621.     private static enum Mode {
  622.  
  623.         COMBAT,
  624.  
  625.         NORMAL
  626.  
  627.     }
  628.  
  629.     /**
  630.  
  631.      * sets the lifeform into combat mode
  632.  
  633.      */
  634.  
  635.     public void setBattle() {mode = Mode.COMBAT;}
  636.  
  637.     /**
  638.  
  639.      * set the lifeform into peace mode
  640.  
  641.      */
  642.  
  643.     public void setPeace() {mode = Mode.NORMAL;}
  644.  
  645.    
  646.  
  647.  
  648.  
  649.     public class AnchorIterator {
  650.  
  651.         String[] keys;
  652.  
  653.         int count;
  654.  
  655.         public AnchorIterator() {
  656.  
  657.             keys = (String[]) anchor.keySet().toArray();
  658.  
  659.             count = 0;
  660.  
  661.         }
  662.  
  663.        
  664.  
  665.         public boolean hasNextElement() {
  666.  
  667.             for(int c = count;c<keys.length;c++){
  668.  
  669.                 if (anchor.get(keys[c]) != null)
  670.  
  671.                     return true;
  672.  
  673.             }
  674.  
  675.             return false;
  676.  
  677.         }
  678.  
  679.        
  680.  
  681.         public Item getNext() {
  682.  
  683.             for(int c = count;c<keys.length;c++){
  684.  
  685.                 if (anchor.get(keys[c]) != null) {
  686.  
  687.                     count = c+1;
  688.  
  689.                     return anchor.get(keys[c]);
  690.  
  691.                 }
  692.  
  693.             }
  694.  
  695.         throw new NoSuchElementException();
  696.  
  697.         }
  698.  
  699.     }
  700.  
  701. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement