- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
您好,这是我第一次在这个网站上发帖,所以我会尽量说得清楚。
我正在使用 javaScript 创建战舰游戏来生成随机船舶放置。我为游戏板创建了两个数组(X 和 Y),创建了一个类来生成随机放置,并为不同的船只创建了一个对象数组,如下所示......
// game board
var xAxis = ["a","b","c","d","e","f","g","h","i","j"];
var yAxis = [1,2,3,4,5,6,7,8,9,10];
// Location Class decleration //
function Location (size,name){
//// Parameters ///////////////////////////////////////////
this.size = size;
this.name = name;
//// Class functions //////////////////////////////////////
// sets the ship to a random position
this.shipPosition = function(){
var orientation = Math.random();
var area = [];
if (orientation < 0.5){
x = Math.floor(Math.random()*(xAxis.length-size+1));
y = Math.floor(Math.random()*(yAxis.length));
for (i=x; i < (size + x); i++){
area.push([xAxis[i],yAxis[y]]);
}
} else {
x = Math.floor(Math.random()*(xAxis.length));
y = Math.floor(Math.random()*(yAxis.length-size+1));
for (i=y; i < (size + y); i++){
area.push([xAxis[x],yAxis[i]]);
}
}
return area;
};
///// Stored variables /////////////////////////////////////////
//stores position
var positionArray = this.shipPosition();
//assigns value to each element in array for checking
//and makes the private value public. ex. a = ['h',1]
var a = positionArray[0];
var b = positionArray[1];
var c = positionArray[2];
var d = positionArray[3];
var e = positionArray[4];
this.getA = a;
this.getB = b;
this.getC = c;
this.getD = d;
this.getE = e;
// locator console logging function
this.whereIs = function(){
console.log(this.name);
console.log("ship size is "+ size);
console.log("-- X - Y --");
console.log(a);
console.log(b);
if (size > 2){console.log(c);}
if (size > 3){console.log(d);}
if (size > 4){console.log(e);}
console.log(" *********************************** ");
};
}
//ship array
var computerShip = new Array();
computerShip[0] = new Location(2, "SHIP 1");
computerShip[1] = new Location(3, "SHIP 2");
computerShip[2] = new Location(3, "SHIP 3");
computerShip[3] = new Location(4, "SHIP 4");
computerShip[4] = new Location(5, "SHIP 5");
//used to call whereIs method for each ship
var genetateComputerShip = function(){
for(i=0; i<computerShip.length; i++){
computerShip[i].whereIs();
}
};
genetateComputerShip();
如何检查船舶数组的每个值以检查是否重叠?我尝试了多种方法,但在挑选元素或找到一种轻松交叉引用它们的方法时遇到了麻烦。有什么想法吗?
最佳答案
我对您的代码进行了一些更改,以维护全局“板”概念,该概念表示为多维数组。请参阅此处的代码:
http://jsfiddle.net/mchail/ytwyS/1/
点击“运行”后,检查浏览器的 JavaScript 控制台,您应该在完整棋盘的末尾看到每艘船都已放置的打印输出。我修改了 shipPosition
(现在称为 setShipPosition
),以便在放置船舶之前检查板上的冲突。
顺便说一句,干得好!
更新
修改了 jsfiddle 以将板也打印到屏幕上。继续点击“运行”即可查看更多随机生成的棋盘。
关于javascript - 比较多个数组的各个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16094756/
这个问题在这里已经有了答案: Prolog - count repetitions in list (3 个答案) 关闭 7 年前。 所以我正在尝试创建一种方法来确定列表中 N 的数量。我已经试验了
使用 sscanf 或任何其他命令从分号后的文件读取的最佳方法是什么,例如,如果我的文件有 5: 4 5 6 7。如何将冒号后的值存储在数组中。此外,分号后面的整数数量可能会有所不同,即在上面给出的示
我正在尝试返回第 n 个数字。如果数字是 3 或 7 的倍数,则从 1 开始,则跳过该数字并获取下一个数字。但是,如果数字是 3 和 7 的倍数,则不会跳过该数字。 public int Multip
如何有效地从末尾获取一定数量的元素? 1 looks like 2 three!! 例如,如何获取最后 2 个 div 的内容? 最佳答案 $(document).ready(function(){
//Generate Food Personality for(i=0; i
我试图在给定的排序数组中找到最大的 K 个数。 例如:输入 -> [ 5, 12, 45, 32, 9, 20, 15]输出 -> K = 3, [45, 32, 20] 到目前为止我编写的代码返回最
两个数字表 a 和 b 被写入并按升序合并在一起,并删除重复项。现在的问题是在这个 super 表中找到比 O(n) 复杂度更好的 nth 数。 Limits 1 #include using nam
给定一个包含 N 个元素的数组 A,我需要找到对 (i,j) 使得 i 不等于 j 并且如果我们为所有对 (i, j) 然后它来到第k个位置。 示例:让 N=4 和数组 A=[1 2 3 4] 如果
给定一组跳过的数字,我需要找到该组中不存在的第 N 个数字。示例: 给定一组 [1, 4, 5] 一些结果: 对于 N = 1 结果 0 对于 N = 2 结果 2(因为 1 被跳过) 对于 N =
几个月前在亚马逊的招聘挑战中遇到了这个问题。 给定两个数字 a 和 b 及其倍数的升序列表,找出第 n 个倍数。 例如,如果 a = 4 , b = 6 和 n = 6 那么答案是 18因为列表是 4
所以我最近一直在研究 Python,我试图找到一种方法来在单个表达式中输出斐波那契数列的第 n 个数。这是我到目前为止编写的代码: (lambda f: f if f 1 # n == 2 -> 1
作业是编写一个 C++ 程序,它接受输入数字 n 并输出序列中的第 n 个数字: 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 1 2 3 4 5 6 ... 这是我到目前为止想出的:
问题很简单(答案很可能):如何找到数组中最小的 2 个数字? for ( i = 1; i 关于c++ - 数组中最小的 2 个数,我们在Stack Overflow上找到一个类似的问题: ht
您可以调用Nokogiri::XML::Node#ancestors.size 来查看节点的嵌套深度。但是有没有办法确定嵌套最深的子节点的嵌套深度呢? 或者,您如何找到从一个节点下降的所有叶节点? 最
这个任务是找到n个数字的fibanocci。任务: 1.找出n个数的斐波那契数。 2.使用变量n,first=0,second=1,next,c。输入格式:使用 printf 语句。使用 scanf
我想添加每 10 个元素的数量。 例如, function myFunction() { for (var i = 1; i "; } } 输出: 1,2,3,4,5,6,7,8,9,
我想编写一个程序来计算斐波那契数列的第 n 个数,这是我使用 printf 和 scanf 完成的。但我希望更改我的程序,以便在命令行中输入序列号,而不是在程序提示时输入。这就是我想出的。它可以编译,
我有一个方案中的对象列表。每个对象都与一个可以在运行时计算的置信度值相关联。我想找到具有最高置信度值的前 50 个此类对象。示例:((WordPair1) (WordPair2)) 等等都是我的对象。
我正在寻找一种给定目标的算法,返回目标位为 0 的第 N 个数字。 例如,对于n={0,1,2,3}和target=1的输入,输出将是(二进制) 000,001,100,101 最佳答案 只写值N-1
我正在尝试创建一个函数来获取 vector 中的 3 个最大数字。例如:数字:1 6 2 5 3 7 4结果:5 6 7 我想我可以对它们进行 DESC 排序,在开始时获取 3 个数字,然后再对它们进
我是一名优秀的程序员,十分优秀!