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

[SOLVED] Golden Choice (JavaScript) Help!

I need some help with Golden Choice. I’ve got some code but it doesn’t seem to work properly. My strategy is to start at every coin and go through the highest value path from that start coin. After checking all the paths and deciding which one has the highest sum, the hero should take that path through an array with left or right instructions.

I got to the hero to hero.say(goldMap[0]) and he said 19 numbers. How does that make sense if each row has 10 or 9 coins? Maybe I’m misunderstanding what the goldMap looks like?

My hero seems to always take a zigzag path and doesn’t choose the highest value path either. Sometimes, the hero tries to move offscreen to the right at the start. Any help would be great!

// 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());
// Find your path.
var pathCoinIndex = []; //array that tells hero to go left or right 4, each index is a row
var sum = 0; // sum of all the values of the coins in a path
var highestSum = 0; //highest sum found out of all paths
var coin1 = 0; //coin on left when comparing two coins
var coin2 = 0;//coin on right when comparing two coins
var startOfRoute = 0; //coin at beginning of path
var currentCoin = 0; //coin currently being checked
var chosenPath = [];

//for every coin in first row
for (var startCoin = 0; startCoin < goldMap[0].length - 1; startCoin++) {
    pathCoinIndex = [];
    sum = goldMap[0][startCoin];
    currentCoin = startCoin;
    for(var row = 0; row < 9; row++){
        if(row % 2 === 0){
            if(currentCoin === 0){
                coin1= 0;
                coin2 = goldMap[row+1][currentCoin];
            }else if(currentCoin == 9){
                coin2 = 0;
                coin1 = goldMap[row+1][currentCoin-1];
                currentCoin = 8;
            }else{
                coin1 = goldMap[row+1][currentCoin-1];
                coin2 = goldMap[row+1][currentCoin];
                if(coin1 > coin2){
                    currentCoin = currentCoin - 1;
                }
            }
        }else{
            coin1 = goldMap[row+1][currentCoin];
            coin2 = goldMap[row+1][currentCoin+1];
            if(coin1 < coin2){
                    currentCoin = currentCoin + 1;
                }
        }
        if(coin1 > coin2){
            pathCoinIndex[row] = -4;
            sum += coin1;
        }else{
            pathCoinIndex[row] = 4;
            sum += coin2;
        }
    }
    if(sum > highestSum){
        highestSum = sum;
        startOfRoute = startCoin;
        chosenPath = pathCoinIndex;
    }
}


hero.moveXY(14 + (8 * startOfRoute), hero.pos.y);
hero.moveXY(hero.pos.x, hero.pos.y + 8);
for (var i = 0; i < chosenPath.length; i++) {
    hero.moveXY(hero.pos.x + chosenPath[i], hero.pos.y + 6);
}
hero.moveXY(hero.pos.x, hero.pos.y + 8);

Solution was that the arrays were 18 long with zeros in between. The comment kind of says it but I didn’t really catch on.