Munchkin harvest


#1

Hi,
I’ve got problem with code.

while(true) {
    var enemy = hero.findNearestEnemy();
    if (hero.isReady("cleave")) {
        hero.cleave(enemy);
    }
    else {
        var enemy=hero.findNearestEnemy();
        if (enemy) {
            hero.attack(enemy);
        }
}
}

With that code hero live a little bit longer but munchkin killing him anyway.
Any idea what I’m doing wrong? Armor i think is ok long sword with the cleave function. I don’t see any solution in FAQ

:frowning:


#2

First check if an enemy exists.
If an enemy exists:
then check if your cleave ability is ready.
If it is ready:
cleave the enemy.
Else:
just attack.

The FAQ does not provide solutions to levels. The purpose of CodeCombat is to help people learn and not just copy off of others. It does, however, provide helpful advice and tips for those who require general assistance.


#3

I don’t want copy anything :wink: Just want to understand :slight_smile:
becouse I think my code should run correctly.
I check if an enemy exist
then check cleave ability
if it’s redy hero cleave
else
hero attack.
But as allways they are killing him and I don’t know why :frowning:


#4

Are you lining up your else statement with if enemy: or if hero.isReady("cleave"):?


#5
while(true){
    var enemy= hero.findNearestEnemy();
    if (hero.isReady("cleave"))
    hero.cleave(enemy);
    } else {
     hero.attack(enemy);
  }
}

so it’s look the same as the level help show it
ther is a loop
I’m looking for enemy
if.hero.isReady clave
else
and just attack the enemy.

I chect many diferent possibilitys of writing the code :frowning: I don’t understand what is wrong with my else

becouse in else section if I wriote:
findenemy or if enemy
and then attack the enemy the are killing hero as always


#6

I do not see an if enemy: statement in there. Be sure to include one or else the code will process even if there is no enemy to fight.


#7
while (true) {
    if (enemy) {
        var enemy = hero.findNearestEnemy();
        if (hero.isReady("clave"))
    }
    else {
        if (enemy) {
            hero.attack(enemy);
        }
    }

if I’m writing if enemy just in" if" section , red warning sign shown off. I added another one if enemy on "else "section as whose recommended but still nothing changed. hero is dead . It’s just a simple loop what is going on :confused:


#8

A few things:

  1. Define enemy before the if-statement.
  2. If you’re confused about the cleave-attack code block, here’s what I mean:
if (enemy) { //cleave-attack code block fits inside the if-statement
    if (hero.isReady("cleave")) {
        // cleave here
    }
    else {
        // attack here
    }
}

Sorry, I use Python, so my JavaScript sucks :sweat_smile:


#9

Oooo I am confused :grinning:
I think I’ve done all as you suggested. Don’t worry your Js is better then mine :wink:


#10

In the if (enemy) statement you forgot a starting bracket: {.
It should look like if (enemy){
Make sure you also add an end bracket to close the if statement: }


#11
while (true) {
    if (enemy) {
        if (hero.isReady("cleave")) {
            var enemy = hero.findNearestEnemy();
            hero.cleave(enemy);
        }
    } else {
        if (enemy) {
            hero.attack(enemy);
        }
    }
}

Like this?


#12

You don’t need that second if (enemy) {. The else-statement matches up with the if (hero.isReady("cleave") {, not the if (enemy) {.
Also the var enemy… is the first statement in the loop, and THEN the rest follows.


#13
while (true) {
    var enemy = hero.findNearestEnemy();
    if (enemy){
        if (hero.isReady("cleave")) {
            hero.cleave(enemy);
        }
    } else {
        hero.attack(enemy);
    }
}

I write something like this .
var statement is the first in the loop I deleted second if(enemy).
I suppose the problem is with the “{ }” something is wrong becouse hero cleave just one and doesn’t attack …


#14

You need to wrap the entire thing in the if (enemy) statement:

[while loop]
  [define variable enemy]
  [if enemy]
     [if cleave is ready]
       then cleave
     }else{
       then attack
  [close bracket for if enemy]
[close bracket for while]

The logic behind this is that this will infinite loop because true is always true. Then you define enemy, and check if there is one. If there is no enemy, your hero will continue to loop and do nothing until one appears. Once an enemy spawns, you check if your cleave has recharged again. If so, cleave, otherwise, just regularly attack (which has no cooldown)


#15

I don’t see the diferent between those two code :sweat:


#16

Your else-statement is not inside the if (enemy) {.


#17

First: hero.isReady needs a starting bracket if(hero.isReady("cleave")){
Then you need to add another end bracket to close the cleave. Then, indent the else statement and everything inside it.


#18

Ok, now I understand.
So the problem should be with “{}” in my code.
Now the code should run properly. Hero cleave and attack more then one time, live longer but not long enought to win the fight


#19

How much health do you have. That may be the problem. If that is the problem you should buy better armor with your gems. Also you may need to line up that else statement with the if statement.


#20

I’ve got 390 health so more then the gay on the help video from game :wink:
I bought long sword yesterday but maybe it’s not enough :confused: