1
我有這個js腳本,完美的作品作爲戰艦,我想改劇本,因爲現在我必須手動添加,即使位置是隨機產生的船舶數量。 在這一刻,有我在船上這種方式來補充。爲什麼我輸入插入到相同的索引大二差異陣列? (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,然後在命中屬性的陣列將輸入的字符串「打」像下面
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", ""]}],
以某種方式實現了對所有相同索引的匹配。
能有人給我一隻手我已經錯過了這是造成這個?
this w應該只是崩潰瀏覽器:( – Dora