AS3 Bingo ticket generator


I'm trying to make a Bingo ticket (Housie) generator, but I've got some problems.

Normally, each row contains five numbers and four blank spaces randomly distributed along the row. Numbers are apportioned by column (1–9, 10–19, 20–29, 30–39, 40–49, 50–59, 60–69, 70–79, and 80–90). My generator looks like this:

My six Bingo tickets

As you can see, I can't manage to have 5 numbers per row. My code is the following (I'm using the RandomPlus class):

package com.demstra.Ticket {

    import flash.display.MovieClip;

    public class Ticket extends MovieClip {

        private var startNumColumn_1:Number = 1;
        private var endNumColumn_1:Number = 9;

        private var startNumColumn_2:Number = 10;
        private var endNumColumn_2:Number = 19;

        private var startNumColumn_3:Number = 20;
        private var endNumColumn_3:Number = 29;

        private var startNumColumn_4:Number = 30;
        private var endNumColumn_4:Number = 39;

        private var startNumColumn_5:Number = 40;
        private var endNumColumn_5:Number = 49;

        private var startNumColumn_6:Number = 50;
        private var endNumColumn_6:Number = 59;

        private var startNumColumn_7:Number = 60;
        private var endNumColumn_7:Number = 69;

        private var startNumColumn_8:Number = 70;
        private var endNumColumn_8:Number = 79;

        private var startNumColumn_9:Number = 80;
        private var endNumColumn_9:Number = 90;

        private var rp1:RandomPlus = new RandomPlus(startNumColumn_1, endNumColumn_1);
        private var rp2:RandomPlus = new RandomPlus(startNumColumn_2, endNumColumn_2);
        private var rp3:RandomPlus = new RandomPlus(startNumColumn_3, endNumColumn_3);
        private var rp4:RandomPlus = new RandomPlus(startNumColumn_4, endNumColumn_4);
        private var rp5:RandomPlus = new RandomPlus(startNumColumn_5, endNumColumn_5);
        private var rp6:RandomPlus = new RandomPlus(startNumColumn_6, endNumColumn_6);
        private var rp7:RandomPlus = new RandomPlus(startNumColumn_7, endNumColumn_7);
        private var rp8:RandomPlus = new RandomPlus(startNumColumn_8, endNumColumn_8);
        private var rp9:RandomPlus = new RandomPlus(startNumColumn_9, endNumColumn_9);

        private var column_1:Array = new Array
                    (rp1.getNum(), rp1.getNum(), rp1.getNum(), rp1.getNum(), rp1.getNum(), rp1.getNum(), rp1.getNum(), rp1.getNum(), rp1.getNum(),
                     "", "", "", "", "", "", "", "", "");

        private var column_2:Array = new Array
                            (rp2.getNum(), rp2.getNum(), rp2.getNum(), rp2.getNum(), rp2.getNum(), rp2.getNum(), rp2.getNum(), rp2.getNum(), rp2.getNum(), rp2.getNum(),
                             "", "", "", "", "", "", "", "");

        private var column_3:Array = new Array
                            (rp3.getNum(), rp3.getNum(), rp3.getNum(), rp3.getNum(), rp3.getNum(), rp3.getNum(), rp3.getNum(), rp3.getNum(), rp3.getNum(), rp3.getNum(),
                             "", "", "", "", "", "", "", "");

        private var column_4:Array = new Array
                            (rp4.getNum(), rp4.getNum(), rp4.getNum(), rp4.getNum(), rp4.getNum(), rp4.getNum(), rp4.getNum(), rp4.getNum(), rp4.getNum(), rp4.getNum(),
                             "", "", "", "", "", "", "", "");

        private var column_5:Array = new Array
                            (rp5.getNum(), rp5.getNum(), rp5.getNum(), rp5.getNum(), rp5.getNum(), rp5.getNum(), rp5.getNum(), rp5.getNum(), rp5.getNum(), rp5.getNum(),
                             "", "", "", "", "", "", "", "");

        private var column_6:Array = new Array
                            (rp6.getNum(), rp6.getNum(), rp6.getNum(), rp6.getNum(), rp6.getNum(), rp6.getNum(), rp6.getNum(), rp6.getNum(), rp6.getNum(), rp6.getNum(),
                             "", "", "", "", "", "", "", "");

        private var column_7:Array = new Array
                            (rp7.getNum(), rp7.getNum(), rp7.getNum(), rp7.getNum(), rp7.getNum(), rp7.getNum(), rp7.getNum(), rp7.getNum(), rp7.getNum(), rp7.getNum(),
                             "", "", "", "", "", "", "", "");

        private var column_8:Array = new Array
                            (rp8.getNum(), rp8.getNum(), rp8.getNum(), rp8.getNum(), rp8.getNum(), rp8.getNum(), rp8.getNum(), rp8.getNum(), rp8.getNum(), rp8.getNum(),
                             "", "", "", "", "", "", "", "");

        private var column_9:Array = new Array
                            (rp9.getNum(), rp9.getNum(), rp9.getNum(), rp9.getNum(), rp9.getNum(), rp9.getNum(), rp9.getNum(), rp9.getNum(), rp9.getNum(), rp9.getNum(), rp9.getNum(),
                             "", "", "", "", "", "", "");

        private var c1:Array = column_1.sort(shuffle);
        private var c2:Array = column_2.sort(shuffle);
        private var c3:Array = column_3.sort(shuffle);
        private var c4:Array = column_4.sort(shuffle);
        private var c5:Array = column_5.sort(shuffle);
        private var c6:Array = column_6.sort(shuffle);
        private var c7:Array = column_7.sort(shuffle);
        private var c8:Array = column_8.sort(shuffle);
        private var c9:Array = column_9.sort(shuffle);

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

        private function populateBoxes():void {
            variant1_1_1.txt.text = c1[0];
            variant1_2_1.txt.text = c1[1];
            variant1_3_1.txt.text = c1[2];
            variant2_1_1.txt.text = c1[3];
            variant2_2_1.txt.text = c1[4];
            variant2_3_1.txt.text = c1[5];
            variant3_1_1.txt.text = c1[6];
            variant3_2_1.txt.text = c1[7];
            variant3_3_1.txt.text = c1[8];
            variant4_1_1.txt.text = c1[9];
            variant4_2_1.txt.text = c1[10];
            variant4_3_1.txt.text = c1[11];
            variant5_1_1.txt.text = c1[12];
            variant5_2_1.txt.text = c1[13];
            variant5_3_1.txt.text = c1[14];
            variant6_1_1.txt.text = c1[15];
            variant6_2_1.txt.text = c1[16];
            variant6_3_1.txt.text = c1[17];

            //-----------------------------

            variant1_1_2.txt.text = c2[0];
            variant1_2_2.txt.text = c2[1];
            variant1_3_2.txt.text = c2[2];
            variant2_1_2.txt.text = c2[3];
            variant2_2_2.txt.text = c2[4];
            variant2_3_2.txt.text = c2[5];
            variant3_1_2.txt.text = c2[6];
            variant3_2_2.txt.text = c2[7];
            variant3_3_2.txt.text = c2[8];
            variant4_1_2.txt.text = c2[9];
            variant4_2_2.txt.text = c2[10];
            variant4_3_2.txt.text = c2[11];
            variant5_1_2.txt.text = c2[12];
            variant5_2_2.txt.text = c2[13];
            variant5_3_2.txt.text = c2[14];
            variant6_1_2.txt.text = c2[15];
            variant6_2_2.txt.text = c2[16];
            variant6_3_2.txt.text = c2[17];

            //-------------------------------

            variant1_1_3.txt.text = c3[0];
            variant1_2_3.txt.text = c3[1];
            variant1_3_3.txt.text = c3[2];
            variant2_1_3.txt.text = c3[3];
            variant2_2_3.txt.text = c3[4];
            variant2_3_3.txt.text = c3[5];
            variant3_1_3.txt.text = c3[6];
            variant3_2_3.txt.text = c3[7];
            variant3_3_3.txt.text = c3[8];
            variant4_1_3.txt.text = c3[9];
            variant4_2_3.txt.text = c3[10];
            variant4_3_3.txt.text = c3[11];
            variant5_1_3.txt.text = c3[12];
            variant5_2_3.txt.text = c3[13];
            variant5_3_3.txt.text = c3[14];
            variant6_1_3.txt.text = c3[15];
            variant6_2_3.txt.text = c3[16];
            variant6_3_3.txt.text = c3[17];

            //------------------------------

            variant1_1_4.txt.text = c4[0];
            variant1_2_4.txt.text = c4[1];
            variant1_3_4.txt.text = c4[2];
            variant2_1_4.txt.text = c4[3];
            variant2_2_4.txt.text = c4[4];
            variant2_3_4.txt.text = c4[5];
            variant3_1_4.txt.text = c4[6];
            variant3_2_4.txt.text = c4[7];
            variant3_3_4.txt.text = c4[8];
            variant4_1_4.txt.text = c4[9];
            variant4_2_4.txt.text = c4[10];
            variant4_3_4.txt.text = c4[11];
            variant5_1_4.txt.text = c4[12];
            variant5_2_4.txt.text = c4[13];
            variant5_3_4.txt.text = c4[14];
            variant6_1_4.txt.text = c4[15];
            variant6_2_4.txt.text = c4[16];
            variant6_3_4.txt.text = c4[17];

            //------------------------------

            variant1_1_5.txt.text = c5[0];
            variant1_2_5.txt.text = c5[1];
            variant1_3_5.txt.text = c5[2];
            variant2_1_5.txt.text = c5[3];
            variant2_2_5.txt.text = c5[4];
            variant2_3_5.txt.text = c5[5];
            variant3_1_5.txt.text = c5[6];
            variant3_2_5.txt.text = c5[7];
            variant3_3_5.txt.text = c5[8];
            variant4_1_5.txt.text = c5[9];
            variant4_2_5.txt.text = c5[10];
            variant4_3_5.txt.text = c5[11];
            variant5_1_5.txt.text = c5[12];
            variant5_2_5.txt.text = c5[13];
            variant5_3_5.txt.text = c5[14];
            variant6_1_5.txt.text = c5[15];
            variant6_2_5.txt.text = c5[16];
            variant6_3_5.txt.text = c5[17];

            //------------------------------

            variant1_1_6.txt.text = c6[0];
            variant1_2_6.txt.text = c6[1];
            variant1_3_6.txt.text = c6[2];
            variant2_1_6.txt.text = c6[3];
            variant2_2_6.txt.text = c6[4];
            variant2_3_6.txt.text = c6[5];
            variant3_1_6.txt.text = c6[6];
            variant3_2_6.txt.text = c6[7];
            variant3_3_6.txt.text = c6[8];
            variant4_1_6.txt.text = c6[9];
            variant4_2_6.txt.text = c6[10];
            variant4_3_6.txt.text = c6[11];
            variant5_1_6.txt.text = c6[12];
            variant5_2_6.txt.text = c6[13];
            variant5_3_6.txt.text = c6[14];
            variant6_1_6.txt.text = c6[15];
            variant6_2_6.txt.text = c6[16];
            variant6_3_6.txt.text = c6[17];

            //------------------------------

            variant1_1_7.txt.text = c7[0];
            variant1_2_7.txt.text = c7[1];
            variant1_3_7.txt.text = c7[2];
            variant2_1_7.txt.text = c7[3];
            variant2_2_7.txt.text = c7[4];
            variant2_3_7.txt.text = c7[5];
            variant3_1_7.txt.text = c7[6];
            variant3_2_7.txt.text = c7[7];
            variant3_3_7.txt.text = c7[8];
            variant4_1_7.txt.text = c7[9];
            variant4_2_7.txt.text = c7[10];
            variant4_3_7.txt.text = c7[11];
            variant5_1_7.txt.text = c7[12];
            variant5_2_7.txt.text = c7[13];
            variant5_3_7.txt.text = c7[14];
            variant6_1_7.txt.text = c7[15];
            variant6_2_7.txt.text = c7[16];
            variant6_3_7.txt.text = c7[17];

            //------------------------------

            variant1_1_8.txt.text = c8[0];
            variant1_2_8.txt.text = c8[1];
            variant1_3_8.txt.text = c8[2];
            variant2_1_8.txt.text = c8[3];
            variant2_2_8.txt.text = c8[4];
            variant2_3_8.txt.text = c8[5];
            variant3_1_8.txt.text = c8[6];
            variant3_2_8.txt.text = c8[7];
            variant3_3_8.txt.text = c8[8];
            variant4_1_8.txt.text = c8[9];
            variant4_2_8.txt.text = c8[10];
            variant4_3_8.txt.text = c8[11];
            variant5_1_8.txt.text = c8[12];
            variant5_2_8.txt.text = c8[13];
            variant5_3_8.txt.text = c8[14];
            variant6_1_8.txt.text = c8[15];
            variant6_2_8.txt.text = c8[16];
            variant6_3_8.txt.text = c8[17];

            //------------------------------

            variant1_1_9.txt.text = c9[0];
            variant1_2_9.txt.text = c9[1];
            variant1_3_9.txt.text = c9[2];
            variant2_1_9.txt.text = c9[3];
            variant2_2_9.txt.text = c9[4];
            variant2_3_9.txt.text = c9[5];
            variant3_1_9.txt.text = c9[6];
            variant3_2_9.txt.text = c9[7];
            variant3_3_9.txt.text = c9[8];
            variant4_1_9.txt.text = c9[9];
            variant4_2_9.txt.text = c9[10];
            variant4_3_9.txt.text = c9[11];
            variant5_1_9.txt.text = c9[12];
            variant5_2_9.txt.text = c9[13];
            variant5_3_9.txt.text = c9[14];
            variant6_1_9.txt.text = c9[15];
            variant6_2_9.txt.text = c9[16];
            variant6_3_9.txt.text = c9[17];
        }

        public function Ticket():void {
            populateBoxes();
            externalizeNumbers();
        }

        public function externalizeNumbers(): void {
            var variant_1:Array = new Array(variant1_1_1.txt.text, variant1_1_2.txt.text, variant1_1_3.txt.text, variant1_1_4.txt.text, variant1_1_5.txt.text, variant1_1_6.txt.text, variant1_1_7.txt.text, variant1_1_8.txt.text, variant1_1_9.txt.text,
                                        variant1_2_1.txt.text, variant1_2_2.txt.text, variant1_2_3.txt.text, variant1_2_4.txt.text, variant1_2_5.txt.text, variant1_2_6.txt.text, variant1_2_7.txt.text, variant1_2_8.txt.text, variant1_2_9.txt.text,
                                        variant1_3_1.txt.text, variant1_3_2.txt.text, variant1_3_3.txt.text, variant1_3_4.txt.text, variant1_3_5.txt.text, variant1_3_6.txt.text, variant1_3_7.txt.text, variant1_3_8.txt.text, variant1_3_9.txt.text);

            var variant_2:Array = new Array(variant2_1_1.txt.text, variant2_1_2.txt.text, variant2_1_3.txt.text, variant2_1_4.txt.text, variant2_1_5.txt.text, variant2_1_6.txt.text, variant2_1_7.txt.text, variant2_1_8.txt.text, variant2_1_9.txt.text,
                                        variant2_2_1.txt.text, variant2_2_2.txt.text, variant2_2_3.txt.text, variant2_2_4.txt.text, variant2_2_5.txt.text, variant2_2_6.txt.text, variant2_2_7.txt.text, variant2_2_8.txt.text, variant2_2_9.txt.text,
                                        variant2_3_1.txt.text, variant2_3_2.txt.text, variant2_3_3.txt.text, variant2_3_4.txt.text, variant2_3_5.txt.text, variant2_3_6.txt.text, variant2_3_7.txt.text, variant2_3_8.txt.text, variant2_3_9.txt.text);

            var variant_3:Array = new Array(variant3_1_1.txt.text, variant3_1_2.txt.text, variant3_1_3.txt.text, variant3_1_4.txt.text, variant3_1_5.txt.text, variant3_1_6.txt.text, variant3_1_7.txt.text, variant3_1_8.txt.text, variant3_1_9.txt.text,
                                        variant3_2_1.txt.text, variant3_2_2.txt.text, variant3_2_3.txt.text, variant3_2_4.txt.text, variant3_2_5.txt.text, variant3_2_6.txt.text, variant3_2_7.txt.text, variant3_2_8.txt.text, variant3_2_9.txt.text,
                                        variant3_3_1.txt.text, variant3_3_2.txt.text, variant3_3_3.txt.text, variant3_3_4.txt.text, variant3_3_5.txt.text, variant3_3_6.txt.text, variant3_3_7.txt.text, variant3_3_8.txt.text, variant3_3_9.txt.text);

            var variant_4:Array = new Array(variant4_1_1.txt.text, variant4_1_2.txt.text, variant4_1_3.txt.text, variant4_1_4.txt.text, variant4_1_5.txt.text, variant4_1_6.txt.text, variant4_1_7.txt.text, variant4_1_8.txt.text, variant4_1_9.txt.text,
                                        variant4_2_1.txt.text, variant4_2_2.txt.text, variant4_2_3.txt.text, variant4_2_4.txt.text, variant4_2_5.txt.text, variant4_2_6.txt.text, variant4_2_7.txt.text, variant4_2_8.txt.text, variant4_2_9.txt.text,
                                        variant4_3_1.txt.text, variant4_3_2.txt.text, variant4_3_3.txt.text, variant4_3_4.txt.text, variant4_3_5.txt.text, variant4_3_6.txt.text, variant4_3_7.txt.text, variant4_3_8.txt.text, variant4_3_9.txt.text);

            var variant_5:Array = new Array(variant5_1_1.txt.text, variant5_1_2.txt.text, variant5_1_3.txt.text, variant5_1_4.txt.text, variant5_1_5.txt.text, variant5_1_6.txt.text, variant5_1_7.txt.text, variant5_1_8.txt.text, variant5_1_9.txt.text,
                                        variant5_2_1.txt.text, variant5_2_2.txt.text, variant5_2_3.txt.text, variant5_2_4.txt.text, variant5_2_5.txt.text, variant5_2_6.txt.text, variant5_2_7.txt.text, variant5_2_8.txt.text, variant5_2_9.txt.text,
                                        variant5_3_1.txt.text, variant5_3_2.txt.text, variant5_3_3.txt.text, variant5_3_4.txt.text, variant5_3_5.txt.text, variant5_3_6.txt.text, variant5_3_7.txt.text, variant5_3_8.txt.text, variant5_3_9.txt.text);

            var variant_6:Array = new Array(variant6_1_1.txt.text, variant6_1_2.txt.text, variant6_1_3.txt.text, variant6_1_4.txt.text, variant6_1_5.txt.text, variant6_1_6.txt.text, variant6_1_7.txt.text, variant6_1_8.txt.text, variant6_1_9.txt.text,
                                        variant6_2_1.txt.text, variant6_2_2.txt.text, variant6_2_3.txt.text, variant6_2_4.txt.text, variant6_2_5.txt.text, variant6_2_6.txt.text, variant6_2_7.txt.text, variant6_2_8.txt.text, variant6_2_9.txt.text,
                                        variant6_3_1.txt.text, variant6_3_2.txt.text, variant6_3_3.txt.text, variant6_3_4.txt.text, variant6_3_5.txt.text, variant6_3_6.txt.text, variant6_3_7.txt.text, variant6_3_8.txt.text, variant6_3_9.txt.text);

            trace(variant_1);
            trace(variant_2);
            trace(variant_3);
            trace(variant_4);
            trace(variant_5);
            trace(variant_6);
        }
    }

}

Answers:


Ok, so like I said in the comment, your script can be shortened drastically. Here's how. 90 Lines instead of 300+, and no double writing code or instantiating ;) I called the Class Main, but you can obviously change that to your needs. I also generate the fields dynamically for simplicity, the boards Array holds all the boards and is publicly accessible.

package  {
    import flash.display.Sprite;
    import flash.text.TextField;

    public class Main extends Sprite{

        public var boards:Array = new Array();
        private static const AMAUNT_BOARDS:uint = 6;
        private static const NUMBER_FIELD_SIZE:uint = 20;

        public function Main() {


            for(var i:uint = 0; i < AMAUNT_BOARDS; i++)
            {
                var numbers:Array = genNumbers();
                numbers = deleteFields(numbers);
                var board:Sprite = getBoard(numbers);
                board.y = NUMBER_FIELD_SIZE * 4 * i;
                boards.push(board);
                addChild(board);
            }           
        }

        //generates a 2 Dimensional Array (3x9) with TextFields
        //populates the according Numbers and returns a board Sprite
        private function getBoard(n:Array):Sprite
        {
            var s:Sprite = new Sprite();
            var a:Array = new Array();
            for(var i:uint = 0; i < 3; i++)
            {
                var b:Array = new Array();
                for(var k:uint = 0; k < 9; k++)
                {
                    //create TextFields
                    var tf:TextField = new TextField();
                    tf.x = k * NUMBER_FIELD_SIZE;
                    tf.y = i * NUMBER_FIELD_SIZE;
                    tf.border = true;
                    tf.width = NUMBER_FIELD_SIZE;
                    tf.height = NUMBER_FIELD_SIZE;                  

                    if(n[k][i] != 0) // adds the number if the value isn't 0
                        tf.text = n[k][i]; // Note that i am switching k & i because the number Array is 9x3

                    b.push(tf);
                    s.addChild(tf);
                }           
            }       
            return s;
        }

        // Turns 4 random numbers out of the lines into 0 And returns the Array
        private function deleteFields(a:Array):Array
        {
            for(var i:uint = 0; i < 3; i++)
            {
                var r:RandomPlus = new RandomPlus(8,0);
                for(var k:uint = 0; k < 4; k++)
                {
                        var t:uint = r.getNum();
                        a[t][i] = 0;
                }
            }           
            return a;
        }

        // Generates and returns a 2 Dimensional Array (9x3) with random numbers
        private function genNumbers():Array
        {
            var a:Array = new Array();
            var st:uint = 1;
            var en:uint = 9;
            for(var i:uint = 0; i < 9; i++)
            {
                var line:Array = new Array();
                var ran:RandomPlus = new RandomPlus(en,st);

                if(i == 0)//accounting for the number differnenz at start
                    st--;

                if(i==7)//accounting for the number differnenz at end
                    en ++;

                st += 10;
                en += 10;

                for(var e:uint = 0; e < 3; e++)
                    line[e] = ran.getNum();

                a.push(line);               
            }
            return a;
        }
    }
}

Outputs this:

Bingo

I didn't really comment the code, because I don't have time. Have a look and play around. I can comment later if you can't follow. I made the field sizes 20*20, change that or anything else to your needs.
Enjoy ;)

EDIT: Added some comments and the Field size constant var!