Advertisement
dddilian

Dungeonest Dark

Mar 16th, 2020
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(str) { //100т.
  2.     let health = 100;
  3.     let coins = 0;
  4.     let word = '';
  5.     let number = 0;
  6.     let roomCounter = 0;
  7.     let healthDiff = 0;
  8.     let died = false;
  9.     let arr = str[0].split(/[\s|]+/);
  10.     //['rat', '10', 'bat', '20', 'potion', '10', 'rat', '10', 'chest', '100', 'boss', '70', 'chest', '1000'];
  11.     //['cat', '10', 'potion', '30', 'orc', '10', 'chest', '10', 'snake', '25', 'chest', '110'];
  12.  
  13.     for (let i = 0; i < arr.length - 1; i += 2) {
  14.         word = arr[i];
  15.         number = Number(arr[i + 1]);
  16.         roomCounter++;
  17.         switch (word) {
  18.             case 'chest':
  19.                 console.log(`You found ${number} coins.`);
  20.                 coins += number;
  21.                 break;
  22.             case 'potion':
  23.                 if (number >= 100 - health) {
  24.                     healthDiff = 100 - health;
  25.                 } else {
  26.                     healthDiff = number;
  27.                 }
  28.                 if (health + number > 100) {
  29.                     health = 100;
  30.                 } else {
  31.                     health += number;
  32.                 }
  33.                 console.log(`You healed for ${healthDiff} hp.`);
  34.                 console.log(`Current health: ${health} hp.`);
  35.                 break;
  36.             default: //враг
  37.                 health -= number; //взима ни живот
  38.                 if (health > 0) {
  39.                     console.log(`You slayed ${word}.`)
  40.                 } else { //влизаме, ако сме умрели
  41.                     console.log(`You died! Killed by ${word}.`);
  42.                     console.log(`Best room: ${roomCounter}`);
  43.                     died = true;
  44.                 }
  45.                 break;
  46.         }
  47.         if (died) {
  48.             break;
  49.         }
  50.     }
  51.     if (!died) {
  52.         console.log(`You've made it!`);
  53.        console.log(`Coins: ${coins}`);
  54.        console.log(`Health: ${health}`);
  55.    }
  56. }
  57.  
  58. solve(["rat 10|bat 20|potion 10|rat 10|chest 100|boss 70|chest 1000"]);
  59. console.log();
  60. solve(["cat 10|potion 30|orc 10|chest 10|snake 25|chest 110"]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement