<頭>
拼图 td.numTd{
幅 : 20px ;
高さ: 20px ;
}
div.numDiv{
幅 : 100% ;
高さ: 100% ;
背景色: #000 ;
色: #FFF ;
テキスト整列 : 中央;
vertical-align : 中央;
}
<スクリプト>
var currPos = 9;
function move(event){
switch(event.keyCode){
case 37 :
// 左键
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;
現在の位置 ;
}
休憩;
ケース 38 :
// 上键
if (currPos var currTd = document.getElementById("numTd_" currPos);
var nextTd = document.getElementById("numTd_" (currPos 3));
var temp = nextTd.innerHTML;
nextTd.innerHTML = " ";
currTd.innerHTML = temp;
currPos = 3;
}
休憩;
case 39 :
// 右键
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 -- ;
}
休憩;
case 40 :
// 下键
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;
}
休憩;
デフォルト:
ブレーク;
}
if (isWin()){
alert("恭喜你,过关了!");
initNums();
}
}
function isWin(){
for (i = 1; i var numTd = document.getElementById(" numTd_" i);
var numDiv = numTd.getElementsByTagName("div");
if (i != numTd.innerText){
return false;
}
}
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;
フラグ = false;
}
}
while(フラグ);
}
}while(inverNum(newNumArr) % 2 == 0);
var len = newNumArr.length;
for(j = 0; 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;
}
関数 inverNum(numArr){
var len = numArr.length;
変数カウント = 0;
for(i = 0; i for(j = i 1; j if (numArr[j] > numArr[ i]){
カウント ;
}
}
}
//alert("逆順数: " count);
戻り数;
}
<テーブル align="center">
|
|
|
|
|
|
|
|
|
|