Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <script src="ime.js" type="text/javascript" ></script>
- DeepClone----------------------------------
- function copy(o) {
- var output, v, key;
- output = new o.constructor();
- for (key in o) {
- v = o[key];
- output[key] = (typeof v === "object") ? copy(v) : v;
- }
- return output;
- }
- Singleton----------------------------------
- var Singleton = (function(){
- this.getInstance = function() {
- if(this.instance === undefined){
- this.instance = Date.now();
- }
- return this.instance;
- }
- return this;
- }
- Currying------------------------------------
- function nest(fn, i, args) {
- return function (x) {
- args.push(x);
- if (i === fn.length) {
- return fn(...args);
- } else {
- return nest(fn, i + 1, args);
- }
- }
- }
- function curry(fn) {
- if(fn.length === 0){
- return fn();
- }
- const args = [];
- return nest(fn, 1, args);
- }
- Game---------------------------------
- function NPC(name, hitpoints) {
- this.name = name;
- this.hitpoints = hitpoints;
- }
- function Hero(name) {
- NPC.call(this, name, 100);
- this.damage = 10;
- this.critical = false;
- }
- Hero.prototype = Object.create(NPC.prototype);
- Hero.prototype.constructor = Hero;
- Hero.prototype.attack= function (defender) {
- defender.hitpoints -= this.critical? this.damage*1.5 : this.damage;
- console.log(this.name+' attacked '+ defender.name + ' with ' + (this.critical? this.damage*1.5 : this.damage)+' dmg');
- if(this.critical && defender instanceof Hero){
- defender.critical = true;
- this.critical = false;
- }
- }
- Hero.prototype.status = function () {
- return this.name+' '+ this.hitpoints;
- }
- function chance(min,array_len,notThisNumbeer) {
- min = min || 0;
- var rand = Math.round(Math.random()*array_len-1);
- if(rand === notThisNumbeer || rand < 0 || rand < min){
- return chance(min,array_len,notThisNumbeer)
- }
- return rand;
- }
- var array = [];
- var N = chance(10,50,-1);
- for(var i =0 ;i < N ; i++){
- chance(0,2,-1) == 0 ? array.push(new Hero('Hero_'+i)): array.push(new NPC('NPC_'+i, 100));
- console.log(array[i]);
- }
- array[chance(0,array.length,-1)].critical = true;
- while( array.length > 1){
- var attacker = chance(0,array.length,-1);
- var defender = chance(0,array.length,attacker);
- if( array[attacker] instanceof Hero){
- array[attacker].attack(array[defender]);
- if(array[defender].hitpoints <= 0)
- array.splice(defender,1);
- }
- }
- 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