ライフゲーム

by barmamutha
♥0 | Line 192 | Modified 2010-06-03 18:15:12 | MIT License
play

ActionScript3 source code

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

package {
    import flash.display.*; 
    import flash.events.Event;  
	import net.hires.debug.Stats;
	import flash.geom.Rectangle;
	import flash.geom.Point;
    [SWF(width="465", height="465", backgroundColor="0xffffff", frameRate="60")]

    public class LifeGame extends Sprite {
	 	private var N:uint = 116;
		public static var field;
		private var _draw:BitmapData;
		private var rect:Rectangle=new Rectangle(0, 0, 1, 1);
		private var bitmapData_1:BitmapData = new BitmapData(1, 1, false, 0x00FF00);
		private var bitmapData_2:BitmapData = new BitmapData(1, 1, false, 0x000000);
		private var _life:Life;
        public function LifeGame() {
		
           init()
		   var no = 10;
			while(no>=1){ 
				var i:uint = Math.random() * (N-5) + 3; 
				var j:uint = Math.random()  * (N-5) + 3; 
				create(i, j); 
				no--
			}
			
			_draw = new BitmapData(N, N, false, 0x000000);
			var bm:Bitmap = this.addChild(new Bitmap(this._draw)) as Bitmap;
			
			draw();
			addEventListener( Event.ENTER_FRAME, onEnterFrameHandler );
			bm.scaleX=4
			bm.scaleY=4
			addChild(new Stats());

			
        }
		public function onEnterFrameHandler(event:Event) :void{ 
			
			_draw.lock();
			for(var i:uint = 0; i < N; ++i) {
				for(var j:uint = 0; j < N; ++j){ 
					field[i][j].next()
				}
			}
			for(i = 0; i < N; ++i) {
				for(j = 0; j < N; ++j){ 
					_life=field[i][j]
					if(_life.update()){
						if(_life.state_now == 1){
							_draw.setPixel(i, j, 0x00FF00);
							//_draw.copyPixels(bitmapData_1, rect, _life.point);
						}else{
							_draw.setPixel(i, j, 0x000000);
							//_draw.copyPixels(bitmapData_2, rect, _life.point);
						}

					}
				}
			}
			_draw.unlock();
			
		}
		public function init() :void{
			field = new Vector.<Vector.<Life>>(N);
			for(var i:uint = 0; i < N; ++i) {
				//field.push(new Vector.<Life>())
				var Vec:Vector.<Life>=new Vector.<Life>(N)
				for(var j:uint = 0; j < N; ++j){ 
					Vec[j]=new Life(i,j);
				}
				field[i]=Vec;
			}
			for(i = 0; i < N; ++i) {
				for(j = 0; j < N; ++j){ 
					field[i][j].init(N);
				}
			}
			
		}
		public function create(i, j) :void{ 
			field[i][j+1].active(); 
			field[i-1][j].active(); 
			field[i][j].active(); 
			field[i+1][j].active(); 
			field[i+1][j-1].active();
			
		}
		public function draw() :void{ 
			
			_draw.lock();
			
			for(var i:uint = 0; i < N; ++i) {
				for(var j:uint = 0; j < N; ++j){ 
					if(field[i][j].state_now == 1){
						_draw.setPixel(i, j, 0x00FF00);
					}else{
						_draw.setPixel(i, j, 0x000000);
					}
				}
			}
			_draw.unlock();
		}
    }
}
import LifeGame;  
import flash.geom.Point;
class Life{
	private var myCheckfield:Vector.<Life> = new Vector.<Life>();
	private var row:uint;
	private var col:uint; 
    public var state_now:uint;  
    public var state_next:uint; 
    public var point:Point; 
	function Life(r:uint, c:uint, s:uint=0){
		row = r;
		col = c;
		state_now = s;
		state_next = 0;
		point=new Point(r,c)
	}
	public function init(n:uint){
		--n;
		trace(row)
		if(row!=0 && row !=n && col!=0 && col !=n){
		trace(row)
			myCheckfield.push(LifeGame.field[row - 1][col -1])
			myCheckfield.push(LifeGame.field[row - 1][col])
			myCheckfield.push(LifeGame.field[row - 1][col +1])
			myCheckfield.push(LifeGame.field[row][col -1])
			myCheckfield.push(LifeGame.field[row][col +1])
			myCheckfield.push(LifeGame.field[row + 1][col -1])
			myCheckfield.push(LifeGame.field[row + 1][col])
			myCheckfield.push(LifeGame.field[row + 1][col +1])
		}else if(row==0){
			myCheckfield.push(LifeGame.field[row + 1][col])
			if(col==0){
				myCheckfield.push(LifeGame.field[row][col +1])
				myCheckfield.push(LifeGame.field[row + 1][col +1])
			}else if(col==n){
				myCheckfield.push(LifeGame.field[row][col -1])
				myCheckfield.push(LifeGame.field[row + 1][col -1])
			}else {
				myCheckfield.push(LifeGame.field[row][col +1])
				myCheckfield.push(LifeGame.field[row + 1][col +1])
				myCheckfield.push(LifeGame.field[row][col -1])
				myCheckfield.push(LifeGame.field[row + 1][col -1])
			}
		}else if(row==n){
			myCheckfield.push(LifeGame.field[row - 1][col])
			if(col==0){
				myCheckfield.push(LifeGame.field[row][col +1])
				myCheckfield.push(LifeGame.field[row - 1][col +1])
			}else if(col==n){
				myCheckfield.push(LifeGame.field[row][col -1])
				myCheckfield.push(LifeGame.field[row - 1][col -1])
			}else {
				myCheckfield.push(LifeGame.field[row][col +1])
				myCheckfield.push(LifeGame.field[row - 1][col +1])
				myCheckfield.push(LifeGame.field[row][col -1])
				myCheckfield.push(LifeGame.field[row - 1][col -1])
			}
		}else if(col==0){
			myCheckfield.push(LifeGame.field[row - 1][col])
			myCheckfield.push(LifeGame.field[row - 1][col +1])
			myCheckfield.push(LifeGame.field[row][col +1])
			myCheckfield.push(LifeGame.field[row + 1][col])
			myCheckfield.push(LifeGame.field[row + 1][col +1])
		}else{
			myCheckfield.push(LifeGame.field[row - 1][col])
			myCheckfield.push(LifeGame.field[row - 1][col -1])
			myCheckfield.push(LifeGame.field[row][col -1])
			myCheckfield.push(LifeGame.field[row + 1][col])
			myCheckfield.push(LifeGame.field[row + 1][col -1])
		}

	 }
    private function getstate():uint {
		return state_now;
	}
	
    public function active():void { state_now = 1; } 
    public function update():Boolean {
		if(state_now!=state_next){
			state_now = state_next;
			return true
		}else{
			return false
		}
	} 


   public function next() {
		var LIVING = 0; 
		for each(var life:Life in myCheckfield) {
			LIVING+=life.state_now;			
		}
		
		if(state_now == 1){ 
		   if(LIVING == 2 || LIVING == 3) 
			   state_next = 1; 
		   else 
			   state_next = 0; 
		} 
		else{ 
			if(LIVING == 3) 
				state_next = 1; 
			else 
				state_next = 0; 
		}
	}
};

Forked