Advertisement
Guest User

NWD kolokvium

a guest
Nov 16th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. <script src="ime.js" type="text/javascript" ></script>
  2.  
  3.  
  4. DeepClone----------------------------------
  5.  
  6. function copy(o) {
  7. var output, v, key;
  8. output = new o.constructor();
  9. for (key in o) {
  10. v = o[key];
  11. output[key] = (typeof v === "object") ? copy(v) : v;
  12. }
  13. return output;
  14. }
  15.  
  16.  
  17. Singleton----------------------------------
  18.  
  19. var Singleton = (function(){
  20.  
  21. this.getInstance = function() {
  22.  
  23. if(this.instance === undefined){
  24.  
  25. this.instance = Date.now();
  26.  
  27. }
  28.  
  29. return this.instance;
  30.  
  31. }
  32.  
  33. return this;
  34.  
  35. }
  36.  
  37.  
  38. Currying------------------------------------
  39.  
  40. function nest(fn, i, args) {
  41. return function (x) {
  42. args.push(x);
  43. if (i === fn.length) {
  44. return fn(...args);
  45. } else {
  46. return nest(fn, i + 1, args);
  47. }
  48. }
  49. }
  50.  
  51. function curry(fn) {
  52. if(fn.length === 0){
  53. return fn();
  54. }
  55. const args = [];
  56. return nest(fn, 1, args);
  57. }
  58.  
  59. Game---------------------------------
  60.  
  61. function NPC(name, hitpoints) {
  62. this.name = name;
  63. this.hitpoints = hitpoints;
  64. }
  65.  
  66. function Hero(name) {
  67. NPC.call(this, name, 100);
  68. this.damage = 10;
  69. this.critical = false;
  70. }
  71.  
  72. Hero.prototype = Object.create(NPC.prototype);
  73. Hero.prototype.constructor = Hero;
  74.  
  75. Hero.prototype.attack= function (defender) {
  76. defender.hitpoints -= this.critical? this.damage*1.5 : this.damage;
  77. console.log(this.name+' attacked '+ defender.name + ' with ' + (this.critical? this.damage*1.5 : this.damage)+' dmg');
  78.  
  79. if(this.critical && defender instanceof Hero){
  80. defender.critical = true;
  81. this.critical = false;
  82. }
  83. }
  84. Hero.prototype.status = function () {
  85. return this.name+' '+ this.hitpoints;
  86. }
  87.  
  88. function chance(min,array_len,notThisNumbeer) {
  89. min = min || 0;
  90. var rand = Math.round(Math.random()*array_len-1);
  91. if(rand === notThisNumbeer || rand < 0 || rand < min){
  92. return chance(min,array_len,notThisNumbeer)
  93. }
  94. return rand;
  95. }
  96.  
  97. var array = [];
  98. var N = chance(10,50,-1);
  99. for(var i =0 ;i < N ; i++){
  100. chance(0,2,-1) == 0 ? array.push(new Hero('Hero_'+i)): array.push(new NPC('NPC_'+i, 100));
  101. console.log(array[i]);
  102. }
  103.  
  104. array[chance(0,array.length,-1)].critical = true;
  105. while( array.length > 1){
  106. var attacker = chance(0,array.length,-1);
  107. var defender = chance(0,array.length,attacker);
  108.  
  109. if( array[attacker] instanceof Hero){
  110. array[attacker].attack(array[defender]);
  111. if(array[defender].hitpoints <= 0)
  112. array.splice(defender,1);
  113. }
  114. }
  115. console.log("The Winner is "+array[0].name +" and has: "+ +array[0].hitpoints+" hitpoints left");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement