generate me a simple puzzle game in html that creates a new one every time you reload the page. center the game on the screen. you should have a color pallete of black, purple, indigo, and dark green
Assistant
Sure! Below is a simple sliding puzzle game implemented in HTML, CSS, and JavaScript. Every time you reload the page, the puzzle pieces are shuffled to create a new game. The game is centered on the screen and uses the colors black, purple, indigo, and dark green.
HTML Code:
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Sliding Puzzle Game</title>
<style>
body {
background-color: #000000;
color: #FFFFFF;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
#puzzle {
display: grid;
grid-template: repeat(3, 100px) / repeat(3, 100px);
gap: 5px;
}
.tile {
background-color: #4B0082;
color: #FFFFFF;
font-size: 2em;
font-weight: bold;
display: flex;
justify-content: center;
align-items: center;
cursor: pointer;
}
.tile.empty {
background-color: #006400;
cursor: default;
}
</style>
</head>
<body>
<div id="puzzle"></div>
<script>
const puzzleContainer = document.getElementById('puzzle');
let tiles = [];
function initPuzzle() {
// Create an array of numbers from 1 to 8
let numbers = [...Array(8).keys()].map(n => n + 1);
numbers.push(null); // Represent the empty tile
// Shuffle the numbers
numbers.sort(() => Math.random() - 0.5);
// Create tiles
tiles = numbers.map(number => {
const tile = document.createElement('div');
tile.classList.add('tile');
if (number) {
tile.textContent = number;
tile.addEventListener('click', () => moveTile(number));
} else {
tile.classList.add('empty');
}
return tile;
});
// Render the tiles
renderPuzzle();
}
function renderPuzzle() {
puzzleContainer.innerHTML = '';
tiles.forEach(tile => {
puzzleContainer.appendChild(tile);
});
}
function moveTile(number) {
const index = tiles.findIndex(tile => tile.textContent == number);
const emptyIndex = tiles.findIndex(tile => tile.classList.contains('empty'));
// Check if the clicked tile is adjacent to the empty tile
if ([index - 1, index + 1, index - 3, index + 3].includes(emptyIndex)) {
[tiles[index], tiles[emptyIndex]] = [tiles[emptyIndex], tiles[index]];
renderPuzzle();
}
}
initPuzzle();
</script>
</body>
</html>
How the Game Works:
Objective: Arrange the tiles in order from 1 to 8, with the empty space at the end.
Gameplay: Click on a numbered tile adjacent to the empty space to move it into the empty spot.
New Game on Reload: Each time you reload the page, the tiles are shuffled randomly to create a new puzzle.
Color Palette Used:
Background Color: Black (#000000)
Puzzle Tiles: Indigo (#4B0082)
Empty Tile: Dark Green (#006400)
Text Color: White (#FFFFFF)
Styling and Centering:
The game is centered on the screen using Flexbox.
The puzzle grid is created using CSS Grid Layout.
Tiles have a consistent size and margin, ensuring the puzzle looks neat and centered.
Feel free to copy this code into an HTML file and open it in your web browser to play the game. Each time you refresh the page, you'll get a new shuffled puzzle to solve. Enjoy!