Advertisement
pain2theworld

lab03-zad01

Nov 20th, 2019
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>Zad01</title>
  8. <script>
  9.  
  10. class NPC{
  11. constructor(name, hitpoints){
  12. this.name = name;
  13. this.hitpoints = hitpoints;
  14. }
  15.  
  16. status(){
  17. console.log(`${this.name} has ${this.hitpoints} health!`);
  18. }
  19. }
  20.  
  21. class Hero extends NPC{
  22.  
  23. constructor(name){
  24. super();
  25. this.name = name;
  26. this.hitpoints = 100;
  27. this.attackDmg = 10;
  28. }
  29.  
  30. attack(defender){
  31. defender.hitpoints -= this.attackDmg;
  32. if(defender instanceof Hero && this.attackDmg == 15){
  33. if(defender.hitpoints > 0){
  34. this.takeCritical();
  35. defender.giveCritical();
  36. }
  37. else{
  38. this.takeCritical();
  39. }
  40. }
  41. }
  42.  
  43. giveCritical(){
  44. this.attackDmg = 15;
  45. }
  46. takeCritical(){
  47. this.attackDmg = 10;
  48. }
  49. }
  50.  
  51. function startGame(){
  52. //inicialize
  53. var i = Math.floor(Math.random() * (50 - 10 + 1) ) + 10;
  54. var arrayClasses = [];
  55. while(i>0){
  56. tip = Math.floor(Math.random() * 2);
  57. if(tip == 0){
  58. var npc = new NPC(i,100);
  59. arrayClasses.push(npc);
  60. }
  61. else{
  62. var hero = new Hero(i);
  63. arrayClasses.push(hero);
  64. }
  65. i--;
  66. }
  67.  
  68. //give crit to random hero
  69. var HeroGetsCrit = arrayClasses[Math.floor(Math.random() * arrayClasses.length)];
  70. while(!(HeroGetsCrit instanceof Hero)){
  71. HeroGetsCrit = arrayClasses[Math.floor(Math.random() * arrayClasses.length)];
  72. }
  73. HeroGetsCrit.giveCritical();
  74.  
  75. //play game
  76. while(arrayClasses.length > 1){
  77. //debugger;
  78. var attacker = arrayClasses[Math.floor(Math.random() * arrayClasses.length)]
  79.  
  80. if(attacker instanceof Hero){
  81. var defenderIndex = Math.floor(Math.random() * arrayClasses.length)
  82. var defender = arrayClasses[defenderIndex]
  83. while(defender == attacker){
  84. var defenderIndex = Math.floor(Math.random() * arrayClasses.length)
  85. var defender = arrayClasses[defenderIndex]
  86. }
  87. //attacker.status();
  88. //defender.status();
  89. attacker.attack(defender);
  90. if(defender.hitpoints <= 0){
  91. arrayClasses.splice(defenderIndex, 1);
  92. }
  93. }
  94. }
  95. console.log(`Winner: ${arrayClasses[0].name} ${arrayClasses[0].hitpoints}hp!`);
  96. }
  97.  
  98. startGame();
  99.  
  100. </script>
  101. </head>
  102. <body>
  103.  
  104. </body>
  105. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement