为什么我的输入插入到具有相同索引的两个diff数组中? (JS)

时间:2016-01-11 00:34:07

标签: javascript arrays indexing insert

我有这个js脚本完美地作为战舰工作,我想改变脚本,因为现在我必须手动添加船只数量,即使位置是随机生成的。 此时,我必须以这种方式添加船只。

ships: [{locations: ["", "", ""], hits: ["", "", ""]},
        {locations: ["", "", ""], hits: ["", "", ""]},
        {locations: ["", "", ""], hits: ["", "", ""]}],

我修改了脚本,现在它是ship: []

使用新脚本以某个位置输入位置并点击位置时,它会点击具有相同命中索引的所有对象。

我将提供完美的工作脚本和我添加的脚本。

工作脚本

var model = {
    boardSize: 7,
    numShips: 3,
    shipLength: 3,
    shipsSunk: 0,
    ships: [{locations: ["", "", ""], hits: ["", "", ""]},
            {locations: ["", "", ""], hits: ["", "", ""]},
            {locations: ["", "", ""], hits: ["", "", ""]}],
    fire: function(guess){
        for(var i = 0; i < this.numShips; i++){
            var ship = this.ships[i];
            var locations = ship.locations;
            var index = locations.indexOf(guess);
            if(index >= 0){
                ship.hits[index] = "hit";
                view.displayMessage('You hit my ship');
                view.displayHit(guess);
                if(this.isSunk(ship)){
                    view.displayMessage("You sunk one of my ship!");
                    this.shipsSunk++;
                }
                return true;
            }
        }
        view.displayMessage("You missed!!!~!~!~!");
        view.displayMiss(guess);
        return false;
    },
    isSunk: function(ship){
        for (var i = 0; i < this.shipLength; i++){
            if(ship.hits[i] !== "hit"){
                return false;
            }
        }
        return true;
    },
    generateShipLocations: function(){
        var row, column;
        var direction = Math.floor(Math.random() * 2);
        if(direction === 1){
            row = Math.floor(Math.random() * this.boardSize);
            column = Math.floor(Math.random() * (this.boardSize - this.shipLength));
        }else{
            row = Math.floor(Math.random() * (this.boardSize - this.shipLength));
            column = Math.floor(Math.random() * this.boardSize);
        }

        var newShipLocations = [];
        for(var i = 0; i < this.shipLength; i++){
            if(direction === 1){
                newShipLocations.push(row + "" + (column + i));
            }else{
                newShipLocations.push((row + i) + "" + column);
            }
        }
        return newShipLocations;
    },
    generateShip: function(){

        var locations;
        for(var i = 0; i < this.numShips; i++){
            do{
                locations = this.generateShipLocations();
            }while(this.collision(locations));
            this.ships[i].locations = locations;
        }
    },
    collision: function(locations){
        for(var i = 0; i < this.numShips; i++){
            var ship = model.ships[i];
            for(var j = 0; j < this.shipLength; j++){
                if(ship.locations.indexOf(locations[j]) >= 0){
                    return true;
                }
            }
        }
        return false;
    },
};

我在模型中添加了一个函数,修改了generateShip()ships

ships: [],

generateShipProps: function(){
    var emptyStrings = [];
    for(var i = 0; i < this.shipLength; i++){
        emptyStrings.push("");
    }

    for(var j = 0; j < this.numShips; j++){
        model.ships.push({locations: emptyStrings, hits: emptyStrings});
    }
},

generateShip: function(){
    this.generateShipProps();

    var locations;
    for(var i = 0; i < this.numShips; i++){
        do{
            locations = this.generateShipLocations();
        }while(this.collision(locations));
        this.ships[i].locations = locations;
    }
}

对于工作脚本,装载时看起来像这样的东西

ships: [{locations: ["10", "11", "12"], hits: ["", "", ""]},
    {locations: ["22", "23", ""24], hits: ["", "", ""]},
    {locations: ["51", "52", "53"], hits: ["", "", ""]}],

如果输入的位置是正确的,让我们说10然后hits属性中的数组将输入一个“hit”字符串,如下所示

ships: [{locations: ["10", "11", "12"], hits: ["hit", "", ""]},
    {locations: ["22", "23", ""24], hits: ["", "", ""]},
    {locations: ["51", "52", "53"], hits: ["", "", ""]}],

对于修改后的脚本ships属性仍会产生类似这样的内容

ships: [{locations: ["10", "11", "12"], hits: ["", "", ""]},
    {locations: ["22", "23", ""24], hits: ["", "", ""]},
    {locations: ["51", "52", "53"], hits: ["", "", ""]}],

但是,如果输入11,则会发生这种情况

ships: [{locations: ["10", "11", "12"], hits: ["", "hit", ""]},
    {locations: ["22", "23", ""24], hits: ["", "hit", ""]},
    {locations: ["51", "52", "53"], hits: ["", "hit", ""]}],

不知何故,它正在对所有相同的索引实施点击。

有人可以帮助我找到我错过的原因吗?

1 个答案:

答案 0 :(得分:0)

替换此代码

locations = this.generateShipLocations();

用这个:

locations.push(this.generateShipLocations());

你还需要初始化这样的位置:

var locations = []

你正在覆盖而不是添加