Code for a digital puzzle game written in JS [learning reference] _ javascript skills

Source: Internet
Author: User
I did nothing yesterday. I wrote a digital puzzle game in JS to entertain myself. Unfortunately, the reverse order number problem has not been solved, and sometimes it cannot be solved. Sorry. The Code is as follows:






Puzzles


Script
Var currPos = 9;
Function move (event ){
Switch (event. keyCode ){
Case 37:
// Left click
If (currPos % 3! = 0 ){
Var currTd = document. getElementById ("numTd _" + currPos );
Var nextTd = document. getElementById ("numTd _" + (currPos + 1 ));
Var temp = nextTd. innerHTML;
NextTd. innerHTML = "";
CurrTd. innerHTML = temp;
CurrPos ++;
}
Break;
Case 38:
// Press
If (currPos <7 ){
Var currTd = document. getElementById ("numTd _" + currPos );
Var nextTd = document. getElementById ("numTd _" + (currPos + 3 ));
Var temp = nextTd. innerHTML;
NextTd. innerHTML = "";
CurrTd. innerHTML = temp;
CurrPos + = 3;
}
Break;
Case 39:
// Right-click
If (currPos % 3! = 1 ){
Var currTd = document. getElementById ("numTd _" + currPos );
Var nextTd = document. getElementById ("numTd _" + (currPos-1 ));
Var temp = nextTd. innerHTML;
NextTd. innerHTML = "";
CurrTd. innerHTML = temp;
CurrPos --;
}
Break;
Case 40:
// Down key
If (currPos> 3 ){
Var currTd = document. getElementById ("numTd _" + currPos );
Var nextTd = document. getElementById ("numTd _" + (currPos-3 ));
Var temp = nextTd. innerHTML;
NextTd. innerHTML = "";
CurrTd. innerHTML = temp;
CurrPos-= 3;
}
Break;
Default:
Break;
}

If (isWin ()){
Alert ("congratulations, pass! ");
InitNums ();
}

}

Function isWin (){
For (I = 1; I <9; I ++ ){
Var numTd = document. getElementById ("numTd _" + I );
Var numDiv = numTd. getElementsByTagName ("p ");
If (I! = NumTd. innerText ){
Return false;
}
}
Return true;

}

Function initNums (){
Var numArr = new Array (1, 2, 3, 4, 5, 6, 7, 8, 9 );
Var newnewNumArr = new Array ();

Do {
Var tempStr = "";
For (I in numArr ){
Var flag = 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 );


Var len = newNumArr. length;
For (j = 0; j <len; j ++ ){
If (newNumArr [j]! = 9 ){
Document. getElementById ("numTd _" + (j + 1). innerHTML ="

"+ NewNumArr [j] +"

";
}
Else {
Document. getElementById ("numTd _" + (j + 1). innerHTML = "";
CurrPos = j + 1;
}
}

// Document. getElementById ("output"). innerText = newNumArr;
}

Function inverNum (numArr ){
Var len = numArr. length;
Var count = 0;
For (I = 0; I <len-1; I ++ ){
For (j = I + 1; j <len; j ++ ){
If (numArr [j]> numArr [I]) {
Count ++;
}
}
}
// Alert ("reverse order:" + count );
Return count;
}
Script





































Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.