Name Blood - Points Defense - Level Attack - Level: in Out Out
Name Blood - Points Defense - Level Attack - Level: in Out Out
/* * Fighter2.java
import java.util.Scanner; import java.util.Random; public class Fighter2 { private private private private String name; int blood_points; int defense_level; int attack_level;
public void input(){ Scanner kb = new Scanner(System.in); System.out.println("Read fighter information from the keyboard"); System.out.println("Enter a name for the fighter"); this.setName(kb.nextLine()); do{ System.out.println("What is the blood points of the fighter?"); System.out.println("Blood points should be less than or equal to 50"); this.setBlood_points(kb.nextInt());} while (this.getBlood_points()>50); do{ System.out.println("What is the defense level of the fighter?"); System.out.println("The sum of the defense level and attack level must be less than or equal to 50"); this.setDefense_level(kb.nextInt());} while (this.getDefense_level() > 50); do{ System.out.println("What is the attack level of the fighter?"); System.out.println("The sum of the defense level and attack level must be less than or equal to 50"); this.setAttack_level(kb.nextInt());} while (this.getDefense_level() + this.getAttack_level() > 50); System.out.println("==============================================================="); } /** * Preconditions: fighter 1 and fighter 2 * Postconditions: fighter 2 attacks fighter1; adjusts fighter1's blood level */ public boolean attack(Fighter2 fighter1){ Random random = new Random(); double defrand; double bldrand; defrand = random.nextDouble(); if (defrand< (10/fighter1.getDefense_level())) { //attack unsucessful System.out.println(this.getName() + " attacks " + fighter1.getName() + " but misses. " + fighter1.getName() + " has "+ fighter1.getBlood_points()+ " blood points left."); } else{ bldrand = random.nextDouble(); fighter1.setBlood_points(fighter1.getBlood_points() - (int) (bldrand * this.getAttack_level() + 0.5)); System.out.println(this.getName() + " hits " +fighter1.getName() +". "+ "He looses " + (int) (bldrand * this.getAttack_level() + 0.5) +" blood points. " +fighter1.getName() + " has "+ fighter1.getBlood_points() + " blood points left."); }
2
if (fighter1.getBlood_points()<=0){ return true;} //fighter1 lost else return false; } public boolean attack1(Fighter2 fighter2){ Random random = new Random(); double defrand; double bldrand;
defrand = random.nextDouble(); if (defrand< (10/fighter2.getDefense_level())){ System.out.println(this.getName() + " attacks " + fighter2.getName() misses. " + fighter2.getName() + " has "+ fighter2.getBlood_points()+ " blood points left."); } else{ bldrand = random.nextDouble(); fighter2.setBlood_points(fighter2.getBlood_points() - (int) (bldrand this.getAttack_level() + 0.5)); System.out.println(this.getName() + " hits " +fighter2.getName() +". looses " + (int) (bldrand * this.getAttack_level() + 0.5) +" blood points. " + fighter2.getName() fighter2.getBlood_points() + " blood points left."); } if (fighter2.blood_points<=0){ return true;} //fighter2 lost else return false;
+ " but
} public int getBlood_points() { return blood_points; } public void setBlood_points(int blood_points) { this.blood_points = blood_points; } public int getAttack_level() { return attack_level; } public void setAttack_level(int attack_level) { this.attack_level = attack_level; } public int getDefense_level() { return defense_level; } public void setDefense_level(int defense_level) { this.defense_level = defense_level; } public String getName() { return name; } public void setName(String name) { this.name = name; }
3
/* * Fight2.java
import java.util.Random; public class Fight2 { public static void main(String[]args){ Fighter2 fighter1 = new Fighter2(); fighter1.input(); Fighter2 fighter2 = new Fighter2(); fighter2.input(); Random random = new Random(); first int start = random.nextInt(2); System.out.println(start); if (start == 0){ attack first System.out.println(fighter2.getName() +" will attack first."); } else{ System.out.println(fighter1.getName() + " will attack first."); } int rounds; // loop for the fight for (rounds = 1; rounds <= 10 && fighter1.getBlood_points() > 0 && fighter2.getBlood_points() > 0; rounds++){ if (start == 0){ fighter2 goes first System.out.println("round "+ rounds + ": "); fighter2.attack(fighter1); fighter1.attack1(fighter2); System.out.println(); } else{ fighter1 goes first System.out.println("round "+ rounds + ": "); fighter1.attack1(fighter2); fighter2.attack(fighter1); System.out.println(); } } //displays results of the fight if (fighter1.getBlood_points() <=0) //fighter 1 looses System.out.println(fighter1.getName() + " is defeated!"); else if (fighter2.getBlood_points() <= 0) //fighter 2 looses System.out.println(fighter2.getName() + " is defeated!"); else //they tie System.out.println("Time is out. The game is a tie"); } } //fight where //tells user who will //makes fighter1
//makes fighter2
//fight where