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

flash on 2011-10-7

Get Adobe Flash player
by kihon 07 Oct 2011
package
{
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Loader;
    import flash.display.LoaderInfo;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.geom.Point;
    import flash.geom.Rectangle;
    import flash.net.URLRequest;
    import flash.system.LoaderContext;
 
    public class Main extends Sprite
    {
        private const SIZE:int = 8;
        private var SCALE:Number = 0.5;
        private var map:Array = [];
        private var tile:BitmapData;
        private var canvas:BitmapData;
        
        public function Main()
        {
            graphics.beginFill(0xACBCD7);
            graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
            graphics.endFill();
 
            canvas = new BitmapData(Map.WIDTH * SIZE, Map.HEIGHT * SIZE, true, 0x0);
            var bitmap:Bitmap = new Bitmap(canvas);
            bitmap.scaleX = bitmap.scaleY = SCALE;
            addChild(bitmap);
            
            var loader:Loader = new Loader();
            loader.contentLoaderInfo.addEventListener(Event.INIT, initHandler);
            loader.load(new URLRequest("http://assets.wonderfl.net/images/related_images/5/52/5250/525077ca6886504891442f9af2ea2f0e3589dde6"), new LoaderContext(true));
        }
        
        private function initHandler(event:Event):void
        {
            var loader:Loader = LoaderInfo(event.currentTarget).loader;
            tile = new BitmapData(loader.width, loader.height, false);
            tile.draw(loader);
            
            stage.addEventListener(MouseEvent.CLICK, onMouseClick);
            onMouseClick();
        }
 
        private function onMouseClick(event:MouseEvent = null):void 
        {
            map = new Map().getMap();
            draw();
        }
 
        private function draw():void
        {
            canvas.fillRect(canvas.rect, 0x0);
            for (var y:int = 0; y < Map.HEIGHT; y++)
            {
                for (var x:int = 0; x < Map.WIDTH; x++)
                {
                    if (map[y][x] == 1)
                    {
                        var index:int = 0;
                        if (0 <= y - 1 && map[y - 1][x] == 1) index += 1;
                        if (x + 1 < Map.WIDTH && map[y][x + 1] == 1) index += 2;
                        if (y + 1 < Map.HEIGHT && map[y + 1][x] == 1) index += 4; 
                        if (0 <= x - 1 && map[y][x - 1] == 1) index += 8; 
                        canvas.copyPixels(tile, new Rectangle(index * SIZE, 0, SIZE, SIZE), new Point(x * SIZE, y * SIZE));
                    }
                }
            }
        }
    }
}


class Map
{
    public static const WIDTH:int = 100;
    public static const HEIGHT:int = 100;
    private const ITERATIONS:int = 100000;
    private const SEA:int = 0;
    private const LAND:int = 1;
    private var map:Array;

    public function getMap():Array
    {
        map = [];
        for (var y:int = 0; y < HEIGHT; y++)
        {
            map[y] = [];
            for (var x:int = 0; x < WIDTH; x++)
            {
                if (Math.random() < 0.5) map[y][x] = SEA;
                else map[y][x] = LAND;
            }
        }

        for (var i:int = 0; i < ITERATIONS; i++)
        {
            var randomX:int = Math.random() * WIDTH;
            var randomY:int = Math.random() * HEIGHT;
            map[randomY][randomX] = (landCount(randomX, randomY) > 4) ? LAND : SEA;
        }
        
        return map;
    }

    private function landCount(tx:int, ty:int):int
    {
        var count:int = 0;

        for (var y:int = -1; y <= 1; y++)
        {
            for (var x:int = -1; x <= 1; x++)
            {
                if (checkTile(tx + x, ty + y)) 
                {
                    count++;
                }
            }
        }
        return count;
    }

    private function checkTile(x:int, y:int):Boolean
    {
        if (0 <= x && x < WIDTH &&
            0 <= y && y < HEIGHT)
        {
            if (map[y][x] != SEA) return true;
            else return false;

        }
        return false;
    }
}