Essa tarde tava no pc e resolvi criar um pequeno jogo de campo minado no JS ai fui criar uma matriz e depois de algumas tentativas falhas fui pesquisar e descobri que JS não suporta matrizes.
Dai dps de quebra a cabeça cheguei ate aqui:
function grid(r,c,bomb){
this.qntBombs = bomb;
this.qntRows = r;
this.qntCols = c;
this.create = function(){
var rBomb, cBomb, content;
this.row = new Array();
for (var i = 0; i < this.qntRows; i++) {
this.row[i] = new cols();
this.row[i].createCols(this.qntCols);
}
for (var i = 0; i < this.qntBombs; i++){
rBomb = Math.floor(Math.random() * this.qntRows);
cBomb = Math.floor(Math.random() * this.qntCols);
content = this.row[rBomb].cols[cBomb].content; // essa e a linha 20 do erro
if (content == null){
this.row[rBomb].cols[cBomb].content = -1;
}else{
i--;
}
}
}
this.desenha = function(){
}
this.atualiza = function(){
}
}
function cols(){
this.cols = new Array();
this.createCols = function(row, qntCols){
for (var i = 0; i < qntCols; i++) {
this.cols[i] = new cell(row,i);
}
}
}
function cell(x,y){
this.x = x;
this.y = y;
this.content = null;
this.revelado = false;
}
var grid = new grid(20,20,20);
grid.create();
Basicamente criei um objeto grid e dentro um objeto row que é um array e dentro de cada posição do array criei um objeto cols que também é um array e dentro das posições cols um objeto cell.
(pelo menos e o que eu acho , sou um iniciante em OO)
Ai fui eu la ver se deu certo todo feliz só para encontrar isso no console
Uncaught TypeError: Cannot read property ‘content’ of undefined
at grid.create (grid.js:20)
at grid.js:40