forked from: forked from: lifegame

by n0wri forked from forked from: lifegame (diff: 61)
lifegame ver 3.0
timerを外に出した

ライフゲーム game of life
♥0 | Line 218 | Modified 2011-08-22 18:30:33 | MIT License
play

ActionScript3 source code

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

// forked from n0wri's forked from: lifegame
// forked from n0wri's lifegame
// lifegame ver 3.0
// timerを外に出した

package
{
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	import net.hires.debug.Stats;
	import flash.text.TextField;
	import flash.display.StageScaleMode;
	import flash.display.StageAlign;
	import flash.display.Bitmap;
	import flash.display.Sprite;

	public class LifeGame extends Sprite
	{
		private var timer:Timer;
		private var lifeGame:LifeGameBase;
		public function LifeGame()
		{
			stage.align=StageAlign.TOP_LEFT;
			stage.scaleMode=StageScaleMode.NO_SCALE;
			
			lifeGame = new LifeGameBase(46,46,[0xff00ffff, 0xff00ff00, 0xff333333, 0xff666666, 0xff999999]);
			var bmp:Bitmap = new Bitmap(lifeGame.view);
			bmp.scaleX=bmp.scaleY=10;
			addChild(bmp);
			timer = new Timer(100);
			timer.addEventListener(TimerEvent.TIMER, timerFunc);
			timer.start();

			//stage.addChild(new Stats());
		}

		private function timerFunc(event:TimerEvent):void
		{
			lifeGame.next();
		}

	}
	
    
}


import flash.geom.Rectangle;
import flash.display.BitmapData;

class LifeGameBase
{
	private var _view:LifeGameView;
	private var _model:LifeGameModel;
	private var _control:LifeGameControl;
	
	public function LifeGameBase(x:int,y:uint,colorar:Array) 
	{
		
		_view = new LifeGameView(x,y,colorar);
		_model = new LifeGameModel(_view,x,y);
		_control = new LifeGameControl(_model);
		
		
	}
	
	public function next():Boolean
	{
		return _control.next();
	}
	

	public function get view():LifeGameView
	{
		return _view;
	}
}

class LifeGameControl
{
	private var _model:LifeGameModel;
	private var isFirst:Boolean=true;
	public function LifeGameControl(model:LifeGameModel) 
	{
		_model = model;
	}
	

	public function next():Boolean
	{
		if(isFirst)
		{
			isFirst=false;
			_model.setColorsRandom();
		};
		return _model.update();
	}
}

class LifeGameModel
{
	private var _view:LifeGameView;
	private var color_List:Array=[];
	private var color_ListX:uint;
	private var color_ListY:uint;
	private var mustUpdate:Boolean;
	private var baseFunc:Function;
	public function LifeGameModel(view:LifeGameView,x:int,y:int) 
	{
		_view = view;
		color_ListX = x;
		color_ListY = y;
		for(var i:uint=0; i<color_ListX;i++)
		{
			color_List[i]=[];
			for (var j : int = 0; j < color_ListY; j++) 
			{
				color_List[i][j] = false;
			};
		};
		
	}


	public function setColorsRandom():void
	{
		var len:uint = int(color_ListX*color_ListY*0.2);
		var sendAr:Array = [];
		for(var i : int = 0; i<len; i++) 
		{
			var x:uint = uint( Math.random()*color_ListX );
			var y:uint = uint( Math.random()*color_ListY );
			color_List[x][y]=true;
			sendAr.push([x,y]);
		};
		mustUpdate=true;
		noteUpdate(sendAr);
	}

	
		
	
	private function noteUpdate(sendar:Array):void
	{
		_view.update(sendar);
	}
	
	public function update():Boolean
	{
		var oldAr:Array = color_List.concat();
		var sendAr:Array = [];
		var calc:Function = function(x:uint,y:uint):uint
		{
			var flg:uint=0;
			var xAr:Array=[];
			var yAr:Array=[];
			 
			if(x)
			{
				xAr.push(x-1);
			};
			xAr.push(x);
			if(x+1<color_ListX)
			{
				xAr.push(x+1);
			};
			
			if(y)
			{
				yAr.push(y-1);
			}
			yAr.push(y);
			if(y+1 < color_ListY)
			{
				yAr.push(y+1);
			};
			
			var xArLen:uint=xAr.length;
			var yArLen:uint=yAr.length;
			
			for (var i : int = 0; i <xArLen; i++)
			{
				
				for(var j : int = 0; j < yArLen; j++) 
				{
					if(oldAr[xAr[i]][yAr[j]] && !(xAr[i]==x && yAr[j]==y))
					{
						flg++;
					};
				}
			}
			return flg;
		};
		
		mustUpdate =false;
		for(var i:uint=0; i<color_ListX;i++)
		{
			for (var j : int = 0; j < color_ListY; j++) 
			{
				var flg:uint = calc(i,j);
				
				var myState:Boolean = oldAr[i][j];
				
				if(!myState && flg==3)
				{
					if(!mustUpdate)mustUpdate=true;
					color_List[i][j]=true;
					sendAr.push([i,j]);
				}
				else if(myState && (flg==2 || flg ==3))
				{
					if(!mustUpdate)mustUpdate=true;
					color_List[i][j]=true;
					sendAr.push([i,j]);
				}
				else
				{
					color_List[i][j]=false;
				};
			};
		};
		noteUpdate(sendAr);
		if(!mustUpdate)
		{
			return false;
		};
		return true;
	}

}

class LifeGameView extends BitmapData
{
	private var _colorAr:Array;
	private var _colorArLen:uint;
	private var xLen:uint;
	private var yLen:uint;
	private var rec:Rectangle;
	private var recSrc:Rectangle;
	
	public function LifeGameView(w:uint,h:uint,colorAr:Array) 
	{
		super(w,h,true);
		_colorAr = colorAr;
		_colorArLen = _colorAr.length;
		xLen = w;
		yLen = h;
		rec=new Rectangle(0,0,1,1);
		recSrc=new Rectangle(0,0,w,h);
	}
	
	public function update(ar:Array):void
	{
		this.fillRect(recSrc, 0x00);
		var len:uint = ar.length;
		var target:Array;
		for(var i:int=0; i<len; i++) 
		{
			target = ar[i];
			rec.x=target[0];
			rec.y=target[1];
			this.fillRect(rec, _colorAr[uint(Math.random()*_colorArLen)]);
		}
	}
}