CopyCode The Code is as follows: <HTML>
<Head>
<Title> puzzle </title>
<Style>
TD. numtd {
Width: 20px;
Height: 20px;
}
Div. numdiv {
Width: 100%;
Height: 100%;
Background-color: #000;
Color: # FFF;
Text-align: center;
Vertical-align: middle;
}
</Style>
<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 ("Div ");
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 = "<Div id = 'numdiv _" + newnumarr [J] + "'class = 'numdiv '>" + newnumarr [J] + "</div> ";
}
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>
</Head>
<Body onkeyup = "move (event);" onLoad = "initnums ();">
<Table align = "center">
<Tr>
<TD id = "numtd_1" class = "numtd">
</TD>
<TD id = "numtd_2" class = "numtd">
</TD>
<TD id = "numtd_3" class = "numtd">
</TD>
</Tr>
<Tr>
<TD id = "numtd_4" class = "numtd">
</TD>
<TD id = "numtd_5" class = "numtd">
</TD>
<TD id = "numtd_6" class = "numtd">
</TD>
</Tr>
<Tr>
<TD id = "numtd_7" class = "numtd">
</TD>
<TD id = "numtd_8" class = "numtd">
</TD>
<TD id = "numtd_9" class = "numtd">
</TD>
</Tr>
</Table>
<Table>
<Tr>
<TD id = "output"> </TD>
</Tr>
</Table>
</Body>
</Html>