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

Totalistic Cellular Automata

http://mathworld.wolfram.com/TotalisticCellularAutomaton.html
Get Adobe Flash player
by greentec 10 Jul 2014
/**
 * Copyright greentec ( http://wonderfl.net/user/greentec )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/aQuS
 */

// forked from greentec's 1D Cellular Automata
package {
    import com.bit101.components.ComboBox;
    import com.bit101.components.Label;
    import com.bit101.components.PushButton;
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.geom.Rectangle;

    public class FlashTest extends Sprite {
        
        public var mainBitmap:Bitmap;
        public var mainBitmapData:BitmapData;
        
        public var data:Vector.<Vector.<int>> = new Vector.<Vector.<int>>(465);
        public var rule:Object = { };
        public var ruleButtonArray:Array = [];
        public var ruleNum:int;
        public var ruleLabel:Label;
        
        public var initCombo:ComboBox;
        public var colors:Array = [0xffffff, 0x7f7f7f, 0x000000];
        public var ruleButtonBitmaps:Array = [];

        public function FlashTest() {
            // write as3 code here..
            stage.scaleMode = "noScale";
            
            mainBitmapData = new BitmapData(465, 465, false, 0xffffff);
            mainBitmap = new Bitmap(mainBitmapData);
            addChild(mainBitmap);
            
            rule["6"] = 1; //rule 739
            rule["5"] = 0;
            rule["4"] = 0;
            rule["3"] = 0;
            rule["2"] = 1;
            rule["1"] = 0;
            rule["0"] = 1;
            
            var i:int;
            var j:int;
            var str:String;
            
            for (i = 0; i < 465; i += 1)
            {
                data[i] = new Vector.<int>(465);
            }
            
            data[0][0] = 1; //initialization
            
            for (i = 0; i < 464; i += 1)
            {
                for (j = 1; j < 464; j += 1)
                {
                    str = String(data[i][j - 1] + data[i][j] + data[i][j + 1]);
                    
                    data[i + 1][j] = rule[str];
                }
            }
            
            for (i = 0; i < 465; i += 1) //draw
            {
                for (j = 0; j < 465; j += 1)
                {
                    if (data[i][j] == 0)
                    {
                        mainBitmapData.setPixel(j, i, 0xffffff);
                    }

                    else if (data[i][j] == 1)

                    {
                        mainBitmapData.setPixel(j, i, 0x7f7f7f);
                    }
                    else
                    {
                        mainBitmapData.setPixel(j, i, 0x0);
                    }
                }
            }
            
            var resetButton:PushButton = new PushButton(this, 465 - 110, 10, "Reset", onReset);
            //resetButton.alpha = 0.8;

            initCombo = new ComboBox(this, resetButton.x, resetButton.y + resetButton.height + 5, "Select Init", ["one left", "one middle", "one right", "25% random", "50% random", "75% random"]);
            //initCombo.alpha = 0.8;
            
            var _ruleButton:PushButton;
            var _bitmapData:BitmapData;
            var _bitmap:Bitmap;
            var colorNum:uint;
            for (i = 0; i < 7; i += 1)
            {
                _ruleButton = new PushButton(this, 10 + i * 30, 10, "", onChangeRule);
                _ruleButton.name = String(6-i);
                _ruleButton.width = 30;
                _ruleButton.height = 30;
                //_ruleButton.alpha = 0.8;
                
                ruleButtonArray.push(_ruleButton);
                
                _bitmapData = new BitmapData(26, 26, false, 0xffffff);
                colorNum = 255 / 6 * i;
                colorNum = colorNum << 16 | colorNum << 8 | colorNum;

                _bitmapData.fillRect(new Rectangle(0, 2, 26, 10), 0x00007f);
                _bitmapData.fillRect(new Rectangle(1, 3, 24, 8), colorNum);
                _bitmapData.fillRect(new Rectangle(8, 14, 10, 10), 0x00007f);
                colorNum = (2 - rule[_ruleButton.name]) * 127;
                colorNum = colorNum << 16 | colorNum << 8 | colorNum;
                _bitmapData.fillRect(new Rectangle(9, 15, 8, 8), colorNum);
                _bitmap = new Bitmap(_bitmapData);
                _bitmap.x = 2;
                _bitmap.y = 2;
                ruleButtonBitmaps.push(_bitmapData);
                _ruleButton.addChild(_bitmap);
            }
            
            ruleLabel = new Label(this, 10, _ruleButton.y + _ruleButton.height + 5, "");
            
            ruleNum = 0;
            for (str in rule)
            {
                ruleNum += rule[str] * Math.pow(3, parseInt(str));
                ruleLabel.text = "Rule " + String(ruleNum);
            }
        }
        
        private function onChangeRule(e:Event):void
        {
            var name:String = e.target.name;
            
            rule[name] += 1;
            rule[name] %= 3;
            
            var colorNum:uint;
            colorNum = (2 - rule[name]) * 127;
            colorNum = colorNum << 16 | colorNum << 8 | colorNum;
            
            ruleButtonBitmaps[6-parseInt(name)].fillRect(new Rectangle(9, 15, 8, 8), colorNum);
            
            
            ruleNum = 0;
            
            var str:String;
            for (str in rule)
            {
                ruleNum += rule[str] * Math.pow(3, parseInt(str));
                ruleLabel.text = "Rule " + String(ruleNum);
            }
        }
        
        private function onReset(e:Event = null):void
        {
            mainBitmapData.fillRect(mainBitmapData.rect, 0xffffff);
            
            var i:int, j:int;
            var str:String;
            
            data[0] = new Vector.<int>(465);
            
            switch(initCombo.selectedItem)
            {
                case "one left":
                    data[0][0] = 1;
                    break;
                    
                case "one middle":
                    data[0][int(465 / 2)] = 1;
                    break;
                    
                case "one right":
                    data[0][464] = 1;
                    break;
                    
                case "25% random":
                    for (i = 0; i < 465; i += 1)
                    {
                        if (Math.random() < 0.25)
                        {
                            data[0][i] = 1;
                        }
                    }
                    break;
                    
                case "50% random":
                    for (i = 0; i < 465; i += 1)
                    {
                        if (Math.random() < 0.5)
                        {
                            data[0][i] = 1;
                        }
                    }
                    break;
                    
                case "75% random":
                    for (i = 0; i < 465; i += 1)
                    {
                        if (Math.random() < 0.75)
                        {
                            data[0][i] = 1;
                        }
                    }
                    break;
                    
                default:
                    data[0][int(465 / 2)] = 1;
                    break;
            }           
           
            for (i = 0; i < 464; i += 1)
            {
                for (j = 1; j < 464; j += 1)
                {
                    str = String(data[i][j - 1] + data[i][j] + data[i][j + 1]);
                    
                    data[i + 1][j] = rule[str];
                }
            }
            
            for (i = 0; i < 465; i += 1) //draw
            {
                for (j = 0; j < 465; j += 1)
                {
                    if (data[i][j] == 0)
                    {
                        mainBitmapData.setPixel(j, i, 0xffffff);
                    }
                    else if (data[i][j] == 1)
                    {
                        mainBitmapData.setPixel(j, i, 0x7f7f7f);
                    }
                    else
                    {
                        mainBitmapData.setPixel(j, i, 0x0);
                    }
                }
            }
        }
    }
}