Golden Choice - Javascript

I don’t even know what to say. Here’s my code:

// You must collect the required amount of gold.
// The gatekeeper will tell you how much you need.
// Always move in the direction of the exit.
// For each row you can take only one coin.
// Choose only one from the nearest coins in the next row.

// Distance between rows and coins.
var distanceX = 4;
var distanceY = 6;
var zeroPoint = {x: 14, y: 14};
var coinLines = 10;

function makeGoldMap(coins) {
    var template = [];
    for (var i = 0; i < coinLines; i++) {
        template[i] = [];
        for (var j = 0; j < 2 * coinLines - 1; j++) {
            template[i][j] = 0;
        }
    }
    for (var c = 0; c < coins.length; c++) {
        var row = Math.floor((coins[c].pos.y - zeroPoint.y) / distanceY);
        var col = Math.floor((coins[c].pos.x - zeroPoint.x) / distanceX);
        template[row][col] = coins[c].value;
    }
    return template;
}

// Prepare the gold map. It looks like:
// [[1, 0, 9, 0, 4],
//  [0, 1, 0, 9, 0],
//  [8, 0, 2, 0, 9]]
var goldMap = makeGoldMap(hero.findItems());

// Find your path.

It’s just the starting code. I just don’t know where to start. I’m not sure what the gold map is going to spit out, and it seems like all the coins have a value of three. I’ve looked at a couple of other people who have done it and I’m sure they understand it, but I don’t get what’s going on. Could someone help me figure out what is even happening in this level?

@Chaboi_3000 Would you consider posting a guide to this level like you did for Gridmancer Redux?

Okay, so I figured out something that works, but it only works about 20% of the time, and I’m sure it’s not what was intended for this level, here it is:

function findFirst(items) {
    let value = 0
    let coin = null;
    for (let item of items) {
        if (item.pos.y < 20 && item.value > value) {
            value = item.value;
            coin = item;
        }
    }
    return coin;
}
let first = findFirst(hero.findItems());
if (first) {
    hero.moveXY(first.pos.x, hero.pos.y);
    hero.moveXY(first.pos.x, first.pos.y);
}
function findNext(items) {
    let value = 0
    let coin = null;
    for (let item of items) {
        let distance = hero.distanceTo(item);
        if (distance < 10 && item.pos.y > hero.pos.y + 2) {
            if (item.value > value) {
                value = item.value;
                coin = item;
            }
        }
    }
    return coin;
}
while(true) {
    let next = findNext(hero.findItems());
    if (next) {
        hero.moveXY(next.pos.x, next.pos.y);
    }
}

I guess that is / could be the solution, but I really do want to know how it is supposed to be done.

I have a similar solution :confused: trying to make one that works 100% of the time

Okay, so I’ve done some stuff to the code, and here’s part of the code:

for (let ro = 0; ro < rows; ro ++) {
    if (ro === 0) {
        var co = map[ro].indexOf(Math.max(map[ro]));
        hero.moveXY(ro * distanceX + 14, 7);
    } else {
        if (map[ro][co + 1] > map[ro][co - 1]) {
            co += 1;
        } else if (map[ro][co + 1] < map[ro][co - 1]) {
            co -= 1;
        }
    }
    hero.moveXY(co * distanceX + 14, ro * distanceY + 14)

However, the second hero.moveXY() action is bringing up an error. It says "hero.moveXY() requires two numbers as arguments. x is not a number." I think that there is a problem with var co = ..., but I’m not sure what it is. I called co with the global variable, var, but when I say co, it asks what co is. Isn’t var a global variable?

2 things, 1. don’t use var, it… behaves weirdly, 2. you have to define it outside of the if or it won’t be defined when ro !== 0

Okay, that kinda worked. Now I’m just weaving between the coins. I think that there must be something else wrong with my code.