Skip to main content
edited tags
Link
nhgrif
  • 25.3k
  • 3
  • 63
  • 129
Source Link
Mast
  • 13.5k
  • 12
  • 54
  • 119

Two-dimensional array, modified one field at a time

Title says it all. I'm basically initializing a two-dimensional array and changing the fields one at a time.

It might be a good idea to do this OO. A Grid class seems right, perhaps as a subclass of Array. So far I haven't been able to successfully port it. Pointers would probably be a good idea as well.

Note this is kind of a sandbox project at the moment. I'm a novice in JS and trying to wrap my head around how I can handle data efficiently (particularly memory efficient, CPU is not as relevant).

Partial reviews are also welcome.

Array.initialize = function(iRows, iColumns, cInit) {
    var array = [];
    for (var i = 0; i < iRows; ++i) {
        var columns = [];
        for (var j = 0; j < iColumns; ++j) {
            columns[j] = initial;
        }
       array[i] = columns;
    }
  return array;
}

Array.shoot = function(row, column, array) {
  array[row][column] = "H";
  return array;
}

var myGrid = Array.initialize(12, 12, "U");
myGrid = Array.shoot(5, 5, myGrid);
myGrid = Array.shoot(0, 4, myGrid);

console.log(myGrid);