なんとな~く残しておこうと思う
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>Life Game Example</title>
<style type="text/css">
<!--
textarea {
width:700px;
height:700px;
font-size:50%
}
-->
</style>
<script type="text/javascript">
//<![CDATA[
var x_width;
var y_width;
var field = new Array(0);
var glider_gun = new Array(0);
glider_gun[0] = new Array("□","□","□","□","□","□","□","□","□","□","□","□","□","□","□","□","□","□","□","□","□","□","□","□","■","□","□","□","□","□","□","□","□","□","□","□");
glider_gun[1] = new Array("□","□","□","□","□","□","□","□","□","□","□","□","□","□","□","□","□","□","□","□","□","□","■","□","■","□","□","□","□","□","□","□","□","□","□","□");
glider_gun[2] = new Array("□","□","□","□","□","□","□","□","□","□","□","□","■","■","□","□","□","□","□","□","■","■","□","□","□","□","□","□","□","□","□","□","□","□","■","■");
glider_gun[3] = new Array("□","□","□","□","□","□","□","□","□","□","□","■","□","□","□","■","□","□","□","□","■","■","□","□","□","□","□","□","□","□","□","□","□","□","■","■");
glider_gun[4] = new Array("■","■","□","□","□","□","□","□","□","□","■","□","□","□","□","□","■","□","□","□","■","■","□","□","□","□","□","□","□","□","□","□","□","□","□","□");
glider_gun[5] = new Array("■","■","□","□","□","□","□","□","□","□","■","□","□","□","■","□","■","■","□","□","□","□","■","□","■","□","□","□","□","□","□","□","□","□","□","□");
glider_gun[6] = new Array("□","□","□","□","□","□","□","□","□","□","■","□","□","□","□","□","■","□","□","□","□","□","□","□","■","□","□","□","□","□","□","□","□","□","□","□");
glider_gun[7] = new Array("□","□","□","□","□","□","□","□","□","□","□","■","□","□","□","■","□","□","□","□","□","□","□","□","□","□","□","□","□","□","□","□","□","□","□","□");
glider_gun[8] = new Array("□","□","□","□","□","□","□","□","□","□","□","□","■","■","□","□","□","□","□","□","□","□","□","□","□","□","□","□","□","□","□","□","□","□","□","□");
function load_data(dat)
{
var i,j;
// 一回り大きなフィールドを作成し初期化する
for(i=0;i<=x_width+1;i++){
field[i] = new Array(0);
for(j=0;j<y_width+1;j++){
field[i][j] = "□";
}
}
for(i=0;i<dat.length;i++){
for(j=0;j<dat[i].length;j++){
field[i+1][j+1] = dat[i][j];
}
}
}
function init()
{
x_width = 70;
y_width = 70;
load_data(glider_gun);
setTimeout( "draw()", 100);
}
/**************************************************************************************/
/* 生死判定をおこなう */
/* 誕生 */
/* 死んでいるセルの周囲に 3つの生きているセルがあれば次の世代では生きる(誕生する) */
/* 維持 */
/* 生きているセルの周囲に2つか3つの生きているセルがあれば次の世代でも生き残る */
/* 死亡 */
/* 上以外の場合には次の世代では死ぬ */
/**************************************************************************************/
function dead_or_alive()
{
var i,j;
var field2 = new Array();
// field から field2 へコピーする
// 同時に field を□で初期化する
for(i=0;i<field.length;i++){
field2[i] = new Array(0);
for(j=0;j<field[i].length;j++){
field2[i][j] = field[i][j];
field[i][j] = "□";
}
}
// x_width, y_width の範囲で生死を判定し、結果をfieldへ入れる
var k,l;
for(i=0;i<x_width;i++){
for(j=0;j<y_width;j++){
// フィールドは一回り大きく作られているのでインデックスをずらした k と l を使う
k = i+1;
l = j+1;
var lifenum;
// k,l の周囲のセル数を数える
lifenum = (field2[k-1][l-1]=="■") + (field2[k+0][l-1]=="■") + (field2[k+1][l-1]=="■")
+ (field2[k-1][l+0]=="■") + (field2[k+1][l+0]=="■")
+ (field2[k-1][l+1]=="■") + (field2[k+0][l+1]=="■") + (field2[k+1][l+1]=="■");
if((field2[k][l]=="□") && (lifenum==3)){
field[k][l] = "■";
}
if((field2[k][l]=="■") &&( (lifenum==2) || (lifenum==3) )){
field[k][l] = "■";
}
}
}
}
function draw()
{
var i,j;
// 生死判定 結果はfield に反映されて帰ってきます
dead_or_alive();
// 表示します
var display = "";
for(i=0;i<x_width;i++){
for(j=0;j<y_width;j++){
display += field[i+1][j+1];
}
display += "\n";
}
document.fm1.disp.value = display;
setTimeout( "draw()", 100);
}
//]]>
</script>
</head>
<body onload="init()">
<form name = "fm1" width=100%>
<textarea name="disp" rows="70" cols="70"></textarea>
</form>
</body>
</html>
0 件のコメント:
コメントを投稿