varcurrPos=9;
functionisWin(){
for(i=1; i<9; i++){
varnumTd=document.getElementById("numTd_"+i);
//var
numDiv = numTd.getElementsByTagName("div");
if(i!=numTd.innerText){
return false;
}
}
return true;
}
functionmove(event){
switch(event.keyCode){
case37:
//左键
if(currPos%3!=0){
varcurrTd=document.getElementById("numTd_"+currPos);
varnextTd=document.getElementById("numTd_"+(currPos+1));
vartemp=nextTd.innerHTML;
nextTd.innerHTML=" ";
currTd.innerHTML=temp;
currPos++;
}
break;
case38:
//上键
if(currPos<7){
varcurrTd=document.getElementById("numTd_"+currPos);
varnextTd=document.getElementById("numTd_"+(currPos+3));
vartemp=nextTd.innerHTML;
nextTd.innerHTML=" ";
currTd.innerHTML=temp;
currPos+=3;
}
break;
case39:
//右键
if(currPos%3!=1){
varcurrTd=document.getElementById("numTd_"+currPos);
varnextTd=document.getElementById("numTd_"+(currPos-1));
vartemp=nextTd.innerHTML;
nextTd.innerHTML=" ";
currTd.innerHTML=temp;
currPos--;
}
break;
case40:
//下键
if(currPos>3){
varcurrTd=document.getElementById("numTd_"+currPos);
varnextTd=document.getElementById("numTd_"+(currPos-3));
vartemp=nextTd.innerHTML;
nextTd.innerHTML=" ";
currTd.innerHTML=temp;
currPos-=3;
}
break;
default:
break;
if(isWin()){
alert("恭喜你,过关了!");
initNums();
}
}
}
functioninverNum(numArr){
varlen=numArr.length;
varcount=0;
for(i=0; i
for(j=i+1; j
j++){
if(numArr[j]>numArr[i]){
count++;
}
}
}
// alert("逆序数:"+count);
returncount;
}
functioninitNums(){
varnumArr=
newArray(1,2,3,4,5,6,7,8,9);
varnewNumArr=
newArray();
do{
vartempStr="";
for(iinnumArr){
varflag=true;
do{
tempNum=numArr[parseInt(Math.random()*100)%9];
if(tempStr.search(tempNum)== -1){
newNumArr[i]=tempNum;
tempStr+=tempNum;
flag=false;
}
}while(flag);
}
}while(inverNum(newNumArr)%2==0);
varlen=newNumArr.length;
for(j=0; j
if(newNumArr[j]!=9){
document.getElementById("numTd_"+(j+1)).innerHTML="
id=numDiv_"+newNumArr[j]+"class=numDiv>"+newNumArr[j]+"";
}else{
document.getElementById("numTd_"+(j+1)).innerHTML=" ";
currPos=j+1;
}
}
document.getElementById("numDiv").innerText=newNumArr;
}