import java.util.*; // for Random public class Fighter // represents a Fighter in our adventure game { private String type; // the type of Fighter (name or "class" such as Dragon or Wizard) private Weapon weapon; // every Fighter will have a Weapon private Armor armor1; // main armor (worn armor) private Armor armor2; // shield if available private Treasure treasure; // the items that this Fighter has private int hitPoints; // number of points of damage this Fighter can take before dying private Random generator; // used for randomness during fighting, resting, drinking potions, using scrolls public Fighter() // no arg constructor { type="unknown"; weapon=new Weapon(); armor1=new Armor(); armor2=null; treasure=new Treasure(); hitPoints=1; generator=null; } public Fighter(String t, Weapon w, Armor a1, Treasure tr, int h, Random g) { type=t; weapon=w; armor1=a1; armor2=null; // no second piece of Armor treasure=tr; hitPoints=h; generator=g; } public Fighter(String t, Weapon w, Armor a1, Armor a2, Treasure tr, int h, Random g) { type=t; weapon=w; armor1=a1; armor2=a2; // has two pieces of Armor treasure=tr; hitPoints=h; generator=g; } // accessors public String getType() {return type;} public Weapon getWeapon() {return weapon;} public Armor getArmor1() {return armor1;} public Armor getArmor2() {return armor2;} // armor2 may be null public Treasure getTreasure() {return treasure;} public int getHitPoints() {return hitPoints;} public void takesDamage(int d) // Fighter takes damage either in fight or by using bad potion/scroll { hitPoints-=d; } public int getDefense() // return the amount of protection that this Fighter has taking into account possibly 2 pieces of Armor { if(armor1!=null&&armor2!=null) return armor1.getProtection()+armor2.getProtection(); else if(armor1!=null) return armor1.getProtection(); else return 0; } public void takeWeapon(Weapon other) // if other is better than weapon, replace weapon with other { if(weapon.isBetter(other)) { weapon=other; System.out.println("Taking " + other.getWeapon()); } } public void takeArmor(Armor other) // if other is better than armor1, replace armor1 with other { if(armor1.isBetter(other)) { armor1=other; System.out.println("Taking " + other.getArmor()); } } public void takeShield(Armor other) // if armor2 is null and other is not, or if other is better than armor2, then replace armor2 with other { if(other!=null&&(armor2==null||armor2.isBetter(other))) { armor2=other; System.out.println("Taking " + other.getArmor()); } } public boolean isAlive() // Fighter is alive if it still has hitPoints { if(hitPoints>0) return true; else return false; } public String toString() // output description of Fighter { if(armor2==null) return "Fighter: " + type + " has " + hitPoints + " hit points, Weapon of " + weapon.getWeapon() + ", Armor of " + armor1.getArmor() + " and treasure of " + treasure.getTreasure(); else return "Fighter: " + type + " has " + hitPoints + " hit points, Weapon of " + weapon.getWeapon() + ", Armor of " + armor1.getArmor() + " and " + armor2.getArmor() + " and treasure of " + treasure.getTreasure(); } public void usePotion() // impact of taking a potion { if(treasure.usePotion()) // test to make sure user has a potion to drink (and remove it from treasure) { int restoration; int temp=generator.nextInt(10)+1; // determine potion's impact if(temp==10) // potion damages Fighter by 1-20 restoration=-1*(generator.nextInt(20)+1); else if(temp>7) // potion heals for 21-30 restoration=generator.nextInt(10)+21; else if(temp>3) // potion heals for 11-20 restoration=generator.nextInt(10)+11; else restoration=generator.nextInt(6)+5; // potion heals for 5-10 hitPoints+=restoration; if(restoration<0) System.out.println("Oh no, potion damages you for " + (restoration*-1) + ", you now have " + hitPoints + " hit points"); else System.out.println("The potion heals you for " + restoration + " and you are now at " + hitPoints + " hit points"); } else System.out.println("You do not currently have a potion to take"); } public void useScroll() // impact of using a scroll { if(treasure.useScroll()) // test to make sure user has a scroll to use (and remove it from treasure) { int temp=generator.nextInt(20)+1; // determine scroll's impact if(temp==1) { // kills user System.out.println("Oh no, the scroll was dark magic and killed you!"); hitPoints=0; } else if(temp<=8) // has no affect System.out.println("The scroll had no affect at all on you"); else { hitPoints*=2; // doubles hit points System.out.println("Congratulations, the scroll doubled your hit points to " + hitPoints); } } else System.out.println("You do not have any scrolls to use"); } public boolean rest() // impact of trying to rest { int temp=generator.nextInt(2); if(temp==1) return false; // rest is interrupted else { temp=generator.nextInt(20)+1; // restores random hit points 1-20 hitPoints+=temp; System.out.println("You rested and healed by " + temp + " hit points and now have " + hitPoints); return true; } } public void fight(Fighter other) // fight this Fighter against other until one dies { int roll, damage, f1Protection=getDefense(), f2Protection=other.getDefense(); // get both Fighters' protection for use below while(isAlive()&&other.isAlive()) // battle until one dies { System.out.println(type + " is attacking " + other.getType()); // output start of fight message for(int i=0;if2Protection) // if > other's protection, score a hit { // compute damage, give other that much damage, output result damage=generator.nextInt(getWeapon().getMaxDamage()-getWeapon().getMinDamage())+1+getWeapon().getMinDamage(); other.takesDamage(damage); System.out.println(type + " scores a hit against " + other.getType() + " for " + damage + " damage, " + other.getType() + " now has " + other.getHitPoints() + " hit points"); } else System.out.println(type + " misses " + other.getType()); // its a miss, output result } if(other.isAlive()) // if other is still alive, now its other's turn { System.out.println(other.getType() + " is attacking " + type); for(int i=0;if1Protection) { damage=generator.nextInt(other.getWeapon().getMaxDamage()-other.getWeapon().getMinDamage())+1+other.getWeapon().getMinDamage(); takesDamage(damage); System.out.println(other.getType() + " scores a hit against " + getType() + " for " + damage + " damage, " + getType() + " now has " + getHitPoints() + " hit points"); } else System.out.println(other.getType() + " misses " + getType()); } } } if(isAlive()) // output the result of the fight System.out.println("Congratulations, " + getType() + " defeats " + other.getType()); else System.out.println("I'm sorry, " + other.getType() + " defeats " + getType()); System.out.println("-------------------------------------------------------------------------------------------------------------"); } }