0

Picture of problem: http://puu.sh/5spfQ.png

My first question here. I hope I can explain it to a decent extent, to give you an idea of what I'm trying to do.

I am working on a small javascript game, where the only objective is to make money. One of the methods to make money is by hiring workers. However, there's a problem. For instance we have Worker A and Worker B. For some odd reason, whenever Worker B mines something, Worker A also receives the mined ore (so both seem to always have the same amount of ore). I can't seem to figure out why this is; they should not be receiving each others ore.

Note: >hired< workers are stored in the "employed" variable.

The fun part, the code:

*This function fills in placeholders to the employed variable for each possible worker for hire. *

    //add "placeholder data" to prevent future undefined/errors
    //and makes future modification easier, specially in workerMain()
    var o = [];
    for(ore in ores){o[ore] = 0;}

    for(w in workers){
        employed[w] = [0, o];
    }

So our employed variable may look something along the lines of:

('miner a' => [0, ['coal_ore' => 0,'diamond_ore' => 0]], 'miner b' => [0, ['coal_ore' => 0, 'diamond_ore' => 0]])

Now once they are hired, a loop that is ran every second will determine how much ore they have mined, and it should put any worker's mined ore in their respective spot.

function workerMain(){
    setTimeout(function(){
        for(e in employed){
            var wObj = workers[e];
            var orePerSecond = Math.ceil(wObj.opm/60)*employed[e][0];
            var oresMined = generateOres(orePerSecond, wObj.pickaxe);

            //add newly mined ore count to worker's ore
            for(ore in oresMined){
                employed[e][1][ore] += oresMined[ore];

                //we aren't storing the ore, so let's go ahead and sell it
                money += ores[ore].worth*oresMined[ore];
            }
        }

        updateValues();
        workerMain();
    }, 1000);
}

But again, somehow if Miner A mines a coal ore, somehow Miner B will also get that coal ore.

Hope I've explained my problem thoroughly. If you can't seem to find the problem within this code, it may lie within these sibling functions:

http://pastebin.com/2WgT8Acg

  • The worker main I see a function called update values, any way we could see this as well? – Trendy Nov 25 '13 at 00:38
  • You shouldn't be looping arrays with `for...in` but a regular `for` loop. Also you forgot to declare all the loop variables so they become implicit globals. – elclanrs Nov 25 '13 at 00:40
  • `for (ore in ores)` makes it look like `ore` is a property key, and `o = []` followed by `o[ore]` makes it look like you think arrays are associative arrays... can I answer assuming this is the case? – sqykly Nov 25 '13 at 00:42
  • @elclanrs Thanks! Will be sure to fix the declaration issues. And wouldn't say for...in/for is more of a preference thing? – user3029571 Nov 25 '13 at 00:43
  • NO! `for(...in...)` is different from `for()`. Please see http://es5.github.com/ and read very very thoroughly. – sqykly Nov 25 '13 at 00:44

1 Answers1

4

Arrays and objects in Javascript are passed by reference so in this code:

var o = [];
for(ore in ores){o[ore] = 0;}

for(w in workers){
    employed[w] = [0, o];
}

You're creating one variable o and assigning references to it to every miner. Ergo, every miner is updating the same set of ores.

You need to create a new array for each miner:

var o;

for(w in workers){
    o = [];
    for(ore in ores){o[ore] = 0;}
    employed[w] = [0, o];
}

I dare say some Javascript guru could make a more efficient version!

  • I'd probably recommend `o = { }` since `o` doesn't look like it is being used an array. Any solution will boil down to a `for...in` loop but some of the fancier ones will hide that behind `$.extend`, `_.extend`, ... – mu is too short Nov 25 '13 at 00:41
  • Woo-hoo! That was the problem. I did not know JavaScript did this. I appreciate the quick help and new knowledge. :) – user3029571 Nov 25 '13 at 00:42
  • http://stackoverflow.com/questions/728360/most-elegant-way-to-clone-a-javascript-object – Havenard Nov 25 '13 at 00:42