In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

Simple Cellular Automata - Randomize

/**
 * Copyright greentec ( http://wonderfl.net/user/greentec )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/2WY8
 */

// forked from greentec's Simple Cellular Automata
package {
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.geom.Rectangle;
    import flash.utils.ByteArray;
    import com.bit101.components.PushButton;
    import com.bit101.components.Label;
    import flash.geom.ColorTransform;

    public class FlashTest extends Sprite {
        
        public var _bitmapData:BitmapData;
        public var _bitmap:Bitmap;
        public var gridSprite:Sprite;
        
        public var tileNum_X:int = 465/5;
        public var tileNum_Y:int = 465/5;
        public var tile_width:int = 5;
        public var tile_height:int = 5; 
        
        public var len:int = 465 / 5;
        public var tiles:Array = [];
        public var cloneTiles:Array;
        public var touchArray:Array = [];
        
        public var rules:Array = [0,0,1,1,1,0,1,0,1];
        public var ruleLabel:Label;

        public var gen:uint = 0;
        public var genMax:uint = 465 / 5 / 2;        
        
        
        public function FlashTest() {
            // write as3 code here..
            _bitmapData = new BitmapData(465, 465, false, 0x292929);
            _bitmap = new Bitmap(_bitmapData);
            addChild(_bitmap);
            
            stage.scaleMode = "noScale";
            
            //draw Line
            var i:int, j:int;
            gridSprite = new Sprite(); //draw Line
            gridSprite.graphics.lineStyle(0, 0x7f7f7f);
            
            for (i = 0; i < tileNum_X + 1; i += 1)
            {
                gridSprite.graphics.moveTo(i * tile_width, 0);
                gridSprite.graphics.lineTo(i * tile_width, tileNum_Y * tile_height);
            }
            for (i = 0; i < tileNum_Y + 1; i += 1)
            {
                gridSprite.graphics.moveTo(0, i * tile_height);
                gridSprite.graphics.lineTo(tileNum_X * tile_width, i * tile_height);
            }
            
            addChild(gridSprite);
            
            //init
            for (i = 0; i < len; i += 1)
            {
                tiles[i] = [];
                touchArray[i] = [];
                for (j = 0; j < len; j += 1)
                {
                    tiles[i].push(false);
                    touchArray[i].push(0);
                }
            }
            
            for (i = -3; i < 4; i += 1)
            {
                tiles[int(len / 2) + i][int(len / 2)] = true;
                touchArray[int(len / 2) + 1 + i][int(len / 2) + 1] = 1;
            }
            
            ruleLabel = new Label(this, 10, 10, "");
            ruleLabel.transform.colorTransform = new ColorTransform(0, 0, 0, 1, 255, 255, 255, 0);
            //initRules();
            var str:String = "Rule : ";
            for (i = 0; i < 9; i += 1)
            {
                str += String(rules[i]);
            }
            ruleLabel.text = str;
            
            var _thisRuleAgain:PushButton = new PushButton(this, 10, 465 - 30, "This Again", onThisRuleAgain);
            var _randomRuleAgain:PushButton = new PushButton(this, _thisRuleAgain.x + _thisRuleAgain.width + 10, _thisRuleAgain.y, "Random Again", onRandomRuleAgain);
            
            addEventListener(Event.ENTER_FRAME, onEnterFrame);
        }
        
        public function resetTiles():void
        {
            var i:int, j:int;
            for (i = 0; i < len; i += 1)
            {
                for (j = 0; j < len; j += 1)
                {
                    tiles[i][j] = false;
                    touchArray[i][j] = 0;
                }
            }
        }
        
        public function onRandomRuleAgain(e:Event):void
        {
            removeEventListener(Event.ENTER_FRAME, onEnterFrame);
            resetTiles();
            resetRules();
            gen = 0;
            
            var i:int;
            for (i = -3; i < 4; i += 1)
            {
                tiles[int(len / 2) + 1 + i][int(len / 2) + 1] = true;
            }
            
            addEventListener(Event.ENTER_FRAME, onEnterFrame);
        }
        
        public function onThisRuleAgain(e:Event):void
        {
            removeEventListener(Event.ENTER_FRAME, onEnterFrame);
            resetTiles();
            gen = 0;
            
            var i:int;
            for (i = -3; i < 4; i += 1)
            {
                tiles[int(len / 2) + 1 + i][int(len / 2) + 1] = true;
            }
            
            addEventListener(Event.ENTER_FRAME, onEnterFrame);
        }
        
        public function resetRules():void
        {
            var i:int;
            for (i = 0; i < 9; i += 1)
            {
                rules[i]= int(Math.random() * 2);
            }
            
            var str:String = "Rule : ";
            for (i = 0; i < 9; i += 1)
            {
                str += String(rules[i]);
            }
            ruleLabel.text = str;
        }
        
        public function initRules():void
        {
            rules = [];
            
            var i:int;
            for (i = 0; i < 9; i += 1)
            {
                rules.push(int(Math.random() * 2));
            }
            
            var str:String = "Rule : ";
            for (i = 0; i < 9; i += 1)
            {
                str += String(rules[i]);
            }
            ruleLabel.text = str;
        }
        
        public function onEnterFrame(e:Event):void
        {
            if (gen < genMax)
            {
                doAutomata();
                drawTiles();
            }
        }
       
        public function doAutomata():void
        {
            var i:int;
            var j:int;
            var neighbor:int;
            
            cloneTiles = clone(tiles);
            
            for (i = 0; i < tiles.length; i += 1)
            {
                for (j = 0; j < tiles[i].length; j += 1)
                {
                    neighbor = 0;
                    if (i > 0)
                    {
                        if (tiles[i - 1][j] == true)
                        {
                            neighbor += 1;
                        }
                        
                        if (j > 0)
                        {
                            if (tiles[i - 1][j - 1] == true)
                            {
                                neighbor += 1;
                            }
                        }
                        
                        if (j < len - 1)
                        {
                            if (tiles[i - 1][j + 1] == true)
                            {
                                neighbor += 1;
                            }
                        }
                    }
                    if (i < len - 1)
                    {
                        if (tiles[i + 1][j] == true)
                        {
                            neighbor += 1;
                        }
                        
                        if (j > 0)
                        {
                            if (tiles[i + 1][j - 1] == true)
                            {
                                neighbor += 1;
                            }
                        }
                        
                        if (j < len - 1)
                        {
                            if (tiles[i + 1][j + 1] == true)
                            {
                                neighbor += 1;
                            }
                        }
                    }
                    if (j > 0)
                    {
                        if (tiles[i][j - 1] == true)
                        {
                            neighbor += 1;
                        }
                    }
                    if (j < len - 1)
                    {
                        if (tiles[i][j + 1] == true)
                        {
                            neighbor += 1;
                        }
                    }
                    
                    if (tiles[i][j] == true)
                    {
                        neighbor += 1;
                    }
                    
                    if (rules[neighbor] == 1)
                    {
                        cloneTiles[i][j] = true;
                        if (tiles[i][j] == false)
                        {
                            touchArray[i][j] += 1;
                        }
                    }
                    else
                    {
                        cloneTiles[i][j] = false;
                    }
                }
            }
            
            tiles = clone(cloneTiles);
            gen += 1;
        }
        
        public function drawTiles():void
        {
            var i:int;
            var j:int;
            var min:int;
            
            for (i = 0; i < tiles.length; i += 1)
            {
                for (j = 0; j < tiles[i].length; j += 1)
                {
                    if (tiles[i][j] == true)
                    {
                        min = touchArray[i][j] < 5 ? touchArray[i][j] : 5;
                        min = min * 30 + 60;
                        _bitmapData.fillRect(new Rectangle(j * 5, i * 5, 5, 5), min << 16 | min << 8 | min);
                    }
                    else
                    {
                        _bitmapData.fillRect(new Rectangle(j * 5, i * 5, 5, 5), 0x292929);
                    }
                }
            }
        }
        
        public function clone(source:Object):*
        {
            var myBA:ByteArray = new ByteArray();
            myBA.writeObject(source);
            myBA.position = 0;
            return(myBA.readObject()); 
        }
   }
}