fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.util.Random;
  7.  
  8.  
  9. abstract class Player
  10. {
  11. private int health;
  12. public Player(int health)
  13. {
  14. this.health = health;
  15. }
  16.  
  17. public int getHealth()
  18. {
  19. return this.health;
  20. }
  21.  
  22. public void reduceHealth(int amount)
  23. {
  24. if(this.health - amount > 0)
  25. this.health -= amount;
  26. else
  27. this.health = 0;
  28. }
  29.  
  30. public void increaseHealth(int amount)
  31. {
  32. this.health += amount;
  33. }
  34.  
  35. public int attack(){
  36. Random power = new Random();
  37. return power.nextInt(25);
  38. }
  39.  
  40. }
  41.  
  42. class Enemy extends Player implements Cloneable
  43. {
  44. public Enemy(int health)
  45. {
  46. super(health);
  47. }
  48. @Override
  49. protected Object clone() throws CloneNotSupportedException {
  50. return super.clone();
  51. }
  52.  
  53. }
  54.  
  55. class Tower extends Player implements Cloneable
  56. {
  57. public Tower(int health)
  58. {
  59. super(health);
  60. }
  61.  
  62. @Override
  63. protected Object clone() throws CloneNotSupportedException {
  64. return super.clone();
  65. }
  66.  
  67. }
  68.  
  69. /* Name of the class has to be "Main" only if the class is public. */
  70. class Ideone
  71. {
  72. public static void main (String[] args) throws java.lang.Exception
  73. {
  74. // Players
  75. Tower t1 = new Tower(100);
  76. Enemy e1 = new Enemy(100);
  77. System.out.println(String.format("Enemy starting health: %d\nTower starting health: %d", e1.getHealth(), t1.getHealth()));
  78. Player attacker;
  79. Player defender;
  80. for(int i=0; i <50; i++)
  81. {
  82. Random whosTurn = new Random();
  83. int power = 0;
  84.  
  85. if(whosTurn.nextInt(1000) % 2 == 0)
  86. {
  87. attacker = t1;
  88. defender = e1;
  89. }
  90. else
  91. {
  92. attacker = e1;
  93. defender = t1;
  94.  
  95. }
  96. power = attacker.attack();
  97. defender.reduceHealth(power);
  98. System.out.println(String.format("%s attacks %s with %d strength", attacker.getClass().getSimpleName(), defender.getClass().getSimpleName(), power));
  99.  
  100.  
  101. if(defender.getHealth() == 0)
  102. {
  103. System.out.println(String.format("%s is dead", defender.getClass().getSimpleName()));
  104. break;
  105. } else {
  106. System.out.println(String.format("%s health reduced to %d", defender.getClass().getSimpleName(), defender.getHealth()));
  107. }
  108. }
  109.  
  110. }
  111. }
  112.  
Success #stdin #stdout 0.09s 2841600KB
stdin
Standard input is empty
stdout
Enemy starting health: 100
Tower starting health: 100
Tower attacks Enemy with 0 strength
Enemy health reduced to 100
Tower attacks Enemy with 19 strength
Enemy health reduced to 81
Tower attacks Enemy with 10 strength
Enemy health reduced to 71
Enemy attacks Tower with 5 strength
Tower health reduced to 95
Tower attacks Enemy with 18 strength
Enemy health reduced to 53
Tower attacks Enemy with 18 strength
Enemy health reduced to 35
Enemy attacks Tower with 10 strength
Tower health reduced to 85
Enemy attacks Tower with 4 strength
Tower health reduced to 81
Enemy attacks Tower with 24 strength
Tower health reduced to 57
Enemy attacks Tower with 19 strength
Tower health reduced to 38
Enemy attacks Tower with 5 strength
Tower health reduced to 33
Enemy attacks Tower with 9 strength
Tower health reduced to 24
Tower attacks Enemy with 9 strength
Enemy health reduced to 26
Tower attacks Enemy with 7 strength
Enemy health reduced to 19
Enemy attacks Tower with 18 strength
Tower health reduced to 6
Tower attacks Enemy with 11 strength
Enemy health reduced to 8
Enemy attacks Tower with 0 strength
Tower health reduced to 6
Enemy attacks Tower with 24 strength
Tower is dead