Ich schreibe jQuery Code, um ein Puzzle zu lösen. Eine 3x3-Tabelle enthält in jeder Zelle ein Bild (8 Bilder und 1 weißes Quadrat), aber sie können nur wechseln, wenn das angeklickte Bild an ein weißes/leeres Quadrat angrenzt. Mein Code arbeitet so weit, dass nur benachbarte Bilder verschoben werden können. Allerdings kann ich jedes Bild nur einmal austauschen. Wie schreibe ich es, damit ich jedes Bild beliebig oft austauschen kann?Wie schreibe ich meine jQuery, damit ich jedes Bild beliebig oft austauschen kann?
$(document).ready(function(event) {
$("img").click(function(){
var blankSquare = $("#blank");
var blankIndex = $("img").index(blankSquare);
//alert(blankIndex); //this updates as square moves
var imageSquare = $(this);
var imageIndex = $("img").index(this); //returns index of selected image
//alert(imageIndex); //will update as selected image changes
//swap image if it's index (position) is next adjacent to the white square
if(imageSquare !== blankSquare) {
if(imageIndex == 0 && blankIndex == 1) { //switch if image is at index 0
imageSquare.replaceWith(blankSquare);
imageSquare.detach().appendTo("td:empty");
} else if(imageIndex == 0 && blankIndex == 3) { //switch if image is at index 0
imageSquare.replaceWith(blankSquare);
imageSquare.detach().appendTo("td:empty");
} else if(imageIndex == 1 && blankIndex == 0) { //switch if image is at index 1
imageSquare.replaceWith(blankSquare);
imageSquare.detach().appendTo("td:empty");
} else if(imageIndex == 1 && blankIndex == 2) { //switch if image is at index 1
imageSquare.replaceWith(blankSquare);
imageSquare.detach().appendTo("td:empty");
} else if(imageIndex == 1 && blankIndex == 4) { //switch if image is at index 1
imageSquare.replaceWith(blankSquare);
imageSquare.detach().appendTo("td:empty");
imageClicked.data('clicked') == false;
} else if(imageIndex == 2 && blankIndex == 1) { //switch if image is at index 2
imageSquare.replaceWith(blankSquare);
imageSquare.detach().appendTo("td:empty");
} else if(imageIndex == 2 && blankIndex == 5) { //switch if image is at index 2
imageSquare.replaceWith(blankSquare);
imageSquare.detach().appendTo("td:empty");
} else if(imageIndex == 3 && blankIndex == 0) { //switch if image is at index 3
imageSquare.replaceWith(blankSquare);
imageSquare.detach().appendTo("td:empty");
} else if(imageIndex == 3 && blankIndex == 4) { //switch if image is at index 3
imageSquare.replaceWith(blankSquare);
imageSquare.detach().appendTo("td:empty");
} else if(imageIndex == 3 && blankIndex == 6) { //switch if image is at index 3
imageSquare.replaceWith(blankSquare);
imageSquare.detach().appendTo("td:empty");
} else if(imageIndex == 4 && blankIndex == 1) { //switch if image is at index 4
imageSquare.replaceWith(blankSquare);
imageSquare.detach().appendTo("td:empty");
} else if(imageIndex == 4 && blankIndex == 3) { //switch if image is at index 4
imageSquare.replaceWith(blankSquare);
imageSquare.detach().appendTo("td:empty");
} else if(imageIndex == 4 && blankIndex == 5) { //switch if image is at index 4
imageSquare.replaceWith(blankSquare);
imageSquare.detach().appendTo("td:empty");
} else if(imageIndex == 4 && blankIndex == 7) { //switch if image is at index 4
imageSquare.replaceWith(blankSquare);
imageSquare.detach().appendTo("td:empty");
} else if(imageIndex == 5 && blankIndex == 2) { //switch if image is at index 5
imageSquare.replaceWith(blankSquare);
imageSquare.detach().appendTo("td:empty");
} else if(imageIndex == 5 && blankIndex == 4) { //switch if image is at index 5
imageSquare.replaceWith(blankSquare);
imageSquare.detach().appendTo("td:empty");
} else if(imageIndex == 5 && blankIndex == 8) { //switch if image is at index 5
imageSquare.replaceWith(blankSquare);
imageSquare.detach().appendTo("td:empty");
} else if(imageIndex == 6 && blankIndex == 3) { //switch if image is at index 6
imageSquare.replaceWith(blankSquare);
imageSquare.appendTo("td:empty");
} else if(imageIndex == 6 && blankIndex == 7) { //switch if image is at index 6
imageSquare.replaceWith(blankSquare);
imageSquare.detach().appendTo("td:empty");
} else if(imageIndex == 7 && blankIndex == 4) { //switch if image is at index 7
imageSquare.replaceWith(blankSquare);
imageSquare.detach().appendTo("td:empty");
} else if(imageIndex == 7 && blankIndex == 6) { //switch if image is at index 7
imageSquare.replaceWith(blankSquare);
imageSquare.detach().appendTo("td:empty");
} else if(imageIndex == 7 && blankIndex == 8) { //switch if image is at index 7
imageSquare.replaceWith(blankSquare);
imageSquare.detach().appendTo("td:empty");
} else if(imageIndex == 8 && blankIndex == 5) { //switch if image is at index 8
imageSquare.replaceWith(blankSquare);
imageSquare.detach().appendTo("td:empty");
} else if(imageIndex == 8 && blankIndex == 7) { //switch if image is at index 8
imageSquare.replaceWith(blankSquare);
imageSquare.detach().appendTo("td:empty");
}
}//end of !== if-statement
});
});
HTML
<html lang="en">
<head>
<meta charset="utf-8">
<title>Tile Game</title>
<script src=" https://code.jquery.com/jquery-3.2.0.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="puzzle2.js" type="text/javascript"></script>
</head>
<body>
<table>
<tr>
<td><img src="SockeyOneRow3A.jpg" /></td>
<td><img src="SockeyOneRow2B.jpg" /></td>
<td><img src="SockeyOneRow3B.jpg" /></td>
</tr>
<tr>
<td><img src="SockeyOneRow2A.jpg" /></td>
<td><img id="blank" src="blank.jpg" /></td>
<td><img src="SockeyOneRow1B.jpg" /></td>
</tr>
<tr>
<td><img src="SockeyOneRow1A.jpg" /></td>
<td><img src="SockeyOneRow2C.jpg" /></td>
<td><img src="SockeyOneRow3C.jpg" /></td>
</tr>
</table>
</body>
</html>
Bitte senden Sie die HTML als auch, und wenn möglich eine Fiddle wäre großartig! – Varun
Ich habe den HTML-Code hinzugefügt. Ich bin mir nicht sicher, was Fiddle ist, tut mir leid. – user8812634