我在卡片中做了一个数字算法,所以目的是:
.|15|23|.|44|.|63|.|86
.|17|25|.|47|.|66|.|88
.|19|28|.|49|.|69|.|89
点(。)是一个空单元格,每行必须有4个点(空单元格)和5个数字,但每列中的垂直方向如下:
第一列 - 从1到9,第二列从10到20,第三列从30到40,第四列从40到50继续,所以我尝试过:
for(var y:int = 0; y < TICKET_MAX_NUM; y++)
{
var l:Label = new Label();
l.name = 'num_' + y;
l.width = 25;
l.height = 21;
var rand:*;
if(y == 0)
{
rand = UtilFunctions.randomRange(9 * multiplier, 1);
}
else
{
do
{
rand = UtilFunctions.randomRange(multiplier * 10, multiplier * 10 - 10);
}
while(existNumber(rand, randomNumbers));
}
randomNumbers.push(rand);
l.styleName = 'ticket_number';
l.text = rand.toString();
t.addChild(l);
if(multiplier == 9)
{
multiplier = 1;
while(emptyLeft > 0)
{
rand = UtilFunctions.randomRange(y, min );
//randomNumbers.splice(rand, 1);
var num = t.getChildByName('num_' + rand);
num.text = '{' + rand.toString() + '}';
emptyLeft--;
}
min += 9;
emptyLeft = 4;
}
else
{
multiplier++;
}
}
但我无法生成4个空单元格和5个数字。有些事情是错的,但我无法理解。
答案 0 :(得分:2)
如果我理解正确,你就会产生一个独特的随机数矩阵。
变量:
var data:Array = []; // multidimensional matrix array collection
var x:uint; // columns
var y:uint; // rows
var n:uint; // number
生成集合的代码,然后仅随机选择一个数字:
for (x = 0; x < 10; x++)
{
data.push([]);
for (n = 0; n < 10; n++)
{
data[x].push((10 * x) + n);
}
}
for (y = 0; y < 10; y++)
{
var row:String = "";
for (x = 0; x < 10; x++)
{
n = getRandomNumber(data[x]);
row += n.toString() + "|";
}
trace(row);
}
实用程序功能,用于从数组中随机选择一个数字,使用后删除该数字:
public function getRandomNumber(set:Array):uint
{
return (set.splice(int(Math.random() *
set.length), 1)[0]);
}
以下是运行时的示例:
// Runtime #1 // Runtime #2
1|15|26|31|41|54|61|76|83|97 7|13|28|35|43|58|69|75|83|91
5|13|28|33|42|50|64|72|89|96 4|12|21|32|41|52|64|71|89|93
8|16|23|35|44|58|67|79|84|98 9|19|23|34|40|54|60|76|80|95
4|14|22|34|45|55|62|77|81|95 0|18|29|33|47|59|67|73|84|90
6|18|25|38|46|53|65|70|86|93 6|11|27|37|46|55|65|72|82|99
3|10|27|30|48|51|66|75|88|90 5|16|20|30|44|56|63|74|86|97
2|11|24|39|40|52|63|74|80|91 3|10|24|38|45|57|61|78|87|92
7|19|21|36|43|56|60|78|87|99 2|14|26|31|48|51|68|70|88|94
0|12|29|32|49|59|68|73|82|92 1|15|22|39|49|50|62|79|85|96
9|17|20|37|47|57|69|71|85|94 8|17|25|36|42|53|66|77|81|98
// Runtime #3 // Runtime #4
5|18|24|38|40|56|65|75|89|96 2|11|24|36|43|59|65|71|89|99
3|13|20|33|43|58|68|72|87|93 6|14|26|38|48|52|69|78|83|92
9|11|25|35|47|55|61|78|84|98 3|10|23|39|47|50|64|75|87|98
6|10|22|31|49|54|66|71|86|94 5|18|25|35|42|58|61|79|85|93
8|16|23|37|44|51|62|77|88|97 9|13|27|37|41|51|60|74|88|96
1|12|29|34|48|59|60|76|81|92 8|19|28|30|44|57|62|77|81|94
7|17|26|30|46|52|67|79|83|99 0|12|22|33|40|53|63|73|84|91
4|15|27|32|42|57|64|70|85|95 4|15|29|34|46|56|67|70|86|97
0|19|21|39|45|50|63|74|80|91 7|16|20|32|49|54|66|76|82|90
2|14|28|36|41|53|69|73|82|90 1|17|21|31|45|55|68|72|80|95
如果要跳过特定列,请添加条件测试:
if (x == 0 || x == 3 || x == 5 || x == 7)
{
row += ".|";
continue;
}
实施此测试,您可以跳过某些列,生成:
.|14|28|.|40|.|64|.|85|99
.|12|25|.|41|.|62|.|80|95
.|11|27|.|44|.|61|.|87|91
.|16|23|.|43|.|69|.|81|94
.|17|26|.|45|.|60|.|86|92
.|13|20|.|42|.|65|.|89|90
.|15|24|.|49|.|67|.|88|96
.|18|29|.|47|.|63|.|84|97
.|10|22|.|48|.|66|.|83|93
.|19|21|.|46|.|68|.|82|98
以下是Flex实现的示例,生成结果集的DataGrid
:
MXML代码:
<?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"
creationComplete="creationCompleteHandler(event)">
<fx:Script>
<![CDATA[
import mx.collections.ArrayList;
import mx.events.FlexEvent;
[Bindable]
public var data:Array = [];
[Bindable]
public var dataProvider:ArrayList = new ArrayList();
protected function creationCompleteHandler(event:FlexEvent):void
{
generateData();
}
protected function clickHandler(event:MouseEvent):void
{
generateData();
}
protected function generateData():void
{
dataProvider = new ArrayList();
var x:uint; // columns
var y:uint; // rows
var n:uint; // number
for (x = 0; x < 10; x++)
{
data.push([]);
for (n = 0; n < 10; n++)
{
data[x].push((10 * x) + n);
}
}
for (y = 0; y < 10; y++)
{
var row:Object = {};
for (x = 0; x < 10; x++)
{
n = getRandomNumber(data[x]);
row[x] = n;
}
dataProvider.addItem(row);
}
}
public function getRandomNumber(set:Array):uint
{
return (set.splice(int(Math.random() *
set.length), 1)[0]);
}
]]>
</fx:Script>
<s:layout>
<s:VerticalLayout />
</s:layout>
<s:Button label="Generate Data"
click="clickHandler(event)" />
<s:DataGrid dataProvider="{dataProvider}" />
</s:Application>