Ad
Code
Diff
  • import java.util.List;
    import java.util.ArrayList;
    
    public class Main {
      public static void main(String[] args) {
        //put the battle in testing so that it is print to log
      }
    }
    
    
    class Fight {
      // do your magic here!
      List<Fighter> fighters = new ArrayList<Fighter>();
      
      public void introduceYourself(String name, int strength, int health) {
        System.out.println("Hi, my name is " + name + " (Strength: " + strength + ", Health: " + health + ")");
        fighters.add(new Fighter(name,strength,health));
      }
      
      public void startFight(){
        System.out.println("\n** The battle begins! **\n");
        while(fighters.size()>1){
          for(int i=0;i<fighters.size();i++){
            int randomTarget = (int)(Math.random()*fighters.size());                        //gets a random targets position in the list to attack
            while(randomTarget == i) randomTarget = (int)(Math.random()*fighters.size());   //if the target is yourself, find a new target
            
            fighters.get(i).attack(fighters.get(randomTarget));
            if(fighters.get(randomTarget).isDead()){
                fighters.remove(randomTarget);                                              //remove target from fighters list if they are dead
            }
          }
        }
        System.out.println("\n" + fighters.get(0).getName() + " is the CHAMPIOOOONNNNN!!!");
      }
      
    }
    
    class Fighter {
      private String name;
      private int strength;
      private int health;
      
      public Fighter(String name, int strength, int health){
        this.name = name;
        this.strength = strength;
        this.health = health;
      }
      
      public boolean isDead(){
        return health <= 0;
      }
      
      public void attack(Fighter target){
        int randomDamage = (int) ((Math.random() * strength)/2)+(strength/2);
        target.health -= randomDamage;
        System.out.println(target.name + " was attacked by " + name + " for "+ randomDamage + " damage! " + ((target.health > 0) ? "("+target.name+"'s health left " + target.health +")" : target.name.toUpperCase() + " HAS DIED!\n"));
      }
      
      public String getName(){
          return name;
      }
    }
    • import java.util.List;
    • import java.util.ArrayList;
    • public class Main {
    • public static void main(String[] args) {
    • Fight fight = new Fight();
    • // do your magic here!
    • System.out.println("Hello world!");
    • fight.introduceYourself("Mateusz");
    • //put the battle in testing so that it is print to log
    • }
    • }
    • class Fight {
    • // do your magic here!
    • static void introduceYourself(String name) {
    • System.out.println("Hi, my name is " + name);
    • List<Fighter> fighters = new ArrayList<Fighter>();
    • public void introduceYourself(String name, int strength, int health) {
    • System.out.println("Hi, my name is " + name + " (Strength: " + strength + ", Health: " + health + ")");
    • fighters.add(new Fighter(name,strength,health));
    • }
    • public void startFight(){
    • System.out.println("\n** The battle begins! **\n");
    • while(fighters.size()>1){
    • for(int i=0;i<fighters.size();i++){
    • int randomTarget = (int)(Math.random()*fighters.size()); //gets a random targets position in the list to attack
    • while(randomTarget == i) randomTarget = (int)(Math.random()*fighters.size()); //if the target is yourself, find a new target
    • fighters.get(i).attack(fighters.get(randomTarget));
    • if(fighters.get(randomTarget).isDead()){
    • fighters.remove(randomTarget); //remove target from fighters list if they are dead
    • }
    • }
    • }
    • System.out.println("\n" + fighters.get(0).getName() + " is the CHAMPIOOOONNNNN!!!");
    • }
    • }
    • class Fighter {
    • private String name;
    • private int strength;
    • private int health;
    • public Fighter(String name, int strength, int health){
    • this.name = name;
    • this.strength = strength;
    • this.health = health;
    • }
    • public boolean isDead(){
    • return health <= 0;
    • }
    • public void attack(Fighter target){
    • int randomDamage = (int) ((Math.random() * strength)/2)+(strength/2);
    • target.health -= randomDamage;
    • System.out.println(target.name + " was attacked by " + name + " for "+ randomDamage + " damage! " + ((target.health > 0) ? "("+target.name+"'s health left " + target.health +")" : target.name.toUpperCase() + " HAS DIED!\n"));
    • }
    • public String getName(){
    • return name;
    • }
    • }