绘制与相邻框颜色不同的框时出现问题
我正在编写一个程序,绘制一个盒子网格,并且盒子的颜色必须与盒子相邻的颜色不同。
我的代码将当前框的颜色与左侧和顶部的颜色进行比较。如果其中任何一个匹配,它会选择另一个随机数(作为颜色)。对于第一行/列上的图块,我将网格的数组索引设置为负数 -1,这样它就不会匹配。
我所拥有的:
private function fillArray():void {
grid = new Array();
grid[-1] = new Array(-1,-1,-1,-1,-1); //paddles the grid[-1][0 to 4] with -1
for (var i = 0; i < HEIGHT; i++) {
grid[i] = new Array();
grid[i][-1] = -1; // paddles the -1 row with -1
for (var j = 0; j < WIDTH; j++) {
while (grid[i-1][j] == grid[i][j] || grid[i][-j] == grid[i][j]) { //while the current box is the same as those to the left or top
grid[i][j] = Math.floor(Math.random() * COLORS) + 1; //random number time!
}
}
}
}
这有一个超时错误,并且网格全部为-1。我不知道我在这里做错了什么。感谢您的帮助!
I'm making a program that draws a grid of boxes and the color of the box must be different to those adjacent to the box.
My code compares the current box's color to those to the left and top. If any of them matches, it picks another random number (as the color). For the tiles on the first row/column, I make my grid's array indexes with negative numbers -1 so that it will not match.
What I have:
private function fillArray():void {
grid = new Array();
grid[-1] = new Array(-1,-1,-1,-1,-1); //paddles the grid[-1][0 to 4] with -1
for (var i = 0; i < HEIGHT; i++) {
grid[i] = new Array();
grid[i][-1] = -1; // paddles the -1 row with -1
for (var j = 0; j < WIDTH; j++) {
while (grid[i-1][j] == grid[i][j] || grid[i][-j] == grid[i][j]) { //while the current box is the same as those to the left or top
grid[i][j] = Math.floor(Math.random() * COLORS) + 1; //random number time!
}
}
}
}
This has a timeout error and grid is all -1. I don't know what I am doing wrong here. Thanks for any help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这行应该是:
编辑:
我真的不明白为什么你需要从负索引开始。它只是无缘无故地让人感到困惑。我重写了你的脚本,只改变了一些小事情,你应该能够自己调整这些小事情(随机颜色只是红色的随机变化,而不是实际上随机的)。
Should this line read:
Edit:
I really don't understand why you need to start with negative indexes. It just makes it confusing for no reason. I rewrote your script changing just a couple minor things which you should be able to tweak yourself (the random color is just a random variation of red instead of actually random).