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ee62d70
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>ZigZag Puzzle</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <script src="https://unpkg.com/feather-icons"></script>
    <script src="https://cdn.jsdelivr.net/npm/feather-icons/dist/feather.min.js"></script>
    <style>
        @keyframes pulse {
            0%, 100% { transform: scale(1); }
            50% { transform: scale(1.05); }
        }
        .pulse-animation {
            animation: pulse 1.5s infinite;
        }
        .tile {
            transition: all 0.3s ease;
        }
        .tile:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
        }
        #game-board {
            perspective: 1000px;
        }
        .win-effect {
            animation: confetti 3s ease-out;
        }
        @keyframes confetti {
            0% { transform: translateY(0) rotate(0deg); opacity: 1; }
            100% { transform: translateY(-100vh) rotate(360deg); opacity: 0; }
        }
    </style>
</head>
<body class="bg-gradient-to-br from-indigo-900 to-purple-800 min-h-screen font-sans text-white">
    <div class="container mx-auto px-4 py-8">
        <header class="text-center mb-8">
            <h1 class="text-4xl md:text-5xl font-bold mb-2">ZigZag Puzzle</h1>
            <p class="text-xl text-indigo-200">Slide the tiles to solve the puzzle!</p>
        </header>

        <div class="flex flex-col md:flex-row gap-8 items-center justify-center">
            <div id="game-board" class="bg-white/10 backdrop-blur-md rounded-xl p-4 shadow-2xl">
                <div class="grid grid-cols-4 gap-2" id="puzzle-container"></div>
            </div>

            <div class="bg-white/10 backdrop-blur-md rounded-xl p-6 shadow-2xl max-w-md w-full">
                <div class="flex items-center justify-between mb-6">
                    <div>
                        <h2 class="text-2xl font-bold">Game Stats</h2>
                        <p class="text-indigo-200">Complete the puzzle!</p>
                    </div>
                    <div class="bg-indigo-600 rounded-full p-3">
                        <i data-feather="clock" class="w-6 h-6"></i>
                    </div>
                </div>

                <div class="space-y-4">
                    <div class="flex justify-between items-center bg-white/5 rounded-lg p-4">
                        <div>
                            <p class="text-indigo-200">Moves</p>
                            <p class="text-2xl font-bold" id="move-counter">0</p>
                        </div>
                        <i data-feather="repeat" class="w-6 h-6 text-indigo-300"></i>
                    </div>

                    <div class="flex justify-between items-center bg-white/5 rounded-lg p-4">
                        <div>
                            <p class="text-indigo-200">Time</p>
                            <p class="text-2xl font-bold" id="time-counter">00:00</p>
                        </div>
                        <i data-feather="clock" class="w-6 h-6 text-indigo-300"></i>
                    </div>

                    <div class="flex justify-between items-center bg-white/5 rounded-lg p-4">
                        <div>
                            <p class="text-indigo-200">Difficulty</p>
                            <p class="text-2xl font-bold" id="difficulty">Easy</p>
                        </div>
                        <i data-feather="sliders" class="w-6 h-6 text-indigo-300"></i>
                    </div>
                </div>

                <div class="mt-6 space-y-3">
                    <button id="shuffle-btn" class="w-full bg-indigo-600 hover:bg-indigo-700 text-white font-bold py-3 px-4 rounded-lg flex items-center justify-center gap-2 transition-colors">
                        <i data-feather="refresh-cw" class="w-5 h-5"></i>
                        <span>Shuffle</span>
                    </button>
                    <button id="new-game-btn" class="w-full bg-purple-600 hover:bg-purple-700 text-white font-bold py-3 px-4 rounded-lg flex items-center justify-center gap-2 transition-colors">
                        <i data-feather="plus-circle" class="w-5 h-5"></i>
                        <span>New Game</span>
                    </button>
                    <div class="grid grid-cols-2 gap-3">
                        <button id="hint-btn" class="bg-amber-600 hover:bg-amber-700 text-white font-bold py-3 px-4 rounded-lg flex items-center justify-center gap-2 transition-colors">
                            <i data-feather="help-circle" class="w-5 h-5"></i>
                            <span>Hint</span>
                        </button>
                        <button id="solve-btn" class="bg-emerald-600 hover:bg-emerald-700 text-white font-bold py-3 px-4 rounded-lg flex items-center justify-center gap-2 transition-colors">
                            <i data-feather="check-circle" class="w-5 h-5"></i>
                            <span>Solve</span>
                        </button>
                    </div>
                </div>
            </div>
        </div>

        <div class="mt-12 text-center">
            <h2 class="text-2xl font-bold mb-4">How to Play</h2>
            <div class="grid md:grid-cols-3 gap-6 max-w-4xl mx-auto">
                <div class="bg-white/5 rounded-xl p-6">
                    <div class="bg-indigo-600 rounded-full w-12 h-12 flex items-center justify-center mb-4 mx-auto">
                        <i data-feather="move" class="w-6 h-6"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-2">Slide Tiles</h3>
                    <p class="text-indigo-200">Click or tap adjacent tiles to swap them with the empty space.</p>
                </div>
                <div class="bg-white/5 rounded-xl p-6">
                    <div class="bg-indigo-600 rounded-full w-12 h-12 flex items-center justify-center mb-4 mx-auto">
                        <i data-feather="target" class="w-6 h-6"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-2">Complete the Pattern</h3>
                    <p class="text-indigo-200">Arrange all tiles to form the correct zigzag pattern.</p>
                </div>
                <div class="bg-white/5 rounded-xl p-6">
                    <div class="bg-indigo-600 rounded-full w-12 h-12 flex items-center justify-center mb-4 mx-auto">
                        <i data-feather="award" class="w-6 h-6"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-2">Beat Your Score</h3>
                    <p class="text-indigo-200">Complete the puzzle in as few moves and as quickly as possible.</p>
                </div>
            </div>
        </div>
    </div>

    <div id="win-modal" class="fixed inset-0 bg-black/80 flex items-center justify-center z-50 hidden">
        <div class="bg-gradient-to-br from-indigo-600 to-purple-700 rounded-2xl p-8 max-w-md w-full mx-4 text-center transform transition-all duration-500 scale-95 opacity-0">
            <div class="bg-white/20 rounded-full w-20 h-20 flex items-center justify-center mb-6 mx-auto pulse-animation">
                <i data-feather="award" class="w-10 h-10 text-white"></i>
            </div>
            <h2 class="text-3xl font-bold mb-2">Puzzle Solved!</h2>
            <p class="text-indigo-100 mb-6">Congratulations on completing the puzzle!</p>
            
            <div class="bg-white/10 rounded-xl p-4 mb-6">
                <div class="grid grid-cols-2 gap-4">
                    <div>
                        <p class="text-indigo-200">Moves</p>
                        <p class="text-2xl font-bold" id="final-moves">0</p>
                    </div>
                    <div>
                        <p class="text-indigo-200">Time</p>
                        <p class="text-2xl font-bold" id="final-time">00:00</p>
                    </div>
                </div>
            </div>
            
            <button id="play-again-btn" class="w-full bg-white text-indigo-900 hover:bg-indigo-100 font-bold py-3 px-4 rounded-lg flex items-center justify-center gap-2 transition-colors">
                <i data-feather="play" class="w-5 h-5"></i>
                <span>Play Again</span>
            </button>
        </div>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            feather.replace();
            
            // Game state
            const state = {
                board: [],
                size: 4,
                emptyPos: { row: 3, col: 3 },
                moves: 0,
                time: 0,
                timer: null,
                isPlaying: false,
                difficulty: 'easy'
            };

            // DOM elements
            const puzzleContainer = document.getElementById('puzzle-container');
            const moveCounter = document.getElementById('move-counter');
            const timeCounter = document.getElementById('time-counter');
            const difficultyDisplay = document.getElementById('difficulty');
            const shuffleBtn = document.getElementById('shuffle-btn');
            const newGameBtn = document.getElementById('new-game-btn');
            const hintBtn = document.getElementById('hint-btn');
            const solveBtn = document.getElementById('solve-btn');
            const winModal = document.getElementById('win-modal');
            const finalMoves = document.getElementById('final-moves');
            const finalTime = document.getElementById('final-time');
            const playAgainBtn = document.getElementById('play-again-btn');

            // Initialize the game
            function initGame() {
                createBoard();
                renderBoard();
                setupEventListeners();
            }

            // Create the board
            function createBoard() {
                state.board = [];
                let counter = 1;
                
                for (let i = 0; i < state.size; i++) {
                    state.board[i] = [];
                    for (let j = 0; j < state.size; j++) {
                        if (i === state.size - 1 && j === state.size - 1) {
                            state.board[i][j] = 0; // Empty tile
                        } else {
                            state.board[i][j] = counter++;
                        }
                    }
                }
                
                // Set empty position
                state.emptyPos = { row: state.size - 1, col: state.size - 1 };
                state.moves = 0;
                moveCounter.textContent = state.moves;
                
                // Reset timer
                clearInterval(state.timer);
                state.time = 0;
                updateTimeDisplay();
            }

            // Render the board
            function renderBoard() {
                puzzleContainer.innerHTML = '';
                puzzleContainer.style.gridTemplateColumns = `repeat(${state.size}, 1fr)`;
                
                for (let i = 0; i < state.size; i++) {
                    for (let j = 0; j < state.size; j++) {
                        const value = state.board[i][j];
                        const tile = document.createElement('div');
                        
                        if (value === 0) {
                            tile.className = 'tile bg-transparent';
                        } else {
                            tile.className = `tile bg-gradient-to-br from-indigo-500 to-purple-600 flex items-center justify-center text-white font-bold text-xl md:text-2xl rounded-lg cursor-pointer hover:shadow-lg transition-all duration-200 h-16 md:h-20`;
                            tile.textContent = value;
                            
                            // Zigzag pattern colors
                            if ((i + j) % 2 === 0) {
                                tile.classList.add('from-indigo-500', 'to-purple-600');
                            } else {
                                tile.classList.add('from-purple-500', 'to-indigo-600');
                            }
                            
                            tile.dataset.row = i;
                            tile.dataset.col = j;
                        }
                        
                        puzzleContainer.appendChild(tile);
                    }
                }
            }

            // Shuffle the board
            function shuffleBoard() {
                let shuffleCount = 0;
                const maxShuffles = 100;
                
                const shuffleInterval = setInterval(() => {
                    if (shuffleCount >= maxShuffles) {
                        clearInterval(shuffleInterval);
                        state.isPlaying = true;
                        startTimer();
                        return;
                    }
                    
                    const directions = [];
                    const { row, col } = state.emptyPos;
                    
                    if (row > 0) directions.push('up');
                    if (row < state.size - 1) directions.push('down');
                    if (col > 0) directions.push('left');
                    if (col < state.size - 1) directions.push('right');
                    
                    const randomDir = directions[Math.floor(Math.random() * directions.length)];
                    moveTile(randomDir, false);
                    
                    shuffleCount++;
                }, 50);
            }

            // Move tile
            function moveTile(direction, countMove = true) {
                const { row, col } = state.emptyPos;
                let newRow = row;
                let newCol = col;
                
                switch (direction) {
                    case 'up':
                        if (row === 0) return false;
                        newRow = row - 1;
                        break;
                    case 'down':
                        if (row === state.size - 1) return false;
                        newRow = row + 1;
                        break;
                    case 'left':
                        if (col === 0) return false;
                        newCol = col - 1;
                        break;
                    case 'right':
                        if (col === state.size - 1) return false;
                        newCol = col + 1;
                        break;
                    default:
                        return false;
                }
                
                // Swap tiles
                state.board[row][col] = state.board[newRow][newCol];
                state.board[newRow][newCol] = 0;
                state.emptyPos = { row: newRow, col: newCol };
                
                if (countMove) {
                    state.moves++;
                    moveCounter.textContent = state.moves;
                }
                
                renderBoard();
                
                // Check if puzzle is solved
                if (isSolved()) {
                    clearInterval(state.timer);
                    showWinModal();
                }
                
                return true;
            }

            // Check if puzzle is solved
            function isSolved() {
                let counter = 1;
                for (let i = 0; i < state.size; i++) {
                    for (let j = 0; j < state.size; j++) {
                        if (i === state.size - 1 && j === state.size - 1) {
                            if (state.board[i][j] !== 0) return false;
                        } else {
                            if (state.board[i][j] !== counter++) return false;
                        }
                    }
                }
                return true;
            }

            // Timer functions
            function startTimer() {
                clearInterval(state.timer);
                state.time = 0;
                updateTimeDisplay();
                
                state.timer = setInterval(() => {
                    state.time++;
                    updateTimeDisplay();
                }, 1000);
            }

            function updateTimeDisplay() {
                const minutes = Math.floor(state.time / 60).toString().padStart(2, '0');
                const seconds = (state.time % 60).toString().padStart(2, '0');
                timeCounter.textContent = `${minutes}:${seconds}`;
            }

            // Show win modal
            function showWinModal() {
                finalMoves.textContent = state.moves;
                finalTime.textContent = timeCounter.textContent;
                
                const modalContent = winModal.querySelector('div');
                modalContent.classList.remove('scale-95', 'opacity-0');
                modalContent.classList.add('scale-100', 'opacity-100');
                
                winModal.classList.remove('hidden');
                
                // Add confetti effect
                for (let i = 0; i < 50; i++) {
                    const confetti = document.createElement('div');
                    confetti.className = 'absolute w-2 h-2 rounded-full bg-yellow-400 win-effect';
                    confetti.style.left = `${Math.random() * 100}%`;
                    confetti.style.top = `${Math.random() * 100}%`;
                    confetti.style.animationDelay = `${Math.random() * 0.5}s`;
                    winModal.appendChild(confetti);
                    
                    setTimeout(() => {
                        confetti.remove();
                    }, 3000);
                }
            }

            // Setup event listeners
            function setupEventListeners() {
                // Tile click
                puzzleContainer.addEventListener('click', (e) => {
                    if (!state.isPlaying) return;
                    
                    const tile = e.target.closest('.tile');
                    if (!tile || tile.textContent === '') return;
                    
                    const row = parseInt(tile.dataset.row);
                    const col = parseInt(tile.dataset.col);
                    const emptyRow = state.emptyPos.row;
                    const emptyCol = state.emptyPos.col;
                    
                    // Check if tile is adjacent to empty space
                    if ((Math.abs(row - emptyRow) === 1 && col === emptyCol) || 
                        (Math.abs(col - emptyCol) === 1 && row === emptyRow)) {
                        
                        // Determine direction
                        let direction;
                        if (row < emptyRow) direction = 'up';
                        else if (row > emptyRow) direction = 'down';
                        else if (col < emptyCol) direction = 'left';
                        else direction = 'right';
                        
                        moveTile(direction);
                    }
                });
                
                // Keyboard controls
                document.addEventListener('keydown', (e) => {
                    if (!state.isPlaying) return;
                    
                    switch (e.key) {
                        case 'ArrowUp':
                            moveTile('down');
                            break;
                        case 'ArrowDown':
                            moveTile('up');
                            break;
                        case 'ArrowLeft':
                            moveTile('right');
                            break;
                        case 'ArrowRight':
                            moveTile('left');
                            break;
                    }
                });
                
                // Shuffle button
                shuffleBtn.addEventListener('click', () => {
                    createBoard();
                    shuffleBoard();
                });
                
                // New game button
                newGameBtn.addEventListener('click', () => {
                    createBoard();
                    shuffleBoard();
                });
                
                // Hint button
                hintBtn.addEventListener('click', () => {
                    if (!state.isPlaying) return;
                    
                    // Simple hint: highlight possible moves
                    const { row, col } = state.emptyPos;
                    const tiles = puzzleContainer.querySelectorAll('.tile');
                    
                    tiles.forEach(tile => {
                        if (tile.textContent === '') return;
                        
                        const tileRow = parseInt(tile.dataset.row);
                        const tileCol = parseInt(tile.dataset.col);
                        
                        if ((Math.abs(tileRow - row) === 1 && tileCol === col) || 
                            (Math.abs(tileCol - col) === 1 && tileRow === row)) {
                            
                            tile.classList.add('ring-4', 'ring-yellow-400');
                            
                            setTimeout(() => {
                                tile.classList.remove('ring-4', 'ring-yellow-400');
                            }, 1000);
                        }
                    });
                });
                
                // Solve button
                solveBtn.addEventListener('click', () => {
                    createBoard();
                    state.isPlaying = false;
                    clearInterval(state.timer);
                });
                
                // Play again button
                playAgainBtn.addEventListener('click', () => {
                    winModal.classList.add('hidden');
                    createBoard();
                    shuffleBoard();
                });
            }

            // Initialize the game
            initGame();
        });
    </script>
    <script>feather.replace();</script>
</body>
</html>