0

我完全不知所措。

我正在尝试生成随机的 Bingo 票,但我现在被卡住了。关键是,具有以下代码:

var startNum:Number = 1;
var endNum:Number = 90;
var rp:RandomPlus = new RandomPlus(endNum, startNum);

var variant_1:Array = new Array
                    (rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), 
                     "", "",  "", "",  "", "", "", "", "", "", "", "");

var variant_2:Array = new Array
                    (rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), 
                     "", "",  "", "",  "", "", "", "", "", "", "", "");

var variant_3:Array = new Array
                    (rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), 
                     "", "",  "", "",  "", "", "", "", "", "", "", "");

var variant_4:Array = new Array
                    (rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), 
                     "", "",  "", "",  "", "", "", "", "", "", "", "");

var variant_5:Array = new Array
                    (rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), 
                     "", "",  "", "",  "", "", "", "", "", "", "", "");

var variant_6:Array = new Array
                    (rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), rp.getNum(), 
                     "", "",  "", "",  "", "", "", "", "", "", "", "");



function shuffle(a, b):Number {
    var num:Number = Math.round(Math.random()*2)-2;
    return num;
}


var b:Array = variant_1.sort(shuffle);
var cnt:uint=0;
for (var i = 0; i<b.length/9; i++) {
    for (var j = 0; j<b.length/3; j++) {
        var grid1:box=new box();
        grid1.x=5+(grid1.width-1)*j;
        grid1.y=5+(grid1.height-1)*i;
        grid1.txt.text=b[cnt];
        addChild(grid1);
        cnt++;
    }
}

var c:Array = variant_2.sort(shuffle);
var cnt2:uint=0;
for (var k = 0; k<c.length/3; k++) {
    for (var l = 0; l<c.length/9; l++) {
        var grid2:box=new box();
        grid2.x=5+(grid2.width-1)*k;
        grid2.y=100+(grid2.height-1)*l;
        grid2.txt.text=c[cnt2];
        addChild(grid2);
        cnt2++;
    }
}

var d:Array = variant_3.sort(shuffle);
var cnt3:uint=0;
for (var m = 0; m<d.length/3; m++) {
    for (var n = 0; n<d.length/9; n++) {
        var grid3:box=new box();
        grid3.x=5+(grid3.width-1)*m;
        grid3.y=200+(grid3.height-1)*n;
        grid3.txt.text=d[cnt3];
        addChild(grid3);
        cnt3++;
    }
}

var e:Array = variant_4.sort(shuffle);
var cnt4:uint=0;
for (var o = 0; o<e.length/3; o++) {
    for (var p = 0; p<e.length/9; p++) {
        var grid4:box=new box();
        grid4.x=5+(grid4.width-1)*o;
        grid4.y=300+(grid4.height-1)*p;
        grid4.txt.text=e[cnt4];
        addChild(grid4);
        cnt4++;
    }
}

var f:Array = variant_5.sort(shuffle);
var cnt5:uint=0;
for (var q = 0; q<f.length/3; q++) {
    for (var r = 0; r<f.length/9; r++) {
        var grid5:box=new box();
        grid5.x=5+(grid5.width-1)*q;
        grid5.y=400+(grid5.height-1)*r;
        grid5.txt.text=f[cnt5];
        addChild(grid5);
        cnt5++;
    }
}

var g:Array = variant_6.sort(shuffle);
var cnt6:uint=0;
for (var s = 0; s<g.length/3; s++) {
    for (var t = 0; t<g.length/9; t++) {
        var grid6:box=new box();
        grid6.x=5+(grid6.width-1)*s;
        grid6.y=500+(grid6.height-1)*t;
        grid6.txt.text=g[cnt6];
        addChild(grid6);
        cnt6++;
    }
}

哪个输出:

答对了 如您所见,我将 162 个单元格划分为 9x3 表。每个表包含 15 个数字和 12 个空格。所有数字都在 1-90 之间,不重复。数字的位置是随机的。

真正的问题是我必须遵守一些规则:

  • 每个表中的数字之和相等。
  • 有9列,必须这样排列:第一列1-9,第二列10-19,第三列20-29,...,第九列80-90。
  • 每行有 4 个空格。

现在怎么办?这似乎是不可能的。

4

1 回答 1

1

你真的应该改进你的代码。您不需要每个变体的变量,您可以使用二维数组来做到这一点。像这样的东西:

var tables:Array = new Array();

for(var i:uint = 0; i <= 6; 6++){
    tables[i] = fillTable();
    drawTables(tables);
}

...

function fillTable():Array{
    var emptySpaces:Array = new Array();
    var tableData:Array   = new Array();
    emptySpaces = getRandomElements(); //this function should return an array which will represent all 12 empty spaces, otherwise, 12 numbers between 0 and 26

   for (var i = 0; i <= 27; i++) {
       if(emptySpaces.indexOf(i) < 0){ // if my current i isn't a empty space I give it a value
           tableData[i] = generateRandomBasedOnIndex(i);
       }
   }
}

function generateRandomBasedOnIndex(i):uint{
    var maxInt = (i+1) * 10 - 1;
    var minInt = Math.abs(maxInt + 1 - 9);

    return new RandomPlus(maxInt, minInt).getNum();
}

function getRandomElements():Array{
    // TODO - homework :)
}

function drawTables(tableData:Array):void{
    // just use tableData array to place yours display objects on Stage
}

为什么你需要所有数字的总和在表格上相等?这是规矩?我认为这不会发生在现实生活中的宾果游戏中。

Ps.:我这里没有任何 ActionScript 编辑器,所以你可能会发现一些语法错误,但是仔细看看这些行,我认为逻辑应该可以正常工作。

于 2013-04-18T19:28:10.363 回答