Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve (array) {
- // Constructing Dungeon string!
- let arr = array.toString();
- let newArray = []; // This array should be used!
- let word = '';
- let index = 0;
- let semiIndex = 1;
- for (let el = 0; el <= arr.length; el++) {
- if (semiIndex % 3 === 0) {
- newArray[index] = '|';
- index++;
- semiIndex = 1;
- }
- if (arr[el] === ' ' || arr[el] === '|'|| el === arr.length) {
- newArray[index] = word;
- index++;
- semiIndex++;
- word = '';
- } else {
- word += arr[el];
- }
- }
- // Walking in dungeon;
- let health = 100;
- let coins = 0;
- let bestRoom = 1;
- let ifMadeIt = true;
- for (let element = 0; element < newArray.length; element += 3) {
- let itemMonster = newArray[element];
- let number = newArray[element + 1];
- // Potion part
- if (itemMonster === 'potion') {
- let currentHealth = health;
- health += Number(number);
- if (health > 100) {
- health = 100;
- }
- console.log(`You healed for ${health - currentHealth} hp.`);
- console.log(`Current health: ${health} hp.`);
- // Chest part
- } else if (itemMonster === 'chest') {
- coins += Number(number);
- console.log(`You found ${number} bitcoins.`);
- // Monster part
- } else {
- health -= Number(number);
- if (health > 0) {
- console.log(`You slayed ${itemMonster}.`);
- } else {
- console.log(`You died! Killed by ${itemMonster}.`);
- ifMadeIt = false;
- break;
- }
- }
- bestRoom++;
- }
- if (ifMadeIt) {
- console.log("You've made it!");
- console.log(`Bitcoins: ${coins}`);
- console.log(`Health: ${health}`);
- } else {
- console.log(`Best room: ${bestRoom}`);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement