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

why is my input inserted into two diff arrays with same index? (js)

本文关键字:两个 diff js 数组 索引 插入 输入 我的 为什么      更新时间:2023-09-26

我有一个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", ""]}],

不知怎么的,它实现了对所有相同索引的命中。

有人能帮我一把吗?是我错过了什么导致了这一切?

替换此代码

locations = this.generateShipLocations();

这个:

locations.push(this.generateShipLocations());

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

var locations = []

您正在重写而不是添加