Advertisement
Radoslav_03

kurec

Oct 23rd, 2023
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.41 KB | None | 0 0
  1. import java.util.Random;
  2.  
  3. interface Monster{
  4.     float attack();
  5. }
  6.  
  7. class Zombie implements Monster{
  8.     @Override
  9.     public float attack(){
  10.         return 5;
  11.     }
  12. }
  13.  
  14. class Vampire implements Monster{
  15.     @Override
  16.     public float attack(){
  17.         return 10;
  18.     }
  19. }
  20.  
  21. class Dragon implements Monster{
  22.     @Override
  23.     public float attack(){
  24.         return 20;
  25.     }
  26. }
  27.  
  28. class MonsterFactory{
  29.     public static Monster createMonster(Random random){
  30.         int monster_type = random.nextInt(3);
  31.         return switch (monster_type) {
  32.             case 0 -> new Zombie();
  33.             case 1 -> new Vampire();
  34.             case 2 -> new Dragon();
  35.             default -> null;
  36.         };
  37.     }
  38. }
  39. public class Game {
  40.     public static void main(String[] args){
  41.         Random random = new Random();
  42.         Monster[] monsters = new Monster[5];
  43.         float total_attack = 0;
  44.  
  45.         for (int i = 0; i < 5; i++){
  46.            monsters[i] = MonsterFactory.createMonster(random);
  47.            total_attack += monsters[i].attack();
  48.            System.out.println("Monster " + (i + 1) + " attacks with " + monsters[i].attack() + " damage.");
  49.         }
  50.  
  51.         System.out.println("Total monsters attack damage: " + total_attack);
  52.  
  53.         if (total_attack < 50){
  54.             System.out.println("Hero wins!");
  55.         }
  56.         else{
  57.             System.out.println("Hero died!");
  58.         }
  59.     }
  60. }
  61.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement