日期:2014-05-16 浏览次数:20438 次
/** * @date 2009-08-05 * @author luoyouhua * */ var D = document; var map = { map_one:["OOOOOOOOOO","OWWWWWWOOO","OWOOGTWWOO","OWOOOBGWOO", "OWWWDWOWOO","WWOOOOOWOO","WOOBOOWWOO","WOOOWWWOOO","WWWWWOOOOO","OOOOOOOOOO"], map_two:["OOOOOOOOOO","OWWWWOOOOO","OWTOWOOOOO","OWOOWOOOOO","WWGOWWWWOO","WOBBGOGWOO","WOOBOWWWOO","WWWOOWOOOO","OOWWWWOOOO","OOOOOOOOOO"], map_three:["OOOOOOOOOO","OWWWWWOOOO","OWGOOWOOOO","OWOWOWWWOO", "OWODBOOWOO","OWOOBGOWOO","OWOOTWWWOO","OWWWWWOOOO","OOOOOOOOOO","OOOOOOOOOO"], map_four:["OOOOOOOOOO","OOOWWWWWOO","OOOWOOOWOO","OOOWOWGWOO", "OWWWOOGWOO","OWTOBBOWOO","OWOOGBOWOO","OWWWWWWWOO","OOOOOOOOOO","OOOOOOOOOO"], map_five:["OOOOOOOOOO", "OWWWWWWOOO","OWOOOTWOOO","OWOBWOWWWO", "OWODOBOOWO","OWOOOWWOWO","OWWGOOGOWO","OOWWOOOWWO","OOOWWWWWWO", "OOOOOOOOOO"] } function keyCode(evt){ var evt = window.event?window.event:evt; return window.event?event.keyCode:evt.which; } function each(arr,fn){ for(var i=0;i<arr.length;i++){ fn(arr[i],i); } } function changeCharAt(id,b){ var r = id.split("");var n = r[0]; r[0] = b?--n:++n; return topId = r.join(""); } function setClassName(div,n){ div.className = n; } function removeAll(t){ t.innerHTML=""; } function getClassName(p){ switch(p){ case "W"://墙壁 return "wall"; break; case "O": //通道 return "hall"; break; case "T"://推手 return "push"; break; case "B"://箱子 return "box"; break; case "G"://目标 return "target"; break; case "D"://到达目标 return "done"; break; } } function Game(t){ var T = this; T.target = t; T.currentDiv = null; T.amount = 0; T.targetAmount = 0; T.m = map.map_one; T.setMap = function(m){if(m) T.m = map[m];} T.initMap = function(){ removeAll(T.target); var f = document.createDocumentFragment(); each(T.m,function(e,i){ var arr = e.split(""); each(arr,function(p,j){ var div = D.createElement("DIV"); div.className=getClassName(p); T.initCell(div,p,i+""+j); f.appendChild(div); T.sets(p,div); }); }); T.target.appendChild(f); }; T.sets = function(p,div){ switch(p){ case "T"://推手 T.currentDiv = div; break; case "G"://目标 T.amount++ break; case "D"://到达目标 T.amount++ T.targetAmount++; break; } } /**=====================initialize div attrib===================*/ T.initCell = function(div,p,id){ div.id = id; T.intPass(div,p);//initialize pass attrib T.intIsTar(div,p); //initialize target attrib T.intIsEmpty(div,p); //initialize empty attrib }; T.intPass = function(div,p){ var pass = (p=="W")?"false":"true"; div.setAttribute("pass",pass) }; T.intIsTar = function(div,p){ var isTar = ((p=="G")||(p=="D"))?"true":"false"; div.setAttribute("isTar",isTar); }; T.intIsEmpty = function(div,p){ var isEmpty = (p=="O"||p=="G"||p=="T")?"true":"false"; div.setAttribute("isEmpty",isEmpty); }; /**============get top bottom previous next div========================*/ T.previousDiv = function(div){ return div.previousSibling; }; T.nextDiv = function(div){ return div.nextSibling; }; T.topDiv = function(div){ if(div){ var topId = changeCharAt(div.id,true); return D.getElementById(topId); } }; T.bottomDiv = function(div){ if(div){ var buttomId = changeCharAt(div.id,false); return D.getElementById(buttomId); } } /**==================set div type===========================*/ T.setBox = function(div){setClassName(div,"box");}; T.setPush = function(div){setClassName(div,"push");} T.setTarget = function(div){setClassName(div,"target");} T.setHall = function(div){setClassName(div,"hall");} T.setDone = function(div){setClassName(div,"done");} T.setEmptyDiv = function(div){ i