/** * A shambling undead terror, hungry for brains and the flesh of the living. * * @author Zach Tomaszewski * @version 12 Sept 2001 */ public class Zombie { private int location = 10; //distance down the hallway private int hitPoints = 10; //how much damage this zombie can take /** * Instructs the zombie to move. Currently, this means * the zombie advances one step down the hallway. */ public void move() { this.location = this.location - 1; } /** * This zombie takes damage from the given book, provided * the book's range is sufficient to reach the zombie. * * @return Whether the book actually hit the zombie. */ public boolean takeDamage(Book book) { if (book.getRange() < this.location) { return false; //book didn't make it }else { this.hitPoints = this.hitPoints - book.getDamage(); return true; } } /** * Query whether zombie is still mobile. */ public boolean isAlive() { return (this.hitPoints > 0); } /** * Returns this zombie's current location. * Currently, this means the zombie's distance down the hallway. */ public int getLocation() { return this.location; } /** * This zombie attacks the given victim zombie. * The zombie making the attack takes 1 point of damage, * while the victim takes 5 points. */ public void attack(Zombie victim) { this.hitPoints = this.hitPoints - 1; victim.hitPoints = victim.hitPoints - 5; } /** * Returns a string representation of this zombie, * including its current hit points. */ public String toString() { return "zombie (" + this.hitPoints + "hp)"; } /** * Creates two zombies, and has one attack the other. */ public static void main(String[] args) { //create two zombies Zombie z1 = new Zombie(); Zombie z2 = new Zombie(); System.out.println("zombie 1: " + z1); System.out.println("zombie 2: " + z2); System.out.println("The first zombie atttacks the second."); z1.attack(z2); System.out.println("zombie 1: " + z1); System.out.println("zombie 2: " + z2); System.out.println("Then the first zombie attacks himself."); z1.attack(z1); System.out.println("zombie 1: " + z1); System.out.println("zombie 2: " + z2); } }//end Zombie