随机生成麻烦,还是脚本麻烦?
理想情况下,它必须在棋盘格的随机位置上给我 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的小应用程序的问题在于 fieldcheck 功能。正如 The_asMan 所说,您遇到的问题是在检查其运行时发生错误。问题是,当您进行一些边界检查时:
这并不包括您对 iRow 和 iColumn 值进行一些操作以查看相邻方块中是否有项目的情况。我将 fieldcheck 操作更改为以下内容,它似乎可以解决问题:
此外,请考虑将以下行更改为:
To :
这应该为您提供更好的数字结果,因为数组是 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:
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:
Additionally, consider changing the following lines from:
To :
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).
这不是一个答案,而是一条评论,该评论太大而无法作为评论发布。
它工作正常,刷新几次后我看到 5 个黑色方块,我收到以下错误代码。
当这段代码弹出时,我没有得到任何方块。
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.