The Trials - Help me understand my code

So, I’ve successfully worked through The Trials levels, but my code feels hacky in the sense that my character doesn’t complete one task before jumping to do another one. For example, what happens is that when the mushrooms appear as my character attacks and finishes an enemy, my character will go pick one up, then engage a new enemy, and then go pick up another mushroom, and then return to attack another enemy, continuing this back-and-forth until all items and enemies are gone.

I’ve tried to use flags to contain actions, in the sense:

if (flag){
hero move to flag
if(item){
pick up items
} hero.pickupFlag
}

but it doesn’t work.

Please help me understand whether it’s a nesting thing or some combination of nesting and while and iff loops…

Below is the general structure of the code, where the blocks are edited out.

while(true){
var nearest = null;
var nearestDistance = 9999;
var enemyIndex = 0;
var enemies = hero.findEnemies();

var greenFlag = hero.findFlag(“green”);

[code block]

//Is this enemy closer than the closest we’ve seen

[code block]

}
}

if(nearest){

[code block]
}

}
}
} if(item){

[code block]
} if (greenFlag){

[code block]
}
}

Please post your code according to our community guidelines. Also give more information about what is wrong with your code. Also a screenshot would be helpful.

-@Luke10
:fox_face::fox_face::fox_face:

Mod edit: Please be nice.

Please learn to post your code properly. There are lots of people here that are able to assist you but we must see the structure of your code. The structure is only displayed when you post it properly. It’s very easy and only takes a tiny bit of effort.

1 Like

It’s not automated, you just need to format your code as Munkey Shynes said. (I’m just making sure alex2 knows these aren’t automated Munkey)
We can’t read your code to look for errors if you haven’t formatted your code, there are no indents.
:lion: :lion: :lion:

1 Like

@alex2

It seems like you are being specific with the hero.findFlag("green") code above. Looking over my approach I didn’t seem to need to specifically use “green”. Have you tried to use hero.findFlag(); ?

And yes, the below construct works:

var flag = hero.findFlag();
if (flag) {
  // do something
}

A few pointers:

  1. If you reference flag in your if statement, ensure it is being declared in a var statement before the if.
  2. Ensure item is also declared in a var statement.
  3. Loops help repeat actions and reuse code.
    a. Question to think about: If you do not include the action inside the loop will it repeat or run once?
    b. Question to think about: If you nest your if statements, will the if inside of the if get a chance to run? Remember that to run code inside an if statement the logic expression must resolve to true. When if statements are nested their expressions have to be added to come up with the qualifiying logic to know if that part of the code will run.

Example of nested if:

// always declare variables you will use later on
var flag = hero.findFlag();
var item = hero.findItem();
if ( flag ) {
    // statement area 1

    if ( item ) {
        // statement area 2
    } 
} 

In the above example, in order for the code to execute in statement area 2 the following condition must be true. “There are flags and there are items at the same instance.”

Remember when we initialize the variables with a var flag = hero.findFlag(); the value at that point in time will be what is stored in the flag variable. If the game changes state later on and new content appears, our original flag variable will be showing us an older point in time and not the current state. The flag variable will need to be updated as time progresses.

-Harry

2 Likes

Mod edit: “Be nice” is a basic rule on this board.