Skip to content

Instantly share code, notes, and snippets.

@abarth500
Created June 16, 2012 08:41
Show Gist options
  • Save abarth500/2940546 to your computer and use it in GitHub Desktop.
Save abarth500/2940546 to your computer and use it in GitHub Desktop.
06/16のプログラム
<html>
<head>
<script type="text/javascript" src="ext-core.js"></script>
<script>
var Cell = function(){
this.status = false;
this.numAlive = 0;
}
Cell.prototype.getNext = function(){
if(this.status == true){
if(this.numAlive==2 || this.numAlive==3){
return true;
}else{
return false
}
}else{
if(this.numAlive==3){
return true;
}else{
return false;
}
}
}
var World = function(){
this.X = 10;
this.Y = 10;
this.cells = [];
for(var x = 0; x < this.X;x++){
this.cells.push([]);
for(var y=0; y<this.Y;y++){
this.cells[x][y] = new Cell();
}
}
for(var y=0; y<this.Y;y++){
var row = document.createElement("div");
row = new Ext.Element(row);
row.setHeight(20);
Ext.get("world").appendChild(row);
for(var x = 0; x < this.X;x++){
var cl = document.createElement("div");
cl.id = "cell_"+x+"_"+y;
cl.style.float = "left";
cl.style.border = "1px solid #f00";
cl.style.backgroundColor="#fff";
cl = new Ext.Element(cl);
cl.setHeight(20);
cl.setWidth(20);
cl.on("click",function(a,b,o){
this.setAlive(o["x"],o["y"],!this.cells[x][y].status);
},this,{"x":x,"y":y})
row.appendChild(cl);
}
}
}
World.prototype.goNext = function(){
var cls = [];
for(var x = 0; x < this.X;x++){
cls.push([]);
for(var y=0; y<this.Y;y++){
cls[x][y] = this.cells[x][y].getNext();
this.cells[x][y].numAlive=0;
}
}
for(var x = 0; x < this.X;x++){
for(var y=0; y<this.Y;y++){
this.setAlive(x,y,cls[x][y]);
}
}
}
World.prototype.setAlive = function(_x,_y,status){
for(var x = 0; x < this.X;x++){
for(var y=0; y<this.Y;y++){
if(x == _x && y == _y){
this.cells[x][y].status=status;
if(status){
Ext.get("cell_"+x+"_"+y).dom.style.backgroundColor="#00f";
}else{
Ext.get("cell_"+x+"_"+y).dom.style.backgroundColor="#fff";
}
}else if(Math.abs(x - _x) <= 1 && Math.abs(y - _y) <= 1){
if(status){
this.cells[x][y].numAlive++;
}
}
}
}
}
window.onload = function(){
var world = new World();
//world.setAlive(2,3,true);
//world.setAlive(3,3,true);
//world.setAlive(4,3,true);
Ext.get("btn").on("click",function(){
world.goNext();
})
//world.goNext();
}
</script>
</head>
<body>
<div id="world"></div>
<input type="button" id="btn">
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment