Garden of Eden Pattern Sequencer
step,numAt,nextWhentという関数は「色々な言語でライフゲーム(http://metatoys.org/propella/lifeGame/)」から借用。
それ以外は自由に利用可能です。
//step,numAt,nextWhentという関数は「色々な言語でライフゲーム(http://metatoys.org/propella/lifeGame/)」から借用。
//それ以外は自由に利用可能です。
package {
import flash.display.Sprite;
import flash.display.Shape;
import flash.events.KeyboardEvent;
import flash.media.Sound;
import flash.events.SampleDataEvent;
public class GardenEden extends Sprite {
private var board : Array;
private var count : int = 0;
private var seq_count : int = 0;
private var state : int = 2;
private var cell:Shape;
public function GardenEden() {
set_init();
var buffer :Sound = new Sound();
buffer.addEventListener("sampleData", onSamplesCallback);
buffer.play();
cell = new Shape();
addChild(cell);
show(0);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
}
public function onSamplesCallback(event:SampleDataEvent):void {
for(var i:int = 0; i < 2048; i++) {
event.data.writeFloat(Number(board[(i + event.position)%9][count%33] * board[(i + event.position)%9][seq_count%33]));
event.data.writeFloat(Number(board[(i + event.position)%9][count%33] * board[(i + event.position)%9][seq_count%33]));
if ((i + event.position) % 9 == 0){ count = count + 1; }
if ((i + event.position) % (9*33*24) == (9*33*24)-1 ) {
seq_count = seq_count + 1;
if (state == 1) { step(); }
if (state == 2) { if (seq_count % 33 == 0) {step();}}
show(seq_count % 33);
}
}
}
public function set_init() : void {
board= [
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
[1,1,0,1,0,1,1,1,0,1,1,1,0,1,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1],
[1,0,1,0,1,1,1,0,1,1,1,0,1,1,1,1,0,1,1,1,0,1,0,1,0,1,0,1,0,1,0,1,0],
[1,1,1,1,1,0,1,1,1,0,1,1,1,0,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
[1,0,1,0,1,1,0,1,1,1,0,1,1,1,0,1,0,1,1,1,0,1,0,1,0,1,0,1,0,1,0,1,0],
[1,1,1,1,0,1,1,1,0,1,1,1,0,1,1,1,1,1,0,1,1,0,1,0,1,0,1,0,1,0,1,0,1],
[0,1,1,0,1,1,1,0,1,1,1,0,1,1,1,0,1,0,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1],
[1,1,0,1,1,0,1,1,1,0,1,1,1,0,1,1,0,1,1,1,1,0,1,0,1,0,1,0,1,0,1,0,1],
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1]];
}
public function show(num : int) : void {
cell.graphics.clear();
cell.graphics.beginFill(0xFF0000);
cell.graphics.drawRect(num*10, 0, 10, 10);
cell.graphics.endFill();
for( var x:int = 0; x<33; x++ ) {
for( var y:int = 0; y<9; y++ ) {
if( board[y][x] == 0 ) {
cell.graphics.beginFill(0xFFFFFF);
cell.graphics.lineStyle(1,0x333000);
cell.graphics.drawRect(x*10, y*10 + 10, 10, 10);
cell.graphics.endFill();
} else {
cell.graphics.beginFill(0x000000);
cell.graphics.lineStyle(1,0x333000);
cell.graphics.drawRect(x*10, y*10 + 10, 10, 10);
cell.graphics.endFill();
}
}
}
}
private function keyDownHandler(event:KeyboardEvent) : void{
if ( event.keyCode == 13 ) {
state = 1;
} else if ( event.keyCode == 32 ) {
state = 2;
} else {
state = 0;
set_init();
}
}
public function step() : void {
var newBoard : Array = board;
for(var x: int = 0; x<33; x++){
for(var y: int = 0; y<9; y++){
newBoard[y][x] = nextWhen(board[y][x], numAt(x, y));
}
}
board = newBoard;
}
public function numAt(x : int, y : int) : int {
var sum : int = 0;
var tempo : Array = [ [x - 1, y - 1], [x - 1, y], [x - 1, y + 1], [x, y - 1], [x, y + 1], [x + 1, y - 1], [x + 1, y], [x + 1, y + 1] ];
for (var i: int = 0; i<tempo.length; i++){
var _x : int = tempo[i][0];
var _y : int = tempo[i][1];
if (0 <= _x && _x < 33 && 0 <= _y && _y < 9 && board[_y][_x] == 1){
sum = sum + 1;
}else{
sum = sum + 0;
}
}
return sum;
}
public function nextWhen(cell : int, num : int) : int{
if (cell == 0){
return [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0][num];
} else {
return [0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0][num];
}
}
}
}