Advertisement
Silviya7

01.Wild West Adventure

Aug 14th, 2024 (edited)
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input){
  2.  
  3. const n=Number(input.shift());
  4. let AllCharacters ={};
  5.  
  6. for (let i = 0; i < n; i++) {
  7.     const [name,HP,bullets]= input.shift().split(' ');
  8.  
  9.     AllCharacters[name]={HP:Number(HP),bullets:Number(bullets)};
  10.    
  11. }
  12.  
  13.  
  14. let CmdCommand=input.shift();
  15.  
  16. while(CmdCommand !='Ride Off Into Sunset'){
  17.     let CmdCurrent=CmdCommand.split(' - ');
  18.     let name=CmdCurrent[1];
  19.     switch  (CmdCurrent[0]){
  20.         case 'FireShot':
  21.            
  22.             if(AllCharacters[CmdCurrent[1]].bullets >0){
  23.                 AllCharacters[CmdCurrent[1]].bullets -=1;
  24.  
  25.                 console.log(`${name} has successfully hit ${CmdCurrent[2]} and now has ${AllCharacters[name].bullets} bullets!`)
  26.             }
  27.             else{
  28.                 console.log(`${name} doesn't have enough bullets to shoot at ${CmdCurrent[2]}!`)
  29.            }
  30.             break;
  31.  
  32.  
  33.        case 'TakeHit':
  34.           const damage=Number(CmdCurrent[2]);
  35.           const attacker=CmdCurrent[3];
  36.  
  37.           AllCharacters[name].HP -=damage;
  38.  
  39.           if(AllCharacters[name].HP >0){
  40.                    console.log(`${name} took a hit for ${damage} HP from ${attacker} and now has ${AllCharacters[name].HP} HP!`)
  41.           }
  42.  
  43.           else{
  44.            console.log(`${name} was gunned down by ${attacker}!`)
  45.           }
  46.            break;
  47.        case 'Reload':
  48.        if(AllCharacters[name].bullets <6){
  49.  
  50.            const Reloaded=6-AllCharacters[name].bullets;
  51.            AllCharacters[name].bullets=6;
  52.            console.log(`${name} reloaded ${Reloaded} bullets!`);
  53.        }
  54.        else{
  55.            console.log(`${name}'s pistol is fully loaded!`)
  56.         }
  57.         break;
  58.  
  59.         case 'PatchUp':
  60.   let amount=Number(CmdCurrent[2]);
  61.   let recoveredhp=0;
  62.    if(AllCharacters[name].HP==100){
  63.     console.log(`${name} is in full health!`);
  64.    }
  65.    else{
  66. if( AllCharacters[name].HP + amount >100){
  67.     AllCharacters[name].HP=100;
  68.  
  69.      recoveredhp=100-AllCharacters[name].HP;
  70.     console.log(`${name} patched up and recovered ${recoveredhp} HP!`)
  71. }
  72.  
  73. else{
  74.     AllCharacters[name].HP +=amount;
  75.     recoveredhp=amount;
  76.     console.log(`${name} patched up and recovered ${recoveredhp} HP!`)
  77. }
  78.    }
  79.              break;
  80.  
  81.  
  82.     }
  83.     CmdCommand=input.shift();
  84.  
  85. }
  86.  
  87. let NewCharacters= Object.entries(AllCharacters)
  88.  
  89. for (const character of NewCharacters) {
  90.     if(character[1].HP>0){
  91.     console.log(`${character[0]}`);
  92.     console.log(`HP: ${character[1].HP}`);
  93.     console.log(`Bullets: ${character[1].bullets}`);
  94.     }
  95.     }
  96.  
  97. }
  98.  
  99. solve
  100.     (["2",
  101.     "Jesse 100 4",
  102.     "Walt 100 5",
  103.  
  104.     "FireShot - Jesse - Bandit",
  105.      "TakeHit - Walt - 30 - Bandit",
  106.      "PatchUp - Walt - 20" ,
  107.      "Reload - Jesse",
  108.      "Ride Off Into Sunset"])
  109.    
  110.  
  111.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement