Monster.java (4659B)
1 package Monster; 2 import java.io.BufferedReader; 3 import java.io.BufferedWriter; 4 import java.io.FileReader; 5 import java.io.FileWriter; 6 import java.io.PrintWriter; 7 import java.util.ArrayList; 8 import java.util.Scanner; 9 10 /** 11 * A big scary monster class 12 */ 13 public class Monster { 14 private MonsterType monsterType; 15 private String hairColor; 16 private int healthPoints; 17 private int powerPoints; 18 private boolean isHostile; 19 private Weapon weapon; 20 private ArrayList<Treasure> treasures; 21 22 /** 23 * Make a new scary monster 24 * @param monsterType The type of the monster 25 * @param hairColor The hair color of the monster 26 * @param isHostile If the monster is angry (maybe get it some food) 27 */ 28 public Monster(MonsterType monsterType, String hairColor, boolean isHostile) { 29 this.monsterType = monsterType; 30 this.hairColor = hairColor; 31 this.isHostile = isHostile; 32 this.treasures = new ArrayList<>(); 33 this.weapon = new Weapon(WeaponType.STICK, 1); 34 this.healthPoints = 20; 35 } 36 37 public Monster load(String path) throws java.io.FileNotFoundException { 38 Scanner sc = new Scanner(new BufferedReader(new FileReader(path))); 39 sc.useDelimiter("\r?\n|\n"); 40 41 String type = sc.nextLine(); 42 String hairColor = sc.nextLine(); 43 String hp = sc.nextLine(); 44 45 Monster newMonster = new Monster(MonsterType.valueOf(type), hairColor, true); 46 newMonster.setHealthPoints(Integer.parseInt(hp)); 47 48 while (sc.hasNextLine()) { 49 String treasureType = sc.nextLine(); 50 String treasureColor = sc.nextLine(); 51 String treasurePp= sc.nextLine(); 52 53 newMonster.addTreasure(new Treasure( 54 TreasureType.valueOf(treasureType), 55 treasureColor, 56 Integer.parseInt(treasurePp) 57 ) 58 ); 59 } 60 61 sc.close(); 62 return newMonster; 63 } 64 65 public void save(String path) throws java.io.IOException { 66 PrintWriter pr = new PrintWriter(new BufferedWriter(new FileWriter(path))); 67 68 pr.printf("%s\n%s\n%d\n", this.monsterType, this.hairColor, this.healthPoints); 69 this.treasures.stream().forEach(t -> t.saveTreasure(pr)); 70 71 pr.close(); 72 } 73 74 /** 75 * Setter for the monster type 76 * @param monsterType The new type of monster 77 */ 78 public void setMonsterType(MonsterType monsterType) { 79 this.monsterType = monsterType; 80 } 81 82 public MonsterType getMonsterType() { 83 return this.monsterType; 84 } 85 86 /** 87 * Setter for the monster's hair color 88 * @param hairColor The new hair of the monster 89 */ 90 public void setHairColor(String hairColor) { 91 this.hairColor = hairColor; 92 } 93 94 public String getHairColor() { 95 return this.hairColor; 96 } 97 98 /** 99 * Setter for the monster's health points 100 * @param healthPoints The new health of the monster 101 */ 102 public void setHealthPoints(int healthPoints) { 103 this.healthPoints = healthPoints; 104 } 105 106 public int getHealthPoints() { 107 return this.healthPoints; 108 } 109 110 /** 111 * Setter for the monster's big scary power points 112 * @param powerPoints The new power points 113 */ 114 public void setPowerPoints(int powerPoints) { 115 this.powerPoints = powerPoints; 116 } 117 118 public int getPowerPoints() { 119 return this.powerPoints; 120 } 121 122 /** 123 * Setter for the monsters hostility 124 * @param isHostile is the monster angry at you? 125 */ 126 public void setHostility(boolean isHostile) { 127 this.isHostile = isHostile; 128 } 129 130 public boolean getHostility() { 131 return this.isHostile; 132 } 133 134 /** 135 * Appends a new item to the monsters treasure, the monster cant have more than MAX_TREASURE treasure 136 * @param item The new bit of treasure 137 */ 138 public void addTreasure(Treasure item) { 139 treasures.add(item); 140 this.calculateHealthPoints(); 141 } 142 143 public int getNumTreasure() { 144 this.treasures 145 .stream() 146 .map((Treasure t) -> { 147 t.setTreasureColor("RED"); 148 return t; 149 }).forEach((Treasure t) -> {System.out.println(t.toString());}); 150 151 152 return this.treasures.size(); 153 } 154 155 public ArrayList<Treasure> getOwnedTreasure() { 156 return this.treasures; 157 } 158 159 /** 160 * A stringer for a Monster 161 * @return A string of a monster 162 */ 163 public String toString() { 164 return 165 "I am a big scary " + this.monsterType + 166 " with " + this.hairColor + " hair" + 167 " and a " + this.weapon.getWeaponType(); 168 } 169 170 /** 171 * Sets the health of the monster based on the weapons the monster has 172 */ 173 private void calculateHealthPoints() { 174 this.healthPoints = 0; 175 for (Treasure t : this.treasures) { 176 this.healthPoints += t.getPowerPoints(); 177 } 178 } 179 180 /** 181 * Setter for the weapon of the monster 182 * @param The new weapon of the monster 183 */ 184 public void setWeapon(Weapon weapon) { 185 this.weapon = weapon; 186 } 187 188 public int attack(Player p) { 189 int playerHealth = p.getHealthPoints() - this.weapon.getDamagePoints(); 190 return (playerHealth < 0) ? 0 : playerHealth; 191 } 192 }