我正在 JavaScript 中构建战舰游戏,我目前正在使用OOP制作一个
的系统a)选择放置船的坐标
b)找到船上的下一个相邻瓷砖
c)用棕色
着色我现在想在步骤c)上工作,即。选择一个坐标,并将其属性hasBoat更改为1,然后将其变为棕色。有一种简单的方法可以做到这一点吗?代码在这里:
var xPos;
var yPos;
var boatPos;
var boatGrid = {
selectPos : function() {
console.log("it works");
for (boatNum = 1; boatNum < 4; boatNum++) {
xPos = Math.floor(Math.random() * 8);
yPos = Math.floor(Math.random() * 10 + 1);
boatPos = "cell_" + xPos + "_" + yPos;
this.hasBoat = 1;
document.getElementById(boatPos);
if (this.hasBoat == 1) {
this.hasBoat.style.backgroundColor = "brown";
}
this.buildBoat();
}
},
buildBoat : function() {
console.log("this works too");
for (boatLen = 1; boatLen < 4; boatLen++) {
xPos = xPos++;
}
},
}
function placeBoat() {
document.getElementById(boatPos) = boatGrid.selectPos();
boatGrid.hasBoat = 1;
}