走迷宫

来源:互联网 发布:python导航 中英文 编辑:程序博客网 时间:2024/05/01 09:25


<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<title>走迷宫
</title>
<style>
body {
 background-Color:#FFF;
 overflow:auto;
 font-size:12px;
}

table {
 font-size:12px;
}
</style>
<script>
//作者Hutia. 转贴请保留,谢谢

var maps = {
 mx : 40, //宽度
 my : 40, //高度
 cx : 5, //显示宽度
 cy : 5, //显示高度
 sx : 0, //显示位置左上角x坐标
 sy : 0, //显示位置左上角y坐标
 px : 0, //指针x坐标
 py : 0, //指针y坐标
 rate : 0.65, //迷宫开放路口比率
 sRate : 1.5, //迷宫曲折率
 
 
 cells : new Array(),
 
 oP : null,
 oE : null,
 element : null,
 
 Cell :
 function(x,y){
  return(this.cells[y*this.mx+x]);
 },
 
 doRand :
 function(){
  for(var i=0;i<this.cells.length;i++){
   this.cells[i].setTop(Math.random()>this.rate);
   this.cells[i].setBottom(Math.random()>this.rate);
   this.cells[i].setLeft(Math.random()>this.rate);
   this.cells[i].setRight(Math.random()>this.rate);
  }
  x1=0;y1=0;
  for(var i=0;i<parseInt(this.mx/this.sRate);i++){
   if(i<parseInt(this.mx/3)-1){
    x2=parseInt(Math.random()*this.mx);
    y2=parseInt(Math.random()*this.my);
   }else{
    x2=this.mx-1;
    y2=this.my-1;
   }
   
   stepX=x1>x2?-1:1;
   stepY=y1>y2?-1:1;
   if(i%2==0){
    if(x1!=x2){
     for(var x=x1;x!=x2;x+=stepX){
      if(stepX>0){
       this.Cell(x,y1).setRight(true);
      }else{
       this.Cell(x,y1).setLeft(true);
      }
     }
    }
    if(y1!=y2){
     for(var y=y1;y!=y2;y+=stepY){
      if(stepY>0){
       this.Cell(x2,y).setBottom(true);
      }else{
       this.Cell(x2,y).setTop(true);
      }
     }
    }
   }else{
    if(y1!=y2){
     for(var y=y1;y!=y2;y+=stepY){
      if(stepY>0){
       this.Cell(x2,y).setBottom(true);
      }else{
       this.Cell(x2,y).setTop(true);
      }
     }
    }
    if(x1!=x2){
     for(var x=x1;x!=x2;x+=stepX){
      if(stepX>0){
       this.Cell(x,y1).setRight(true);
      }else{
       this.Cell(x,y1).setLeft(true);
      }
     }
    }
   }
   
   x1=x2;y1=y2;
  }
 },
 
 element_onkeydown :
 function(){
  with(this.parentObject){
   switch(HTML.keyChar()){
    case "up":
     if(py>0&&Cell(px,py).iTop){
      py--;
      if(py<sy||(py-sy)*2+1<cy){
       sy--;
       if(sy<0){
        sy=0;
       }
      }
      this.parentObject.refreshElement();
     }
    break;
    case "down":
     if(py<my-1&&Cell(px,py).iBottom){
      py++;
      if(py>cy-1||(py-sy)*2>cy){
       sy++;
       if(sy>my-cy){
        sy=my-cy;
       }
      }
      this.parentObject.refreshElement();
     }
    break;
    case "left":
     if(px>0&&Cell(px,py).iLeft){
      px--;
      if(px<sx||(px-sx)*2+1<cx){
       sx--;
       if(sx<0){
        sx=0;
       }
      }
      this.parentObject.refreshElement();
     }
    break;
    case "right":
     if(px<mx-1&&Cell(px,py).iRight){
      px++;
      if(px>cx-1||(px-sx)*2>cx){
       sx++;
       if(sx>mx-cx){
        sx=mx-cx;
       }
      }
      this.parentObject.refreshElement();
     }
    break;
   }
  }
 },
 
 init :
 function(){
  this.cells=new Array();
  for(var x=0;x<this.mx;x++){
   for(var y=0;y<this.my;y++){
    this.cells[y*this.mx+x]=new _node(x,y,this.mx,this.my);
   }
  }
  this.linkCells();
  this.doRand();
  this.initElement();
 },
 
 initElement :
 function(){
  if(this.element){
   this.element.outerHTML="";
  }
  this.element=document.createElement("Table");
  this.element.style.border="#FFF 0px solid";
  this.element.cellSpacing=0;
  this.element.cellPadding=0;
  for(var y=0;y<this.cy;y++){
   this.element.insertRow();
   for(var x=0;x<this.cx;x++){
    theCell=this.element.rows[y].insertCell();
    theCell.innerText=" ";
    with(theCell.style){
     width=40;
     height=40;
     paddingLeft=5;
     overflow="hidden";
    }
    this.refreshElementCell(this.sx+x,this.sy+y);
   }
  }
  document.body.insertBefore(this.element);
  this.element.parentObject=this;
  this.element.onkeydown=maps.element_onkeydown;
  
  this.oP=document.createElement("IMG");
  this.oP.src="http://pic1.blueidea.com/bbs/smile.gif";
  this.element.rows[0].cells[0].insertBefore(this.oP);
  
  this.element.focus();
 },
 
 linkCells :
 function(){
  for(var i=0;i<this.cells.length;i++){
   this.cells[i].oTop=this.cells[i].topIndex==null?null:this.cells[this.cells[i].topIndex];
   this.cells[i].oBottom=this.cells[i].bottomIndex==null?null:this.cells[this.cells[i].bottomIndex];
   this.cells[i].oLeft=this.cells[i].leftIndex==null?null:this.cells[this.cells[i].leftIndex];
   this.cells[i].oRight=this.cells[i].rightIndex==null?null:this.cells[this.cells[i].rightIndex];
  }
 },
 
 refreshElement :
 function(){
  for(var x=0;x<this.cx;x++){
   for(var y=0;y<this.cy;y++){
    this.refreshElementCell(x,y);
   }
  }
  this.element.rows[this.py-this.sy].cells[this.px-this.sx].innerHTML="";
  this.element.rows[this.py-this.sy].cells[this.px-this.sx].innerText=" ";
  this.element.rows[this.py-this.sy].cells[this.px-this.sx].insertBefore(this.oP);
  if(this.px==this.mx-1&&this.py==this.my-1){
   alert("恭喜你,通过了");
  }
 },
  
 refreshElementCell :
 function(x,y){
  theCell=this.element.rows[y].cells[x];
  x+=this.sx;y+=this.sy;
  if(x!=this.mx&&y!=this.my){theCell.innerHTML="";theCell.innerText=" ";}
  with(theCell.style){
   if(x>this.mx||y>this.my||x<0||y<0){
    border="#FFF 1px solid";
   }else{
    borderTop=(this.Cell(x,y).iTop?"#FFF":"#000")+" 1px solid";
    borderBottom=(this.Cell(x,y).iBottom?"#FFF":"#000")+" 1px solid";
    borderLeft=(this.Cell(x,y).iLeft?"#FFF":"#000")+" 1px solid";
    borderRight=(this.Cell(x,y).iRight?"#FFF":"#000")+" 1px solid";
   }
  }
  if(x==this.mx-1&&y==this.my-1&&(this.px!=x||this.py!=y)){
   theCell.innerHTML="";
   this.oE=document.createElement("IMG");
   this.oE.src="http://pic1.blueidea.com/bbs/biggrin.gif";
   theCell.insertBefore(this.oE);
  }
 }

}

function init(){
 maps.init();
}

function _node(x,y,mx,my){
 this.x=x;
 this.y=y;
 this.mx=mx;
 this.my=my;
 this.index=y*mx+x;
 
 this.topIndex=y<1?null:(y-1)*mx+x;
 this.bottomIndex=y>my-1?null:(y+1)*mx+x;
 this.leftIndex=x<1?null:y*mx+x-1;
 this.rightIndex=x>mx-1?null:y*mx+x+1;
 
 this.iTop=false;
 this.iBottom=false;
 this.iLeft=false;
 this.iRight=false;
 
 this.oTop=null;
 this.oBottom=null;
 this.oLeft=null;
 this.oRight=null;
 
 this.setTop=_node_setTop;
 this.setBottom=_node_setBottom;
 this.setLeft=_node_setLeft;
 this.setRight=_node_setRight;
}

function _node_setTop(flag){
 if(this.oTop&&this.y>0){
  this.iTop=flag;
  this.oTop.iBottom=flag;
 }
}

function _node_setBottom(flag){
 if(this.oBottom&&this.y<this.my-1){
  this.iBottom=flag;
  this.oBottom.iTop=flag;
 }
}

function _node_setLeft(flag){
 if(this.oLeft&&this.x>0){
  this.iLeft=flag;
  this.oLeft.iRight=flag;
 }
}

function _node_setRight(flag){
 if(this.oRight&&this.x<this.mx-1){
  this.iRight=flag;
  this.oRight.iLeft=flag;
 }
}

var HTML = {
 keyChar:
  function(){
   if(event.shiftKey){
    switch(event.keyCode.toString()){
     case "96":return("0");
     case "97":return("1");
     case "98":return("2");
     case "99":return("3");
     case "100":return("4");
     case "101":return("5");
     case "102":return("6");
     case "103":return("7");
     case "104":return("8");
     case "105":return("9");
     case "111":return("/");
     case "110":return(".");
     case "106":return("*");
     case "109":return("-");
     case "107":return("+");
     case "144":return("numlock");
     case "192":return("~");
     case "49":return("!");
     case "50":return("@");
     case "51":return("#");
     case "52":return("$");
     case "53":return("%");
     case "54":return("^");
     case "55":return("&");
     case "56":return("*");
     case "57":return("(");
     case "48":return(")");
     case "189":return("_");
     case "187":return("+");
     case "219":return("{");
     case "221":return("}");
     case "220":return("|");
     case "20":return("caplock");
     case "186":return(":");
     case "222":return("\"");
     case "188":return("<");
     case "190":return(">");
     case "191":return("?");
     case "8":return("backspace");
     case "9":return("tab");
     case "81":return("Q");
     case "87":return("W");
     case "69":return("E");
     case "82":return("R");
     case "84":return("T");
     case "89":return("Y");
     case "85":return("U");
     case "73":return("I");
     case "79":return("O");
     case "80":return("P");
     case "65":return("A");
     case "83":return("S");
     case "68":return("D");
     case "70":return("F");
     case "71":return("G");
     case "72":return("H");
     case "74":return("J");
     case "75":return("K");
     case "76":return("L");
     case "16":return("shift");
     case "90":return("Z");
     case "88":return("X");
     case "67":return("C");
     case "86":return("V");
     case "66":return("B");
     case "78":return("N");
     case "77":return("M");
     case "17":return("ctrl");
     case "91":return("win");
     case "92":return("win");
     case "18":return("alt");
     case "32":return(" ");
     case "93":return("menu");
     case "38":return("up");
     case "40":return("down");
     case "37":return("left");
     case "39":return("right");
     case "45":return("insert");
     case "46":return("delete");
     case "36":return("home");
     case "35":return("end");
     case "33":return("pageup");
     case "34":return("pagedown");
     case "34":return("printcreen");
     case "145":return("scrolllock");
     case "19":return("pause");
     case "27":return("esc");
     case "12":return("middle");
     case "13":return("\r\n");
     case "112":return("f1");
     case "113":return("f2");
     case "114":return("f3");
     case "115":return("f4");
     case "116":return("f5");
     case "117":return("f6");
     case "118":return("f7");
     case "119":return("f8");
     case "120":return("f9");
     case "121":return("f10");
     case "122":return("f11");
     case "123":return("f12");
     default:return("unknown");
    }
   }else{
    switch(event.keyCode.toString()){
     case "96":return("0");
     case "97":return("1");
     case "98":return("2");
     case "99":return("3");
     case "100":return("4");
     case "101":return("5");
     case "102":return("6");
     case "103":return("7");
     case "104":return("8");
     case "105":return("9");
     case "111":return("/");
     case "110":return(".");
     case "106":return("*");
     case "109":return("-");
     case "107":return("+");
     case "144":return("numlock");
     case "192":return("`");
     case "49":return("1");
     case "50":return("2");
     case "51":return("3");
     case "52":return("4");
     case "53":return("5");
     case "54":return("6");
     case "55":return("7");
     case "56":return("8");
     case "57":return("9");
     case "48":return("0");
     case "189":return("-");
     case "187":return("=");
     case "8":return("backspace");
     case "9":return("tab");
     case "81":return("q");
     case "87":return("w");
     case "69":return("e");
     case "82":return("r");
     case "84":return("t");
     case "89":return("y");
     case "85":return("u");
     case "73":return("i");
     case "79":return("o");
     case "80":return("p");
     case "219":return("[");
     case "221":return("]");
     case "220":return("\\");
     case "20":return("caplock");
     case "65":return("a");
     case "83":return("s");
     case "68":return("d");
     case "70":return("f");
     case "71":return("g");
     case "72":return("h");
     case "74":return("j");
     case "75":return("k");
     case "76":return("l");
     case "186":return(";");
     case "222":return("'");
     case "16":return("shift");
     case "90":return("z");
     case "88":return("x");
     case "67":return("c");
     case "86":return("v");
     case "66":return("b");
     case "78":return("n");
     case "77":return("m");
     case "188":return(",");
     case "190":return(".");
     case "191":return("/");
     case "17":return("ctrl");
     case "91":return("win");
     case "92":return("win");
     case "18":return("alt");
     case "32":return(" ");
     case "93":return("menu");
     case "38":return("up");
     case "40":return("down");
     case "37":return("left");
     case "39":return("right");
     case "45":return("insert");
     case "46":return("delete");
     case "36":return("home");
     case "35":return("end");
     case "33":return("pageup");
     case "34":return("pagedown");
     case "34":return("printcreen");
     case "145":return("scrolllock");
     case "19":return("pause");
     case "27":return("esc");
     case "12":return("middle");
     case "13":return("\r\n");
     case "112":return("f1");
     case "113":return("f2");
     case "114":return("f3");
     case "115":return("f4");
     case "116":return("f5");
     case "117":return("f6");
     case "118":return("f7");
     case "119":return("f8");
     case "120":return("f9");
     case "121":return("f10");
     case "122":return("f11");
     case "123":return("f12");
     default:return("unknown");
    }
   }
  }
}
</script>

</head>

<body onload="init();" onunload="APP.save();">

</body>
</html>