ライフゲーム(グライダーガン)

by svartalf
wikipedia ライフゲーム
* http://ja.wikipedia.org/wiki/%E3%83%A9%E3%82%A4%E3%83%95%E3%82%B2%E3%83%BC%E3%83%A0
♥0 | Line 135 | Modified 2009-11-30 19:06:16 | MIT License
play

ActionScript3 source code

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

/**
 * wikipedia ライフゲーム
 * http://ja.wikipedia.org/wiki/%E3%83%A9%E3%82%A4%E3%83%95%E3%82%B2%E3%83%BC%E3%83%A0
 */
package 
{
	import flash.display.Sprite;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	/**
	 * LifeGame
	 * @author svartalf
	 */
	[SWF(width='300',height='300',backgroundColor='0xFFFFFF')]
	public class Main extends Sprite 
	{
		private var bitmap:Bitmap;
		
		private var newMap:Array = [];
		private var diff:Array = [[ -1, -1], [0, -1], [1, -1], [ -1, 0], [1, 0], [ -1, 1], [0, 1], [1, 1]];
		private var map: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, 0, 0],
			[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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_STAGE, init);
		}
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			// entry point
			
			var bitmapData:BitmapData = new BitmapData(map[0].length, map.length, false, 0xFFFFFFFF);
			bitmap = new Bitmap(bitmapData);
			bitmap.scaleX = bitmap.scaleY = 5;
			addChild(bitmap);
			
			makeNewMap();
			
			timer = new Timer(100, 0);
			timer.addEventListener(TimerEvent.TIMER,timerHandler);
			timer.start();
		}
		
		/**
		 * newMapを初期化
		 */
		private function makeNewMap():void
		{
			var xlen:uint = map[0].length;
			var ylen:uint = map.length;
			for (var y:uint = 0; y < ylen; y++ )
			{
				newMap[y] = [];
				for (var x:uint = 0; x < xlen; x++)
				{
					newMap[y][x] = 0;
				}
			}
		}
		
		/**
		 * タイムイベントハンドラー
		 * 走査と描画を繰り返す
		 * @param	event
		 */
		private function timerHandler(event:TimerEvent):void
		{
			search();
			draw();
		}
		
		/**
		 * 走査処理
		 */
		private function search():void
		{
			var xlen:uint = map[0].length;
			var ylen:uint = map.length;
			var len:uint = diff.length;
			var count:uint = 0;
			
			for (var y:uint = 0; y < ylen; y++ )
			{
				for (var x:uint = 0; x < xlen; x++)
				{
					count = 0;
					
					for (var i:uint = 0; i < len; i++ )
					{
						var diffX:int = x + diff[i][0];
						var diffY:int = y + diff[i][1];
						if( diffY >= 0 && diffY < ylen && diffX >= 0 && diffX < xlen )
						{
							if ( map[diffY][diffX] == 1 )
							{
								count++;
							}
						}
					}
					
					if ( count == 3 && map[y][x] == 0)
					{
						newMap[y][x] = 1;
					}
					else if( count >= 2 && count <= 3 && map[y][x] == 1 )
					{
						newMap[y][x] = 1;
					}
					else
					{
						newMap[y][x] = 0;
					}
				}
			}
			
			for (i = 0; i < map.length; i++)
			{
				map[i] = newMap[i].concat();
			}
		}
		
		/**
		 * 描画処理
		 */
		private function draw():void
		{
			var xlen:uint = newMap[0].length;
			var ylen:uint = newMap.length;
			var bitmapData:BitmapData = bitmap.bitmapData;
			bitmapData.fillRect(bitmapData.rect, 0xFFFFFF);
			for (var y:uint = 0; y < ylen; y++ )
			{
				for (var x:uint = 0; x < xlen; x++)
				{
					if (newMap[y][x] == 1)
					{
						bitmapData.setPixel(x, y, 0x000000);
					}
				}	
			}
		}
	}
}