Advertisement
Luninariel

textGame - Attack

Dec 11th, 2018
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. import java.time.LocalTime;
  2.  
  3. public class Attack {
  4.  
  5. /**
  6. *
  7. * An attack requires both the attacker and the defender to be alive.
  8. * If one or both are dead, then a "No Attack" message is returned.
  9. *
  10. * A random number between 0.0 <= 0.0 < 1.0. If this number is less than the attacker's aggressiveness, then
  11. * an attack occurs.
  12. * If the attack occurs the strength of the attack is calculated using the strength and stamina of the attacker.
  13. * If no attack occurs the ccreature's attack message is returned.
  14. *
  15. */
  16.  
  17. public static boolean isNight(){
  18. boolean night = false;
  19. LocalTime time = LocalTime.now();
  20. int currentHour = time.getHour();
  21. night = (currentHour <6 || currentHour >= 18);
  22. return night;
  23. }
  24.  
  25. private static String normalAttack(Entity attacker, Entity defender){
  26. String message = "Nothing So far";
  27. if(attacker.isDead() || defender.isDead()){
  28. message = "One or both players are dead. No attack";
  29. }else {
  30. boolean attack = Math.random() < attacker.getAggressiveness();
  31. if (attack) {
  32. double force = attacker.getStrength() * Math.random();
  33. double damage = force / defender.getStamina();
  34. defender.subtractHealth(damage);
  35. message = String.format("%s %d %s with %d%% force and doing %d%% damage",
  36. attacker.symbol, attacker.getId(), attacker.getAttackMessage(), (int)Math.round(force*100),
  37. (int)Math.round(damage*100));
  38. }else{
  39. message = String.format("%s %d %s", attacker.symbol,attacker.getId(), attacker.getPassiveMessage());
  40. }
  41. }
  42. return message;
  43. }
  44.  
  45.  
  46.  
  47.  
  48. public static String attack(Entity attacker, Entity defender) {
  49.  
  50. if(isNight()==true && (defender instanceof Nocturnal)){
  51. if(Math.random()>0.5){
  52. return normalAttack(attacker,defender);
  53. }
  54. else
  55. return String.format("%s %d has hidden", defender.getSymbol(),defender.getId());
  56. }
  57.  
  58. if (isNight()==false && ( attacker instanceof Nocturnal)){
  59. return String.format("%s",attacker.getPassiveMessage());
  60. }
  61. if(isNight()==true){
  62. return normalAttack(attacker, defender);
  63. }
  64. return normalAttack(attacker, defender);
  65. }
  66.  
  67. /**
  68. * This method has player1 attack player 2, and then player 2 attack player 1.
  69. *
  70. * @param player1
  71. * @param player2
  72. * @param round This is an integer that indicates round number. It is normally an incremented variable.
  73. *
  74. * There is no return value
  75. */
  76.  
  77. public static void round(Entity player1, Entity player2, int round){
  78. System.out.println("\nRound " + round);
  79. System.out.println(Attack.attack(player1, player2));
  80. System.out.println(Attack.attack(player2, player1));
  81. }
  82.  
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement