Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

Golden Choice Javascript Help

I have been working on this level for a while and can not seem to reach a solution. With this code, my ninja does not move and the level runs out of time. I would appreciate some guidance, thanks!

var distanceX = 4;
var distanceY = 6;
var zeroPoint = {x: 14, y: 14};
var lines = 10;

function makeGoldMap(coins) {
    var template = [];
    for (var i = 0; i < lines; i++) {
        template[i] = [];
        for (var j = 0; j < 2 * lines - 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());
for(var i = 0; i < hero.findItems; i++){
    var coin = hero.findItems[i];
    var row = (coin.pos.y - zeroPoint.y) / distanceY;
    var col = (coin.pos.x - zeroPoint.x) / distanceX;
    goldMap[row][col] = coin.value;
}

// Find your path.
var map = goldMap;
var rows = map.length;
var cols = map[0].length;

for(var r = 1; r < rows.length; r++){
    map[r-1] += [0];
    for(var c = 0 ; c<cols.length; 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] += max(map[r-1][c-1], map[r-1][c+1]);    
        }
    }
}


for(var r = 0; r < rows.length; r++){
    if(r===0){
         var c = map[r].index(max(map[r]));
         hero.moveXY(c*distanceX+14, 7);
    } else {
        if(map[r][c+1] > map[r][c-1]){
            c+=1;    
        } else{
            c -= 1;
            hero.say("Next: " + (c*distanceX+14) + ", " + (r*distanceY+14));
        }
    }
    hero.moveXY(c*distanceX + 14, r*distanceY + 14);
}

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.