【求助】js的一个生物圈小程序 跑不起来 实在不知道为什么

rzk 发布于 2016/05/29 14:34
阅读 138
收藏 0
感觉在一个循环里面出不来 不知道为什么
<script type="text/javascript">
var thePlan=
["############################", 
"# o                                                          #",
"#               ##                         # o         ##",
"#                                                              #",
"#                     o           ######             #",
"#      o                                                     #",
"#                 ###                                     #",
"#                   ##          o                      ##",
"#                                                            #",
"#       #                o             ##               #",
"#                                                            #",
"#             #  #                                     o #",
"############################"]; //图形模拟 O是虫子 #是墙
function Point(x,y){
this.x=x;
this.y=y;
} //空间坐标
Point.prototype.add=function(other){ 
return new Point(this.x+other.x,this.y+other.y);
};
Point.prototype.toString=function (){
return "("+this.x+","+this.y+")";
}
function Grid(width,height){
this.width=width;
this.height=height;
this.cells=new Array(width*height);
}; //创建一个网格,储存内容
Grid.prototype.valueAt=function(point){
return this.cells[point.y*this.width+point.x];};
Grid.prototype.setValueAt=function(point,value){
return this.cells[point.y*this.width+point.x]=value;
};
Grid.prototype.isInside=function(point){
return point.x>=0 && point.y>=0 &&
point.x< this.width && point.y<this.height;
};
Grid.prototype.moveValue=function(from,to){
return this.setValueAt(to,valueAt(from));
this.setValueAt(from,undefined);
};
Grid.prototype.each=function(action){
for(var y=0;this.y>y;y++){
for(var x=0;this.x>x;x++){
var point=new Point(x,y);
action(point,this.valueAt(point)); }
}
}; //遍历网格
function froEachIn(object,action){
for(var property in object){
if(Object.prototype.hasOwnProperty.call(Object,property))
action(property,object[property]);
}


}
function Dictionary(startvalues){ 
this.value= startvalues || "";
}; //储存虫子运动方向的词典
Dictionary.prototype.lookup=function(name){
return this.values[name];
};
Dictionary.prototype.store=function(name,value){
this.values[name]=value;
};
Dictionary.prototype.contains=function(name){
return Object.prototype.propertyIsEnumerable.call(this.values,name);
};
Dictionary.prototype.each=function(action){
forEachIn(this.values,action);
}
var directions=new Dictionary(

{ "ne":new Point(1,-1),
"n": new Point(0,-1),
"s": new Point(0,1),
"se":new Point(1,1),
"sw":new Point(-1,1),
"nw":new Point(-1,-1),
"w": new Point(-1,0),
"e": new Point(1,0)
});
function StupidBug(){ };
StupidBug.prototype.act=function(surroundings){
return {type:"move",direction:"s"};
};
StupidBug.prototype.character="o"; //创建一个向南走的虫子


var wall={};
wall.character="#"; //墙
function elementFromCharacter(character){ var i=1;document.write(i++);
if(character=="")
{return undefined;}
else if(character=="#")
{return wall;}
else if(character=="o")
{return new StupidBug;}
}; //将字符转换为储存至网格中的值
function characterFromElement(element){
if(element==undefined)
return "";
else
return element.character;




};


function Terrarium(plan){ alert("plan");
var grid =new Grid(plan[0].length,plan.length);
for(var y=0;y<plan.length;y++){
var line=plan[y];
for(var x=0;x<line.length;x++){
grid.setValueAt(new Point(x,y),elementFromCharacter(line.charAt(x))
);
}
}
this.grid=grid;
}; //生态圈
Terrarium.prototype.toString=function(){ alert("tostring");
var characters=[];
var endOfLine=this.grid.width-1;
this.grid.each(function(point,value){
characters.push(characterFromElement(value));
if (point.x==endOfLine)
characters.push("\n");
});

return characters.join("");
}
function bind(func,object){
return function(){
return func.apply(object,arguments);
};
} //将内部函数bind到this 它与外部函数有一样的THIS对象
Terrarium.prototype.listActingCreatures = function(){ 
var found=[];
this.grid.each(function(point,value){
if(value != undefined &&value.act)
found.push({object:value,point:point});
}); //将虫子储存在数组里 以便操作
return found;
};
Terrarium.prototype.listSurroundings= function(center){
var result ={};
var grid =this.grid;
directions.each(function(name,direction){
var place=center.add(direction);
if(grid.isInside(place))
result[name] =characterFromElement(grid.valueAt(place));
else
result[name]="#";
}); //判断虫子附近有没有墙
return result;
};
Terrarium.prototype.processCreature=function(creature,point){
var action =creature.act(this.listSurroundings(point));
if(action.type="move" && directiona.contains(action.direction)){
var to=point.add(directions.lookup(action.direction));
if(this.grid.isInside(to) && this.grid.valueAt(to)==undefined)
this.grid.moveValue(point,to);


}
else{
throw new Error("Unsupported action:"+ action.type);
}
}; //判断虫子周围有没有空地 让虫子动
function forEach(array,action){
for(var i=0;i<array.length;i++)
action(array[i]);
} 
Terrarium.prototype.step=function(){
forEach(this.listActingCreatures(),bind(this.processCreature,this));
} //这里让小虫子动一步
var terrarium =new Terrarium(thePlan);
terrarium.step();
document.write(terrarium); // 输出不了 求解答!
</script>



加载中
返回顶部
顶部