nikolayneykov

Untitled

Mar 1st, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function printDungeon(params) {
  2.     let player = {
  3.         health: 100,
  4.         coins: 0,
  5.         commandProcessor(content, value, room) {
  6.             switch (content) {
  7.                 case 'potion':
  8.                     let healthGain = Math.min(value, 100 - this.health);
  9.                     this.health += healthGain;
  10.                     console.log(`You healed for ${healthGain} hp.\nCurrent health: ${this.health} hp.`);
  11.                     break;
  12.                 case 'chest':
  13.                     this.coins += value;
  14.                     console.log(`You found ${value} coins.`);
  15.                     break;
  16.                 default:
  17.                     let monsterName = content;
  18.                     let monsterDamage = value;
  19.                     this.health -= monsterDamage;
  20.                     if (this.health <= 0) {
  21.                         console.log(`You died! Killed by ${monsterName}.\nBest room: ${room}`);
  22.                     } else {
  23.                         console.log(`You slayed ${monsterName}.`);
  24.                     }
  25.                     break;
  26.             }
  27.         }
  28.     };
  29.  
  30.     let rooms = params[0].split('|');
  31.  
  32.     for (let i = 0; i < rooms.length; i++) {
  33.         let room = rooms[i].split(' ');
  34.         let roomContent = room[0];
  35.         let roomValue = Number(room[1]);
  36.  
  37.         if (player.health > 0) {
  38.             player.commandProcessor(roomContent, roomValue, i + 1);
  39.         }
  40.     }
  41.  
  42.     if (player.health > 0) {
  43.         console.log(`You've made it!\nCoins: ${player.coins}\nHealth: ${player.health}`);
  44.    }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment