Help on Golden Mirage using Java

I really need help on this level. I have been working on this for a long time and I kind of gave up. Can anyone give me advice to help me and fix my code? The problem with my code is that my hero does not even move. My code is below:

// Collect 10 real coins.

while (true) {
    var coins = hero.findItems();
    var real = coins[0];
    for(var i=0; i<coins.length; i++){
        var coin = coins[i];
        if (coin == real) {
            real = coins[i+1];
        }
    }    
    if (real) {
        hero.moveXY(real.pos.x, real.pos.y);
    }
}

1 Like

I’ve not played this level yet, but just from looking at your code I might be able to take a crack at helping. I think the problem that you are having is related to that if statement at the bottom. Are you attempting to check if the real variable is instantiated? Since it is declared above that should always equate to true, which makes me wonder why an If statement is needed.

Without knowing what you are looking for specifically, I would recommend trying to keep your code as simple as possible, and moving that For loop comparison into it’s own function. An added benefit of doing that is being able to break the function and return the value you are looking for as soon as you find it.

Also, you can use the hero.say() function to call out the values to make sure your loop is working correctly. I use this as a kind-of debug sometimes.

Hi @ToxicReaper,
This level is very hard, but not impossible.
You’re doing the right thing by using a for loop, now you have to add another one inside the first one.
If you’re still stuck (don’t worry if you are) post your new code and I’ll see what I can do.
Also make sure you read the hints (very carefully) and go back to the mountain levels where you’re doing the same kind of things.
:lion: :lion: :lion:

1 Like

A few big ideas.

  1. You need to compare each coin to every other coin, so at some point you will need a coin1 == coin2.
  2. As @Deadpool198 mentioned nesting a second for loop in this will allow you to get both coins using their indexes. Your code is set up correctly for the first coin
for(var i=0; i<coins.length; i++){
        var coin = coins[i];
  1. Make sure you aren’t comparing the same coin to itself giving you a false positive.
Hint

You can use the indexes to quickly check this. If index 1 == index 2 then it is the same coin.

  1. Consider that there may be only one coin that matches the first so you need to run through all of the coins in the second loop before moving on to the next coin or you will get a false positive.
1 Like