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

Posted

技术标签:

【中文标题】为啥我的输入被插入到两个具有相同索引的 diff 数组中? (js)【英文标题】:why is my input inserted into two diff arrays with same index? (js)为什么我的输入被插入到两个具有相同索引的 diff 数组中? (js) 【发布时间】:2016-04-15 05:28:17 【问题描述】:

我有这个 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】:

替换此代码

locations = this.generateShipLocations();

用这个:

locations.push(this.generateShipLocations());

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

var locations = []

您正在覆盖而不是添加

【讨论】:

这只会让浏览器崩溃:(

以上是关于为啥我的输入被插入到两个具有相同索引的 diff 数组中? (js)的主要内容,如果未能解决你的问题,请参考以下文章

具有许多索引的表的慢速批量插入

HashMap为啥不安全?

在 Unix/Linux 中判断两个文件是不是具有相同内容的最快方法?

使用 codeigniter 将具有相同输入名称的表单输入插入数据库行

具有相同索引的数组已更改而未重新分配

vue中使用v-for时为啥不能用index作为key?