Changed around line 1
+ let currentImage = null;
+ let tiles = Array.from(document.querySelectorAll('.tile'));
+ let emptyTileIndex = 8;
+ let isShuffling = false;
+
+ document.getElementById('imageInput').addEventListener('change', handleImageUpload);
+ document.getElementById('shuffleButton').addEventListener('click', shufflePuzzle);
+ document.getElementById('showOriginal').addEventListener('mousedown', showOriginal);
+ document.getElementById('showOriginal').addEventListener('mouseup', hideOriginal);
+ document.getElementById('showOriginal').addEventListener('mouseleave', hideOriginal);
+
+ tiles.forEach(tile => {
+ tile.addEventListener('click', () => {
+ if (!isShuffling && currentImage) {
+ moveTile(parseInt(tile.dataset.index));
+ }
+ });
+ });
+
+ function handleImageUpload(e) {
+ const file = e.target.files[0];
+ if (file) {
+ const reader = new FileReader();
+ reader.onload = function(event) {
+ currentImage = event.target.result;
+ setupPuzzle();
+ shufflePuzzle();
+ };
+ reader.readAsDataURL(file);
+ }
+ }
+
+ function setupPuzzle() {
+ tiles.forEach((tile, index) => {
+ if (index !== 8) {
+ const bgPosX = (index % 3) * 50;
+ const bgPosY = Math.floor(index / 3) * 50;
+ tile.style.backgroundImage = `url(${currentImage})`;
+ tile.style.backgroundPosition = `-${bgPosX}% -${bgPosY}%`;
+ }
+ });
+ }
+
+ function moveTile(index) {
+ if (isValidMove(index)) {
+ const clickedTile = tiles[index];
+ const emptyTile = tiles[emptyTileIndex];
+
+ // Swap background styles
+ const tempBg = clickedTile.style.backgroundImage;
+ const tempPos = clickedTile.style.backgroundPosition;
+
+ clickedTile.style.backgroundImage = '';
+ clickedTile.style.backgroundPosition = '';
+ emptyTile.style.backgroundImage = tempBg;
+ emptyTile.style.backgroundPosition = tempPos;
+
+ // Swap classes
+ clickedTile.classList.add('empty');
+ emptyTile.classList.remove('empty');
+
+ // Update empty tile index
+ emptyTileIndex = index;
+
+ checkWin();
+ }
+ }
+
+ function isValidMove(index) {
+ const row = Math.floor(index / 3);
+ const col = index % 3;
+ const emptyRow = Math.floor(emptyTileIndex / 3);
+ const emptyCol = emptyTileIndex % 3;
+
+ return (Math.abs(row - emptyRow) + Math.abs(col - emptyCol)) === 1;
+ }
+
+ function shufflePuzzle() {
+ if (!currentImage) return;
+
+ isShuffling = true;
+ let moves = 100;
+ let lastMove = -1;
+
+ function makeRandomMove() {
+ const possibleMoves = [];
+ tiles.forEach((tile, index) => {
+ if (isValidMove(index) && index !== lastMove) {
+ possibleMoves.push(index);
+ }
+ });
+
+ const moveIndex = possibleMoves[Math.floor(Math.random() * possibleMoves.length)];
+ lastMove = emptyTileIndex;
+ moveTile(moveIndex);
+ }
+
+ const shuffleInterval = setInterval(() => {
+ makeRandomMove();
+ moves--;
+ if (moves <= 0) {
+ clearInterval(shuffleInterval);
+ isShuffling = false;
+ }
+ }, 10);
+ }
+
+ function checkWin() {
+ const currentOrder = tiles.map(tile => tile.style.backgroundPosition);
+ const isWin = currentOrder.every((pos, index) => {
+ if (index === 8) return !pos;
+ const correctBgPosX = (index % 3) * 50;
+ const correctBgPosY = Math.floor(index / 3) * 50;
+ const correctPos = `-${correctBgPosX}% -${correctBgPosY}%`;
+ return pos === correctPos;
+ });
+
+ if (isWin) {
+ document.querySelector('.win-message').removeAttribute('hidden');
+ setTimeout(() => {
+ document.querySelector('.win-message').setAttribute('hidden', '');
+ }, 3000);
+ }
+ }
+
+ function showOriginal() {
+ if (currentImage) {
+ tiles.forEach(tile => {
+ tile.style.backgroundImage = `url(${currentImage})`;
+ tile.style.backgroundPosition = 'center';
+ tile.style.backgroundSize = '100%';
+ });
+ }
+ }
+
+ function hideOriginal() {
+ if (currentImage) {
+ setupPuzzle();
+ }
+ }