flash on 2010-4-14

by kihon
♥0 | Line 113 | Modified 2010-04-14 00:17:29 | MIT License
play

ActionScript3 source code

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

package
{
	import flash.display.Sprite;
	import flash.events.Event;
 
	[SWF(width="465", height="465", frameRate="6", backgroundColor="0x0")]
	public class Main extends Sprite
	{
		private var board:Board;
 
		public function Main()
		{
			board = new Board(this.graphics);
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
		}
 
		private function onEnterFrame(event:Event):void
		{
			board.update();
		}
	}
}
 
import flash.display.Graphics;
import flash.geom.Matrix;
import flash.utils.ByteArray;
 
class Board
{
	public var WIDTH:Number;
	public var HEIGHT:Number;
	public const SIZE:int = 10;
 
	public var board:Array = 
	[
		[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
		[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
		[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1], 
		[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1], 
		[1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
		[1, 1, 0, 0, 0, 0, 0, 0, 0, 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0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
		[0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
		[0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
		[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
		[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
		[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
	];
	private var temp:Array;
	public var g:Graphics;
 
	public function Board(g:Graphics)
	{
		this.g = g;
		WIDTH = board[0].length;
		HEIGHT = board.length;
		temp = clone(board);
	}
 
	public function update():void
	{
		for (var y:int = 0; y < HEIGHT; y++)
		{
			for (var x:int = 0; x < WIDTH; x++)
			{				
				var count:int = 0;
				for (var yy:int = -1; yy <= 1; yy++)
				{
					for (var xx:int = -1; xx <= 1; xx++)
					{
						if (yy == 0 && xx == 0) continue;
						if (y + yy < 0 || HEIGHT <= y + yy || x + xx < 0 || WIDTH <= x + xx) continue;
						if (board[y + yy][x + xx] == Status.ALIVE) count++;
					}
				}
 
				if (count == 2) temp[y][x] = board[y][x];
				else if (count == 3) temp[y][x] = Status.ALIVE;
				else temp[y][x] = Status.DEAD;
			}
		}
 
		board = clone(temp);
 
		draw();
	}
 
	private function clone(array:Array):Array
	{
		var b:ByteArray = new ByteArray();
		b.writeObject(array);
		b.position = 0;
		return b.readObject();
	}
 
	private function draw():void
	{
		g.clear();
		g.lineStyle(1.0);
 
		var matrix:Matrix = new Matrix();
 
		for (var y:int = 0; y < HEIGHT; y++)
		{
			for (var x:int = 0; x < WIDTH; x++)
			{
				matrix.createGradientBox(SIZE, SIZE, 45 * Math.PI / 180, x * SIZE, y * SIZE);
				if (board[y][x] == Status.ALIVE) g.beginGradientFill("linear", [0xffc905, 0xffff00], [1.0, 1.0], [0, 255], matrix);
				else if (board[y][x] == Status.DEAD) g.beginGradientFill("linear", [0x393939, 0x0], [1.0, 1.0], [0, 255], matrix);
 
				g.drawRect(x * SIZE, y * SIZE, SIZE, SIZE);
				g.endFill();
			}
		}
	}
}
 
class Status
{
	public static const DEAD:int = 0;
	public static const ALIVE:int = 1;
}