/* ライフゲーム Game of Life 生命の誕生、進化、淘汰などのプロセスを簡易的なモデルで再現したシミュレーションゲーム ja http://ja.wikipedia.org/wiki/%E3%83%A9%E3%82%A4%E3%83%95%E3%82%B2%E3%83%BC%E3%83%A0 en http://en.wikipedia.org/wiki/Conway's_Game_of_Life グライダー銃 Glider gun ライフゲームのパターンのひとつで、無限にピクセル数が増え続けるパターン ja http://ja.wikipedia.org/wiki/%E3%82%B0%E3%83%A9%E3%82%A4%E3%83%80%E3%83%BC%E9%8A%83 en http://en.wikipedia.org/wiki/Glider_gun クリックすると画面上にグライダーを一つ生成します click stage, create a glider フルスクリーンにすると楽しいかもしれない 重くなるけど */ package { import flash.display.Bitmap; import flash.display.BitmapData; import flash.display.Sprite; import flash.display.StageAlign; import flash.display.StageScaleMode; import flash.events.Event; import flash.events.MouseEvent; import frocessing.color.ColorHSV; public class Main extends Sprite { private const WHITE:uint =0xffffffff; private const SCALE:int = 5; private var w:int; private var h:int; private var bmd:BitmapData; private var bmp:Bitmap; private var pixels1:Vector.<uint> private var pixels2:Vector.<uint> private var colors:Vector.<uint> public function Main():void { stage.scaleMode = StageScaleMode.NO_SCALE; stage.align = StageAlign.TOP_LEFT; w = stage.stageWidth / SCALE; h = stage.stageHeight / SCALE; var color:ColorHSV = new ColorHSV(); colors = new Vector.<uint>(w * h, true); for (var i:int = 0; i < h; i++) { for (var j:int = 0; j < w; j++) { color.h = (i + j) * 4; colors[w * i + j] = color.value32; } } pixels2 = new Vector.<uint>(w * h, true); bmd = new BitmapData(w, h); bmp = new Bitmap(bmd); bmp.scaleX = bmp.scaleY = SCALE; addChild(bmp); setPixel(24, 0); setPixel(22, 1); setPixel(24, 1); setPixel(12, 2); setPixel(13, 2); setPixel(20, 2); setPixel(21, 2); setPixel(34, 2); setPixel(35, 2); setPixel(11, 3); setPixel(15, 3); setPixel(20, 3); setPixel(21, 3); setPixel(34, 3); setPixel(35, 3); setPixel(0, 4); setPixel(1, 4); setPixel(10, 4); setPixel(16, 4); setPixel(20, 4); setPixel(21, 4); setPixel(0, 5); setPixel(1, 5); setPixel(10, 5); setPixel(14, 5); setPixel(16, 5); setPixel(17, 5); setPixel(22, 5); setPixel(24, 5); setPixel(10, 6); setPixel(16, 6); setPixel(24, 6); setPixel(11, 7); setPixel(15, 7); setPixel(12, 8); setPixel(13, 8); addEventListener(Event.ENTER_FRAME, update); stage.addEventListener(MouseEvent.CLICK, onClick); } private function setPixel(x:int, y:int):void { var _x:int = x; var _y:int = y; if (x < 0) _x = w - 1; else if (x >= w) _x = 0; if (y < 0) _y = h - 1; else if (y >= h) _y = 0; bmd.setPixel32(_x, _y, colors[w * _y + _x]); } private function onClick(e:MouseEvent):void { var _x:int = mouseX / SCALE; var _y:int = mouseY / SCALE; setPixel(_x, _y-1); setPixel(_x+1, _y); setPixel(_x-1, _y+1); setPixel(_x, _y+1); setPixel(_x + 1, _y + 1); } private function update(e:Event):void { var n:int; var now:int; pixels1 = bmd.getVector(bmd.rect); for (var i:int = 0; i < h; i++) { for (var j:int = 0; j < w; j++) { now = w * i + j; n = checkAround(j, i); if (n == 3) pixels2[now] = colors[now]; else if (n == 2) pixels2[now] = pixels1[now]; else pixels2[now] = WHITE; } } bmd.setVector(bmd.rect, pixels2); } private function checkAround(x:int, y:int):int { var sum:int = 0; var _x:int; var _y:int; for (var i:int = -1; i <= 1; i++) { for (var j:int = -1; j <= 1; j++) { if (i == 0 && j == 0) continue; _x = x + j; _y = y + i; if (_x < 0) _x = w - 1; else if (_x >= w) _x = 0; if (_y < 0) _y = h - 1; else if (_y >= h) _y = 0; sum += pixels1[w * _y + _x] != WHITE ? 1 : 0; } } return sum; } } } ライフゲーム「グライダー銃」 Game of Life「Glider gun」