随机生成麻烦,还是脚本麻烦?

发布于 2024-12-05 14:32:41 字数 6193 浏览 5 评论 0原文

理想情况下,它必须在棋盘格的随机位置上给我 5 个黑色方块。但事实上它给了我更少的黑色方块。

我真的很坚持这个,因为我真的找不到答案。

PS这里是编译后的应用程序。 http://dl.dropbox.com/u/9288177/CSE/images /征服者.swf

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="800" minHeight="600" creationComplete="main()">
    <s:layout>
        <s:BasicLayout/>
    </s:layout>
    <fx:Script>
        <![CDATA[
            import flash.display.Shape;
            import flash.display.Sprite;

            import mx.core.UIComponent;

            import spark.core.SpriteVisualElement;
            private var rectangle:Sprite = new Sprite();
            private var insterter:UIComponent = new UIComponent();
            private var theme:SpriteVisualElement = new SpriteVisualElement();
            private var yH:int;
            private var xH:int;
            private var grid:Array = new Array();
            const WIDTH:int = 20;
            const HEIGHT:int = 20;
            private function main():void
            {
                var uic:UIComponent = new UIComponent();
                uic.width = uic.height = 50;
                uic.graphics.lineStyle(1,0x888888);
                //uic.graphics.
                //uic.graphics.drawRect(0,0,50,50);
                canvas.rawChildren.addChild(uic);

                const boxW:int = 20;
                const boxH:int = 20;
                const planetnumber:int = 5;


                for(yH = 0; yH < HEIGHT; yH++) {
                    var row:Array = new Array();
                    for (xH = 0; xH < WIDTH; xH++) {

                        uic.graphics.drawRect(boxW*yH,boxH*xH,boxW,boxH);
                        row.push(0);
                    }
                    grid.push(row);
                }
                var currentplanet:int = 0;
                var rw:int;
                var clmn:int;
                var counter:int = 0;
                while (currentplanet < planetnumber)
                {
                    clmn= (Math.random() * (WIDTH-5)) + 5;
                     rw = (Math.random() * (HEIGHT-5)) + 5;
                    if (fieldcheck(rw, clmn) == true)
                    {
                        grid[rw][clmn] = 1;
                        uic.graphics.beginFill(0x000000, 1);
                        uic.graphics.drawRect(boxW*clmn,boxH*rw,boxW,boxH);
                        uic.graphics.endFill();
                        currentplanet++;
                        if (counter == 4)
                        {
                            c4.text=clmn.toString();
                            r4.text=rw.toString();
                        }
                        if (counter == 3)
                        {
                            c3.text=clmn.toString();
                            r3.text=rw.toString();
                            counter++;
                        }
                        if (counter == 2)
                        {
                            c2.text=clmn.toString();
                            r2.text=rw.toString();
                            counter++;
                        }
                        if (counter == 1)
                        {
                            c1.text=clmn.toString();
                            r1.text=rw.toString();
                            counter++;
                        }
                        if (counter == 0)
                        {
                            c0.text=clmn.toString();
                            r0.text=rw.toString();
                            counter++;
                        }




                    }


                }

            }
            private function fieldcheck(iRow:int, iColumn:int):Boolean
            {
                if (grid[iRow][iColumn]==0)
                {
                    if ((iRow > 0)&&(iRow < HEIGHT))
                    {
                        if ((iColumn > 0) && (iColumn < WIDTH))
                            {
                                if ((grid[iRow-1][iColumn-1]==0)&&(grid[iRow][iColumn-1]==0)&&(grid[iRow+1][iColumn-1]==0)&&(grid[iRow-1][iColumn]==0)&&(grid[iRow+1][iColumn]==0)&&(grid[iRow-1][iColumn+1]==0)&&(grid[iRow][iColumn+1]==0)&&(grid[iRow+1][iColumn+1]==0))
                                    {
                                        return true;
                                    }
                            }
                    }
                }
            return false;
            }

            //var rnd:int = (Math.random()* 41) + 10;
            /*if (rnd >= 48) {
            uic.graphics.beginFill(0x000000, 1);
            uic.graphics.drawRect(boxW*y,boxH*x,boxW,boxH);
            uic.graphics.endFill();                         
            }
            else
            {
            uic.graphics.drawRect(boxW*y,boxH*x,boxW,boxH);
            }*/
        ]]>
    </fx:Script>
    <mx:Canvas id="canvas" x="34" y="10" width="90%" height="90%" textAlign="center">
    </mx:Canvas>
    <s:Label id="r1" x="34" y="565" width="84" height="11" text="Label"/>
    <s:Label id="r2" x="34" y="575" width="84" height="11" text="Label"/>
    <s:Label id="c2" x="126" y="575" width="77" height="11" text="Label"/>
    <s:Label id="c1" x="126" y="565" width="77" height="11" text="Label"/>
    <s:Label id="r3" x="34" y="585" width="84" height="11" text="Label"/>
    <s:Label id="c3" x="126" y="585" width="77" height="11" text="Label"/>
    <s:Label id="r4" x="215" y="555" width="84" height="11" text="Label"/>
    <s:Label id="c4" x="307" y="555" width="77" height="11" text="Label"/>
    <s:Label id="r0" x="34" y="555" width="84" height="11" text="Label"/>
    <s:Label id="c0" x="126" y="555" width="77" height="11" text="Label"/>
</s:Application>

In ideal it must give me 5 black squares on random positions on checker field. But in fact it giving me less up to none black squares.

I really stuck into this because i literally can't find answer.

PS here is compiled app.
http://dl.dropbox.com/u/9288177/CSE/images/Conquester.swf

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="800" minHeight="600" creationComplete="main()">
    <s:layout>
        <s:BasicLayout/>
    </s:layout>
    <fx:Script>
        <![CDATA[
            import flash.display.Shape;
            import flash.display.Sprite;

            import mx.core.UIComponent;

            import spark.core.SpriteVisualElement;
            private var rectangle:Sprite = new Sprite();
            private var insterter:UIComponent = new UIComponent();
            private var theme:SpriteVisualElement = new SpriteVisualElement();
            private var yH:int;
            private var xH:int;
            private var grid:Array = new Array();
            const WIDTH:int = 20;
            const HEIGHT:int = 20;
            private function main():void
            {
                var uic:UIComponent = new UIComponent();
                uic.width = uic.height = 50;
                uic.graphics.lineStyle(1,0x888888);
                //uic.graphics.
                //uic.graphics.drawRect(0,0,50,50);
                canvas.rawChildren.addChild(uic);

                const boxW:int = 20;
                const boxH:int = 20;
                const planetnumber:int = 5;


                for(yH = 0; yH < HEIGHT; yH++) {
                    var row:Array = new Array();
                    for (xH = 0; xH < WIDTH; xH++) {

                        uic.graphics.drawRect(boxW*yH,boxH*xH,boxW,boxH);
                        row.push(0);
                    }
                    grid.push(row);
                }
                var currentplanet:int = 0;
                var rw:int;
                var clmn:int;
                var counter:int = 0;
                while (currentplanet < planetnumber)
                {
                    clmn= (Math.random() * (WIDTH-5)) + 5;
                     rw = (Math.random() * (HEIGHT-5)) + 5;
                    if (fieldcheck(rw, clmn) == true)
                    {
                        grid[rw][clmn] = 1;
                        uic.graphics.beginFill(0x000000, 1);
                        uic.graphics.drawRect(boxW*clmn,boxH*rw,boxW,boxH);
                        uic.graphics.endFill();
                        currentplanet++;
                        if (counter == 4)
                        {
                            c4.text=clmn.toString();
                            r4.text=rw.toString();
                        }
                        if (counter == 3)
                        {
                            c3.text=clmn.toString();
                            r3.text=rw.toString();
                            counter++;
                        }
                        if (counter == 2)
                        {
                            c2.text=clmn.toString();
                            r2.text=rw.toString();
                            counter++;
                        }
                        if (counter == 1)
                        {
                            c1.text=clmn.toString();
                            r1.text=rw.toString();
                            counter++;
                        }
                        if (counter == 0)
                        {
                            c0.text=clmn.toString();
                            r0.text=rw.toString();
                            counter++;
                        }




                    }


                }

            }
            private function fieldcheck(iRow:int, iColumn:int):Boolean
            {
                if (grid[iRow][iColumn]==0)
                {
                    if ((iRow > 0)&&(iRow < HEIGHT))
                    {
                        if ((iColumn > 0) && (iColumn < WIDTH))
                            {
                                if ((grid[iRow-1][iColumn-1]==0)&&(grid[iRow][iColumn-1]==0)&&(grid[iRow+1][iColumn-1]==0)&&(grid[iRow-1][iColumn]==0)&&(grid[iRow+1][iColumn]==0)&&(grid[iRow-1][iColumn+1]==0)&&(grid[iRow][iColumn+1]==0)&&(grid[iRow+1][iColumn+1]==0))
                                    {
                                        return true;
                                    }
                            }
                    }
                }
            return false;
            }

            //var rnd:int = (Math.random()* 41) + 10;
            /*if (rnd >= 48) {
            uic.graphics.beginFill(0x000000, 1);
            uic.graphics.drawRect(boxW*y,boxH*x,boxW,boxH);
            uic.graphics.endFill();                         
            }
            else
            {
            uic.graphics.drawRect(boxW*y,boxH*x,boxW,boxH);
            }*/
        ]]>
    </fx:Script>
    <mx:Canvas id="canvas" x="34" y="10" width="90%" height="90%" textAlign="center">
    </mx:Canvas>
    <s:Label id="r1" x="34" y="565" width="84" height="11" text="Label"/>
    <s:Label id="r2" x="34" y="575" width="84" height="11" text="Label"/>
    <s:Label id="c2" x="126" y="575" width="77" height="11" text="Label"/>
    <s:Label id="c1" x="126" y="565" width="77" height="11" text="Label"/>
    <s:Label id="r3" x="34" y="585" width="84" height="11" text="Label"/>
    <s:Label id="c3" x="126" y="585" width="77" height="11" text="Label"/>
    <s:Label id="r4" x="215" y="555" width="84" height="11" text="Label"/>
    <s:Label id="c4" x="307" y="555" width="77" height="11" text="Label"/>
    <s:Label id="r0" x="34" y="555" width="84" height="11" text="Label"/>
    <s:Label id="c0" x="126" y="555" width="77" height="11" text="Label"/>
</s:Application>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

情泪▽动烟 2024-12-12 14:32:41

您的小应用程序的问题在于 fieldcheck 功能。正如 The_asMan 所说,您遇到的问题是在检查其运行时发生错误。问题是,当您进行一些边界检查时:

if ((iRow > 0)&&(iRow < HEIGHT))
if ((iColumn > 0) && (iColumn < WIDTH))

这并不包括您对 iRow 和 iColumn 值进行一些操作以查看相邻方块中是否有项目的情况。我将 fieldcheck 操作更改为以下内容,它似乎可以解决问题:

private function fieldcheck(iRow:int, iColumn:int):Boolean
{
    // I used looping here as it made the range checks simpler, plus you can
    // extend the range of a boxes space out pretty easily by changing the
    // - 1 and + 1 to a different number
    for(var r:int = iRow - 1; r == iRow + 1; r++) {
        if(r >= 0 && r < HEIGHT) {
            for(var c:int = iColumn - 1; c == iColumn + 1; c++) {
                if(c >= 0 && c < WIDTH) {
                    if(grid[r][c]==1) {
                        return false;
                    }
                }
            }
        }
    }
    return true;
}

此外,请考虑将以下行更改为:

clmn= (Math.random() * (WIDTH-5)) + 5;
rw = (Math.random() * (HEIGHT-5)) + 5;

To :

clmn= (Math.random() * (WIDTH-5)) + 4;
rw = (Math.random() * (HEIGHT-5)) + 4;

这应该为您提供更好的数字结果,因为数组是 0 索引的,并且您生成的数字是从 5-20 而不是 4-19。 (我假设 0-3 的缓冲点是故意的)。

The problem with your little app lies in the fieldcheck function. The problem you are having is, as The_asMan says, an error occurring in the check that it is running. The problem is that while you are doing some bounds checking:

if ((iRow > 0)&&(iRow < HEIGHT))
if ((iColumn > 0) && (iColumn < WIDTH))

This doesn't cover when you are doing some operations on the iRow and iColumn values to see if neighboring squares have items in them. I changed the fieldcheck operation to the following and it seems to do the trick:

private function fieldcheck(iRow:int, iColumn:int):Boolean
{
    // I used looping here as it made the range checks simpler, plus you can
    // extend the range of a boxes space out pretty easily by changing the
    // - 1 and + 1 to a different number
    for(var r:int = iRow - 1; r == iRow + 1; r++) {
        if(r >= 0 && r < HEIGHT) {
            for(var c:int = iColumn - 1; c == iColumn + 1; c++) {
                if(c >= 0 && c < WIDTH) {
                    if(grid[r][c]==1) {
                        return false;
                    }
                }
            }
        }
    }
    return true;
}

Additionally, consider changing the following lines from:

clmn= (Math.random() * (WIDTH-5)) + 5;
rw = (Math.random() * (HEIGHT-5)) + 5;

To :

clmn= (Math.random() * (WIDTH-5)) + 4;
rw = (Math.random() * (HEIGHT-5)) + 4;

This should give you better results with the numbers as the arrays are 0-indexed and the numbers you were generating went from 5-20 instead of 4-19. (I'm assuming that the buffer spots of 0-3 are intentional).

寂寞美少年 2024-12-12 14:32:41

这不是一个答案,而是一条评论,该评论太大而无法作为评论发布。
它工作正常,刷新几次后我看到 5 个黑色方块,我收到以下错误代码。
当这段代码弹出时,我没有得到任何方块。

TypeError: Error #1010: A term is undefined and has no properties.
    at Conquester/fieldcheck()[C:\Users\Smoke_Fumus\Adobe Flash Builder 4.5\Conquester\src\Conquester.mxml:109]
    at Conquester/main()[C:\Users\Smoke_Fumus\Adobe Flash Builder 4.5\Conquester\src\Conquester.mxml:55]
    at Conquester/___Conquester_Application1_creationComplete()[C:\Users\Smoke_Fumus\Adobe Flash Builder 4.5\Conquester\src\Conquester.mxml:4]
    at flash.events::EventDispatcher/dispatchEventFunction()
    at flash.events::EventDispatcher/dispatchEvent()
    at mx.core::UIComponent/dispatchEvent()[E:\dev\4.5.1\frameworks\projects\framework\src\mx\core\UIComponent.as:13128]
    at mx.core::UIComponent/set initialized()[E:\dev\4.5.1\frameworks\projects\framework\src\mx\core\UIComponent.as:1818]
    at mx.managers::LayoutManager/doPhasedInstantiation()[E:\dev\4.5.1\frameworks\projects\framework\src\mx\managers\LayoutManager.as:842]
    at mx.managers::LayoutManager/doPhasedInstantiationCallback()[E:\dev\4.5.1\frameworks\projects\framework\src\mx\managers\LayoutManager.as:1180]

This isn't an answer but a comment that is to big to post as a comment.
It was working fine and I saw 5 black squares after a few refreshes I got the following error code.
When this code popped up I got no squares.

TypeError: Error #1010: A term is undefined and has no properties.
    at Conquester/fieldcheck()[C:\Users\Smoke_Fumus\Adobe Flash Builder 4.5\Conquester\src\Conquester.mxml:109]
    at Conquester/main()[C:\Users\Smoke_Fumus\Adobe Flash Builder 4.5\Conquester\src\Conquester.mxml:55]
    at Conquester/___Conquester_Application1_creationComplete()[C:\Users\Smoke_Fumus\Adobe Flash Builder 4.5\Conquester\src\Conquester.mxml:4]
    at flash.events::EventDispatcher/dispatchEventFunction()
    at flash.events::EventDispatcher/dispatchEvent()
    at mx.core::UIComponent/dispatchEvent()[E:\dev\4.5.1\frameworks\projects\framework\src\mx\core\UIComponent.as:13128]
    at mx.core::UIComponent/set initialized()[E:\dev\4.5.1\frameworks\projects\framework\src\mx\core\UIComponent.as:1818]
    at mx.managers::LayoutManager/doPhasedInstantiation()[E:\dev\4.5.1\frameworks\projects\framework\src\mx\managers\LayoutManager.as:842]
    at mx.managers::LayoutManager/doPhasedInstantiationCallback()[E:\dev\4.5.1\frameworks\projects\framework\src\mx\managers\LayoutManager.as:1180]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文