Level Help: Golden Choice, Javascript


#1

I have tried as many solutions I can think of, even translating a working python solution so javascript, (didn’t work out too well). I need help on the level Golden Choice, and at this point I would take a solution as I have absolutely NO idea how this would be properly solved. Thanks for your help.

CURRENT CODE BELOW:

// You must collect the required amount of gold.
// The gate keeper 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());
var coins = hero.findItems();
// Find your path.
for(var y = zeroPoint.y; y < 10; y+=distanceY){
    for(var x = zeroPoint.x; x < 10; x+=distanceX){
    var coin = coins[x][y];
    var highestValue = 0;
    var highestCoin = [null];
    if(coin.value > highestValue){
        highestValue = coin.value;
        highestCoin = coin.push();
        hero.move(coin.pos);
    }
}
}

PREVIOUS CODE BELOW:

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;
}

var map = goldMap.reverse();
var rows = map.length;
var cols = map[0].length;

for(var r = 1;  r < rows; r++){
      map[r - 1] += [0];
      for(var c = 0; c < cols; c++){          
        if(map[r][c] === 0){
            continue;
        }
        if(c === 0){
            map[r][c] += map[r - 1][c + 1];
        }
        else if(c == cols - 1){
            map[r][c] += map[r - 1][c - 1];
        }
        else{
            map[r][c] += Math.max(map[r - 1][c - 1], map[r - 1][c + 1]);
        }
    }
}

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



#2

We do not give out solutions even if you have tried everything in this level. Please explain what goes wrong when running your code.