code a simple puzzle website
- User
code a simple puzzle website
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
code a simple puzzle website
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Here's a simple sliding puzzle website. The puzzle consists of 8 numbered tiles and one empty space. The goal is to arrange the tiles in numerical order by sliding them into the empty space. The website includes a shuffle button to randomize the tiles, a reset button to return the puzzle to its initial state, and a timer to track how long it takes to solve the puzzle.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sliding Puzzle</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
margin: 0;
padding: 20px;
background-color: #f0f0f0;
}
h1 {
color: #333;
}
#puzzle-container {
display: grid;
grid-template-columns: repeat(3, 100px);
grid-template-rows: repeat(3, 100px);
gap: 5px;
margin: 20px auto;
width: 310px;
}
.puzzle-piece {
width: 100px;
height: 100px;
background-color: #fff;
border: 1px solid #000;
display: flex;
justify-content: center;
align-items: center;
font-size: 24px;
cursor: pointer;
background-size: cover;
}
button {
padding: 10px 20px;
margin: 5px;
font-size: 16px;
cursor: pointer;
}
#timer {
margin-top: 20px;
font-size: 18px;
}
</style>
</head>
<body>
<h1>Sliding Puzzle</h1>
<div id="puzzle-container"></div>
<button id="shuffle-btn">Shuffle</button>
<button id="reset-btn">Reset</button>
<div id="timer">Time: 0s</div>
<script>
let puzzlePieces = [1, 2, 3, 4, 5, 6, 7, 8, null];
let emptyIndex = 8;
let timerInterval;
let seconds = 0;
function createPuzzle() {
const puzzleContainer = document.getElementById('puzzle-container');
puzzleContainer.innerHTML = '';
puzzlePieces.forEach((piece, index) => {
const pieceElement = document.createElement('div');
pieceElement.className = 'puzzle-piece';
if (piece !== null) {
pieceElement.textContent = piece;
pieceElement.onclick = () => movePiece(index);
} else {
pieceElement.style.backgroundColor = '#ccc';
}
puzzleContainer.appendChild(pieceElement);
});
}
function movePiece(index) {
const row = Math.floor(index / 3);
const col = index % 3;
const emptyRow = Math.floor(emptyIndex / 3);
const emptyCol = emptyIndex % 3;
const isAdjacent =
(Math.abs(row - emptyRow) === 1 && col === emptyCol) ||
(Math.abs(col - emptyCol) === 1 && row === emptyRow);
if (isAdjacent) {
puzzlePieces[emptyIndex] = puzzlePieces[index];
puzzlePieces[index] = null;
emptyIndex = index;
createPuzzle();
checkWin();
}
}
function shufflePuzzle() {
for (let i = 0; i < 1000; i++) {
const possibleMoves = [];
const row = Math.floor(emptyIndex / 3);
const col = emptyIndex % 3;
if (row > 0) possibleMoves.push(emptyIndex - 3);
if (row < 2) possibleMoves.push(emptyIndex + 3);
if (col > 0) possibleMoves.push(emptyIndex - 1);
if (col < 2) possibleMoves.push(emptyIndex + 1);
const randomMove = possibleMoves[Math.floor(Math.random() * possibleMoves.length)];
puzzlePieces[emptyIndex] = puzzlePieces[randomMove];
puzzlePieces[randomMove] = null;
emptyIndex = randomMove;
}
createPuzzle();
}
function resetPuzzle() {
puzzlePieces = [1, 2, 3, 4, 5, 6, 7, 8, null];
emptyIndex = 8;
createPuzzle();
seconds = 0;
document.getElementById('timer').textContent = `Time: ${seconds}s`;
}
function checkWin() {
const isSolved = puzzlePieces.every((piece, index) => {
if (index === 8) return piece === null;
return piece === index + 1;
});
if (isSolved) {
clearInterval(timerInterval);
alert(`Congratulations! You solved the puzzle in ${seconds} seconds.`);
}
}
function startTimer() {
clearInterval(timerInterval);
seconds = 0;
document.getElementById('timer').textContent = `Time: ${seconds}s`;
timerInterval = setInterval(() => {
seconds++;
document.getElementById('timer').textContent = `Time: ${seconds}s`;
}, 1000);
}
document.getElementById('shuffle-btn').addEventListener('click', () => {
shufflePuzzle();
startTimer();
});
document.getElementById('reset-btn').addEventListener('click', () => {
resetPuzzle();
clearInterval(timerInterval);
});
createPuzzle();
</script>
</body>
</html>
This code creates a functional sliding puzzle game with a timer and reset functionality. You can save this code into an HTML file and open it in a web browser to play the game.