проблемы с функциями прототипов (объектов) javascript - PullRequest
0 голосов

у меня есть этот объект (Tablero):

   function Tablero(){

    this.mapa = 
                 [[0,0,0,0,0,0],
                  [0,0,0,0,0,0],
                  [0,0,0,0,0,0],
                  [0,0,0,0,0,0],
                  [0,0,0,0,0,0],
                  [0,0,0,0,0,0]];


    this.habilitarPos = function(){
        var coorCompleta = this.disponible().split("-");        
        return [coorCompleta[0],coorCompleta[1]];       
    };

    this.disponible = function(){ 
            var nuevoDisp = []; //retorna un arreglo de las posiciones 
            this.mapa.forEach(function(fila,indexf) {
                fila.forEach(function(colum,indexc) {
                    if(colum === 0){
                        nuevoDisp.push(indexf+"-"+indexc);
                    }
                });
            });
            //retorna una posicion cualquiera del arreglo de posiciones vacias
            return nuevoDisp[Math.floor((Math.random() * nuevoDisp.length))]

    };  

    this.actualizar = function(elemento){
        if(this.mapa[elemento.posfila][elemento.poscol] !== 0){
            if(this.mapa[elemento.posfila][elemento.poscol].tipo == 3){
                this.mapa[elemento.posfila][elemento.poscol] =  elemento;           
            }else{
                this.mapa[elemento.posfila][elemento.poscol] =  4;
            }
        }else{
            this.mapa[elemento.posfila][elemento.poscol] =  elemento;
        }
    };

    this.quitarCaballo = function(elemento){
         if(this.mapa[elemento.posfila][elemento.poscol] == 4){ 
            if(elemento.tipo == 1){
                this.mapa[elemento.posfila][elemento.poscol] =  maquina;
            }else if(elemento.tipo == 2){
                this.mapa[elemento.posfila][elemento.poscol] =  usuario;
            }   
         }else{
             this.mapa[elemento.posfila][elemento.poscol] =  0;
         }
    };

    this.pintarme = function(){

        this.mapa.forEach(function(fila,indexf) { //recorrer mi mapa y pintar 0 = vacio, 4 = usuario y maquina, objeto = objeto
                fila.forEach(function(elemento,indexc){
                    if(elemento !== 0 && elemento !== 4){
                        switch(elemento.tipo) {
                                case 1://usuario
                                    $("#"+elemento.posfila+elemento.poscol).html("<img src='img/usuario.png' width='50px' height='50px' />");
                                    break;
                                case 2://maquina
                                    $("#"+elemento.posfila+elemento.poscol).html("<img src='img/maquina.png' width='50px' height='50px' />");
                                    break;                              
                                case 3://manzana
                                    $("#"+elemento.posfila+elemento.poscol).html("<img src='img/manzana.png' width='50px' height='50px' />");
                                    break;  
                                default:
                                    $("#"+indexf+indexc).html("");
                            } 
                    }else if(elemento == 4){
                                $("#"+indexf+indexc).html("<img src='img/UM.png' width='50px' height='50px' />");
                    }else{
                        $("#"+indexf+indexc).html("");
                    }
                });
            });
    }

}

** и имя переменной var tablero_Original: **

var tablero_Original = new Tablero();

затем я добавляю объект вмассив this.mapa, объект:

function Caballo(usu) {
    this.posinicial = tablero.habilitarPos();
    this.posfila = 0; //this.posinicial[0];
    this.poscol = 0; //this.posinicial[1];
    this.manzanas = 0;
    this.tipo = usu; // 1 usuario, 2 maquina
    this.dondeSaltar = function(){
        return movimientosValidos(parseInt(this.posfila),parseInt(this.poscol));
    };
    this.setPos = function(pf,pc) {     
            this.poscol = pc;
            this.posfila = pf;
        };
}


maquina = new Caballo(2);// start the maquina   
tablero_Original.actualizar(maquina); //add maquina in tablero

СЕЙЧАС, я хочу создать другой Tablero с той же таблицей maro

var tableroClon =  new Tablero();
tableroClon.mapa = tablero_Original.mapa;

Проблема заключается в том, что когда я использую функцию actuaizar (которая изменяет массив mapa объекта), изменяю mapa tableroClon и mapa tablero_Original:

usuario = new Caballo(1);// inicio el usuario   
tableroClon.actualizar(usuario); //ingreso el usuario en el tablero

Два объекта Tableroобновляются, т. е. мапа двух Tablero остаются неизменными.

...