Player.java (1861B)
1 package Monster; 2 3 public class Player { 4 private int healthPoints; 5 private Weapon weapon; 6 private Treasure treasure[]; 7 private int treasureCount; 8 private String name; 9 private Room room; 10 11 public final int MAXTREASURE = 10; 12 13 public Player(int healthPoints, Weapon weapon, String name) { 14 this.healthPoints = healthPoints; 15 this.weapon = weapon; 16 this.treasure = new Treasure[MAXTREASURE]; 17 this.treasureCount = 0; 18 this.name = name; 19 } 20 21 public void setWeapon(Weapon weapon) { this.weapon = weapon; } 22 23 public void setRoom(Room room) { this.room = room; } 24 public Room getRoom() { return this.room; } 25 public Room moveNorth() { return (this.room = (this.room.getNorth() != null) ? this.room.getNorth() : this.room); } 26 public Room moveSouth() { return (this.room = (this.room.getSouth() != null) ? this.room.getSouth() : this.room); } 27 public Room moveEast() { return (this.room = (this.room.getEast() != null) ? this.room.getEast() : this.room); } 28 public Room moveWest() { return (this.room = (this.room.getWest() != null) ? this.room.getWest() : this.room); } 29 30 private boolean addtreasure(Treasure t) { 31 if (this.treasureCount < this.MAXTREASURE) this.treasure[this.treasureCount] = t; 32 else return false; 33 return true; 34 } 35 36 public int getHealthPoints() { return this.healthPoints; } 37 38 public void setHealthPoints(int healthPoints) { this.healthPoints = healthPoints; } 39 40 public int attack(Monster m) { 41 int monsterHealth = m.getHealthPoints() - this.weapon.getDamagePoints(); 42 if (monsterHealth < 0) { 43 for (Treasure t : m.getOwnedTreasure()) { 44 if (!addtreasure(t)) { 45 System.out.println("The players pockets are full, you leave the treasure"); 46 break; 47 } 48 } 49 } else return monsterHealth; 50 51 return 0; 52 } 53 54 public String toString() { 55 return "Player called " + this.name + 56 " with health " + this.healthPoints; 57 } 58 }