Advertisement
Guest User

Untitled

a guest
Sep 20th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. import java.util.Random;
  2. import java.util.logging.ConsoleHandler;
  3.  
  4. public class PvPDuell
  5. {
  6.  
  7. /**
  8. * @param args
  9. */
  10. Object lock = new Object();
  11. private static int life1 = 1000;
  12. private static int life2 = 1000;
  13. private static int dazed1 = 0;
  14. private static int dazed2 = 0;
  15. Thread p1 = new Thread()
  16. {
  17.  
  18. public void run()
  19. {
  20.  
  21. while (life1 > 0 && life2 > 0)
  22. {
  23.  
  24. try
  25. {
  26. if (dazed1 == 0)
  27. {
  28. try
  29. {
  30.  
  31. sleep(900);
  32. } catch (Exception e)
  33. {
  34. continue;
  35. // TODO: handle exception
  36. }
  37.  
  38. } else
  39. {
  40.  
  41. sleep(6000);
  42. dazed1 --;
  43. continue;
  44. }
  45. } catch (InterruptedException ie)
  46. {
  47. }
  48. synchronized (lock)
  49. {
  50. if (life1 > 0 && life2 > 0 && dazed1 == 0)
  51. {
  52. int i = 0;
  53. if (new Random().nextInt(10) != 1)
  54. {
  55. i = new Random().nextInt(30) + 30;
  56. } else
  57. {
  58. i = 120;
  59. System.out.print("++++ Critical! ++++ ");
  60. }
  61. life2 -= i;
  62. System.out.println("Player 1 hits Player 2 with " + i
  63. + " Dmg HP Left:"+((life2<0)?0:life2));
  64. if (new Random().nextInt(15) == 3)
  65. {
  66. System.out.println("Player 2 is dazed");
  67. dazed2 ++;
  68. if(dazed2==1)
  69. p2.interrupt();
  70. }
  71. }
  72. }
  73.  
  74. }
  75. if (life2 <= 0)
  76. System.out.println("Player 2 is dead");
  77. }
  78.  
  79. // }
  80. };
  81. Thread p2 = new Thread()
  82. {
  83. public void run()
  84. {
  85. while (life1 > 0 && life2 > 0)
  86. {
  87. try
  88. {
  89. if (dazed2 == 0)
  90. {
  91. try{
  92. sleep(2000);
  93. }catch (InterruptedException ie)
  94. {
  95. continue;
  96. }
  97. } else
  98. {
  99.  
  100. sleep(6000);
  101. dazed2 --;
  102. continue;
  103. }
  104. } catch (InterruptedException ie)
  105. {
  106.  
  107. }
  108.  
  109. synchronized (lock)
  110. {
  111. if (life1 > 0 && life2 > 0 && dazed2 == 0)
  112. {
  113. int i=0;
  114. if (new Random().nextInt(10) != 1)
  115. {
  116. i = new Random().nextInt(100) + 100;
  117. } else
  118. {
  119. i = 300;
  120. System.out.print("++++ Critical! ++++ ");
  121. }
  122. life1 -= i;
  123. System.out.println("Player 2 hits Player 1 with " + i + " Dmg HP Left:"+((life1<0)?0:life1));
  124. if (new Random().nextInt(10) == 3)
  125. {
  126. System.out.println("Player 1 is dazed");
  127. dazed1 ++;
  128. if(dazed1==1)
  129. p1.interrupt();
  130. }
  131. }
  132. }
  133. }
  134. if (life1 <= 0)
  135. System.out.println("Player 1 is dead");
  136. }
  137.  
  138. };
  139.  
  140. public static void main(String[] args)
  141. {
  142. new PvPDuell().start();
  143. }
  144.  
  145. private void start()
  146. {
  147. p1.start();
  148. p2.start();
  149.  
  150. }
  151.  
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement