swimbird’s blog
wordpress移行テスト
- 2010-10-23 (土)
- wordpress
自宅サーバで稼動していたwordpressをcoreserverに移行してみたのでテスト。
- Comments: 0
- Trackbacks: 0
javascriptでライフゲーム
- 2009-11-30 (月)
- javascript
順列都市を読んで
作中にやたらと「エデンの園配置」というキーワードがでていて、ふと気になってwikipediaで調べてみたら
エデンの園配置(英: Garden of Eden pattern)とは、セル・オートマトンにおいて他のいかなる配置からも到達できない配置を指す。以前の状態が存在しない、つまり最初からそのように配置しない限り出現しないということから、聖書のエデンの園にちなんで命名された。
Moore (1962) によれば、1950年代にジョン・テューキーが命名したもので、これはジョン・ホートン・コンウェイがライフゲームを発明するずっと前のことである。
今度はライフゲームというワードが気になり調べてみると、
単純なアルゴリズムで生命の誕生、進化、淘汰などのプロセスを簡易的なモデルで再現できる
ものらしいということなので、jsで書いてみた。
実行(IE以外)
一日中みてても飽きないわー。
(function() {
Element = {
remove: function(element) {
element.parentNode.removeChild(element);
},
create: function(tagName, options) {
options = options || {};
var element = document.createElement(tagName);
var parent;
if(options.parent) {
parent = options.parent;
delete options.parent;
}
if(options.style) {
for(var p in options.style)
element.style[p] = options.style[p];
delete options.style;
}
for(var p in options) element[p] = options[p];
if(parent) parent.appendChild(element);
return element;
}
};
//GardenOfEden
var GOD = {
GardenX : 120,
GardenY : 120,
Garden : [],
C : [],
_C : [],
Timer : 500,
init : function(){
this.CreateGarden();
this.BeginningTimer();
},
CreateGarden : function(){
var n = 0;
for (var y = 0; y <= this.GardenY - 1; y++){
for(var x = 0; x <= this.GardenX - 1; x++, n++){
if(this.Shake()){
var Gtype = "birth";
var GColor = this.Image.birth;
this.C[n] = 1;
}else{
var Gtype = "death";
var GColor = this.Image.death;
this.C[n] = 0;
}
this.Garden[n] = Element.create('div', {
parent: document.body,
type : Gtype,
id : n,
style : {
position : 'absolute',
zIndex : 90000,
backgroundColor: GColor,
width: this.Image.width,
height: this.Image.height,
top: (y * this.Image.width)+'px',
left: (x * this.Image.height)+'px'
}
});
this.Garden[n].addEventListener('mousedown', function(event) {
if(this.type === "birth"){
this.style.backgroundColor = GOD.Image.death;
this.type = "death";
GOD.C[this.id] = 0;
}else if(this.type === "death"){
this.style.backgroundColor = GOD.Image.birth;
this.type = "birth";
GOD.C[this.id] = 1;
}
}, false);
}
}
},
Shake : function(){
var shake = 10; //persent
return (Math.floor((Math.random()*100)) < shake) ? true : false;
},
Beginning : function(){
var n;
this.NextDay();
for (n = 0; n < this.GardenY * this.GardenX; n++) this.C[n] = this._C[n];
},
BeginningTimer : function(){
this.t = setInterval(this._bindFunc(this, this.Beginning), this.Timer);
},
NextDay : function(){
var n = 0;
for (var y = 0; y < this.GardenY; y++) {
for (var x = 0; x < this.GardenX; x++, n++) {
this._C[n] = this.Judgment(x, y, n);
if (this.C[n] != this._C[n]) this.DeathOrBirth(n, this._C[n]);
}
}
},
Judgment : function(x, y, n){
var i, j, l = 0, _x, _y;
if ((x > 0 && x < this.GardenX - 1) && (y > 0 && y < this.GardenY - 1)) {
l = this.C[n - this.GardenX - 1] + this.C[n - this.GardenX] + this.C[n - this.GardenX + 1]
+ this.C[n - 1] + this.C[n + 1]
+ this.C[n + this.GardenX - 1] + this.C[n + this.GardenX] + this.C[n + this.GardenX + 1]
;
} else {
for (i = -1; i < 2; i++) {
for (j = -1; j < 2; j++) {
if (i == 0 && j == 0) continue;
if (x + j == -1) _x = this.GardenX - 1;
if (x + j == this.GardenX) _x = 0;
if (x + j != -1 && x + j != this.GardenX) _x = x + j;
if (y + i == -1) _y = this.GardenY - 1;
if (y + i == this.GardenY) _y = 0;
if (y + i != -1 && y + i != this.GardenY) _y = y + i;
l += this.C[this.GardenX * _y + _x];
}
}
}
if (this.C[n] == 0 && l == 3) return 1;
if (this.C[n] == 1 && (l == 2 || l == 3)) return 1;
return 0;
},
DeathOrBirth : function(n, p){
this.Garden[n].style.backgroundColor = (p == 0) ? this.Image.death : this.Image.birth;
},
Image : {
width : 5,
height : 5,
death : "#ffffff",
birth : "#330033"
},
_bindFunc : function (bind, func){
return function(){
return func.apply(bind, arguments);
};
}
};
GOD.init();
})();
- Comments: 0
- Trackbacks: 0
- Search
- Feeds
-