Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

Yet Another Dueling Grounds Difficulty: Shield Appears to not Work

I have looked at many, if not most, of the related posts on this, to no avail. I am not sure that the problem is my code, per se, but I suspect more of my strategy. Nevertheless, I am quite stuck.

I do not have some of the features other people have, (e.g. hero.wait ()) and so on, which I would be inclined to use. I have the shield equipped, however, and it seems to not help.

Here is my code:

while(true) {
    var badguy = hero.findNearestEnemy();
    if (badguy) {
    hero.cast("chain-lightning", badguy);
 
        if (hero.isReady("chain-lightning")) {
             hero.cast("chain-lightning", badguy);
            }
        else {
        hero.shield();
         }
    }
}

Thank you in advance, I appreciate being able to learn Javascript in this bite-sized and enjoyable way.

Hello, and welcome to the forum. CodeCombat is definitely a great way to learn Javascript and overall code structure and syntax.

I believe that one line is causing you problems, the first chain-lightning command. Chain-Lightning has a cool-down period of 20 seconds and can only be used again after that time. If you call that command when your hero isn’t ready, your code won’t run anything until it completes that command (potentially waiting 20 seconds doing nothing). You are on the right track with checking before you call the command in the next set of code. Eventually, you will always want to check if you can run a command before calling it. The earlier levels are timed out just right so you don’t run into this problem.

Thank you. So, if I understand right, this is getting in the way of the use of the shield? I have ran into same problem even when I ran hero.attack().

In fact, I just updated my code to this:


while(true) {
    // Find and attack the enemy inside a loop.
    // When you're done, submit to the multiplayer ladder!
    var badguy = hero.findNearestEnemy();
    if (badguy) {
    hero.attack(badguy);
    then; 
    hero.shield();
    
    }
        else {
        hero.shield();
         }
    }

And again, the two guys strike at the same time, and die, at the same time. Thanks again.

(update) I am not sure why the code is all funny…with the red and what not…

Also, removing the then; changes nothing either.

I am not sure why the code is all funny…with the red and what not…

Did you remove just the then; like below? This will run both commands every time your hero finds the badguy. I chose a good sword and was able to beat the simple CPU. Now start adding your other commands to get a better attack.

    if (badguy) {
    hero.attack(badguy);
    hero.shield();
    }

FYI… The apostrophe is handled like single quotations and triggers the red until you close it.

Just an ' example' to show "how this works"

Yes, I did remove it. I even copied and pasted what you just provided now. Allow me to copy and past the new ammended code:

    var badguy = hero.findNearestEnemy();
    if (badguy) {
    hero.attack(badguy);
    hero.shield();
    }
        else {
        hero.shield();
         }
    }

No matter what I do (basically) my guy always dies in one blow. But, like I said, I have a shield equipped, and am using the command – I am at a bit of a loss.

You can also add “javascript” in the same line of the triple `

for example:
```javascript
// Yes this isn’t going to mess up the colors anymore!
```
results in:

// Yes this isn't going to mess up the colors anymore!
3 Likes

Cool!! :star_struck: I didn’t know that trick!!

Dying on one blow? The code isn’t as much the issue, but likely the rest of your gear. It sounds like you don’t have good armor to boost your overall health score.

When you attack, you open yourself up for an attack and take the full damage. And depending on the shield, it will only absorb a certain percentage of the damage. For fun, you can comment out the attack line using the # symbol just to see how the shield command provides some protection.

With the way your code is setup, you will always attack once and then shield once as long as your hero can find an enemy.

Is the shield perpetual? Or is it something that, for example, lasts only a 1s? I can try to add some more regarding health.

UPDATE: I upgraded my armor, and the opponent did as well! Using the code just lead to bashing each other until my guy died. At least there is more time to interact and strategize now. I will keep playing around, thank you.

Every method has its attributes: How long it takes to use? How long it will stay in use (spells or rings) How long it takes to use again (cool-down). When you click on the methods in the middle bar, it expands to show some of the primary attributes for each piece of gear.

For example: I have the Deflector Shield currently equipped. The shield method takes .324 seconds to run and block 79% of the damage. A short time to protect you. To keep the shield “active”, place it after a bunch of if/elif statements for your bigger attacks that take cool-down time and have shield as the else until you are ready to reuse them.

Shield%20attribute

1 Like

Ok, I did what you said…tried just using shield. BIG DIFFERENCE. So, the shield now is staying up, whereas before, I don’t even think I saw it come up. Starting to get somewhere…

Got it to work, though, still not 100% sure what the hang-up was. Thanks again. I will be back soon, I am sure.

Just to clarify, the reason your old code wasn’t working is that until you kill him/her there will always be a “badguy”, so the else will never trigger. And the hero.shield(); you put after the hero.attack(); lasts only .324 seconds (As Brooksy said), so you’ll attack, shield for a small amount of time, attack, shield etc… etc…

    var badguy = hero.findNearestEnemy();
    if (badguy) {
        hero.attack(badguy); 
        hero.shield(); // this lasts for a short amount of time before looping back to the line above.
    }
        else {
        hero.shield();
         }
    }

The way to keep your shield up is to do this (this wouldn’t be a great tactic in Dueling grounds, but it might work better in Cavern Survival):

while (true) {
    var enemy = hero.findNearestEnemy()
    if(enemy) {
        if(hero.isReady("cleave") {
            hero.cleave(enemy);
        }else {
            hero.shield();
        }
    }
}
// A similar tactic could also be used with power-up or maybe even with fire-traps! 
// It's up to you.

I hope this helps explain it a bit more to you. If you have more difficulties, could you post your current code so we can help you.
Thanks
:lion: :lion: :lion: