绘制与相邻框颜色不同的框时出现问题

发布于 2024-11-26 16:01:42 字数 869 浏览 0 评论 0原文

我正在编写一个程序,绘制一个盒子网格,并且盒子的颜色必须与盒子相邻的颜色不同。

我的代码将当前框的颜色与左侧和顶部的颜色进行比较。如果其中任何一个匹配,它会选择另一个随机数(作为颜色)。对于第一行/列上的图块,我将网格的数组索引设置为负数 -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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

⊕婉儿 2024-12-03 16:01:42

这行应该是:

while (grid[i-1][j] == grid[i][j] || grid[i][j-1] == grid[i][j])

编辑:

我真的不明白为什么你需要从负索引开始。它只是无缘无故地让人感到困惑。我重写了你的脚本,只改变了一些小事情,你应该能够自己调整这些小事情(随机颜色只是红色的随机变化,而不是实际上随机的)。

var randomNum:Number = 0;
var HEIGHT:Number = 10;
var WIDTH:Number = 10;
var grid:Array;

function fillArray():void {
grid = new Array();
for (var i = 0; i < HEIGHT; i++) {
    grid.push(new Array());
    for (var j = 0; j < WIDTH; j++) {
        var prevRow:Number = (i > 0) ? grid[i -1][j]: 0;
        var prevCol:Number = (j > 0) ? grid[i][j -1]: 0;

        if(prevRow != 0 && prevCol != 0){
            randomNum = Math.floor(Math.random() * 256) + 1;
            while(randomNum == prevRow || randomNum == prevCol){
                randomNum = Math.floor(Math.random() * 256) + 1;
            }

            this.graphics.beginFill(randomNum * 0xFF0000,1);
            this.graphics.drawRect(i*10,j*10,10,10);

        }
    }
}
}

fillArray();

Should this line read:

while (grid[i-1][j] == grid[i][j] || grid[i][j-1] == grid[i][j])

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).

var randomNum:Number = 0;
var HEIGHT:Number = 10;
var WIDTH:Number = 10;
var grid:Array;

function fillArray():void {
grid = new Array();
for (var i = 0; i < HEIGHT; i++) {
    grid.push(new Array());
    for (var j = 0; j < WIDTH; j++) {
        var prevRow:Number = (i > 0) ? grid[i -1][j]: 0;
        var prevCol:Number = (j > 0) ? grid[i][j -1]: 0;

        if(prevRow != 0 && prevCol != 0){
            randomNum = Math.floor(Math.random() * 256) + 1;
            while(randomNum == prevRow || randomNum == prevCol){
                randomNum = Math.floor(Math.random() * 256) + 1;
            }

            this.graphics.beginFill(randomNum * 0xFF0000,1);
            this.graphics.drawRect(i*10,j*10,10,10);

        }
    }
}
}

fillArray();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文