deepsite-testing / index.html
upe12345's picture
Add 2 files
1cb6bff verified
Raw History Blame Contribute Delete
26.7 kB
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Conway's Game of Life</title>
<script src="https://cdn.tailwindcss.com"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<style>
.cell {
width: 20px;
height: 20px;
border: 1px solid #e5e7eb;
transition: background-color 0.2s ease;
}
.cell.alive {
background-color: #3b82f6;
}
.cell.dead {
background-color: #f3f4f6;
}
.grid-container {
display: grid;
grid-template-columns: repeat(auto-fill, 22px);
gap: 0;
justify-content: center;
}
@media (min-width: 768px) {
.cell {
width: 25px;
height: 25px;
}
.grid-container {
grid-template-columns: repeat(auto-fill, 27px);
}
}
.pattern-btn {
transition: all 0.2s ease;
}
.pattern-btn:hover {
transform: translateY(-2px);
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
}
</style>
</head>
<body class="bg-gray-100 min-h-screen">
<div class="container mx-auto px-4 py-8">
<div class="text-center mb-8">
<h1 class="text-4xl font-bold text-gray-800 mb-2">Conway's Game of Life</h1>
<p class="text-gray-600 max-w-2xl mx-auto">
A cellular automaton where cells live, die, or multiply based on simple rules.
Click cells to toggle them, or try one of the preset patterns.
</p>
</div>
<div class="flex flex-col lg:flex-row gap-8">
<div class="lg:w-3/4">
<div class="bg-white rounded-lg shadow-lg p-4 mb-4">
<div class="grid-container" id="grid"></div>
</div>
<div class="flex flex-wrap justify-center gap-4 mb-6">
<button id="start" class="bg-green-500 hover:bg-green-600 text-white px-4 py-2 rounded-lg flex items-center gap-2">
<i class="fas fa-play"></i> Start
</button>
<button id="stop" class="bg-red-500 hover:bg-red-600 text-white px-4 py-2 rounded-lg flex items-center gap-2">
<i class="fas fa-stop"></i> Stop
</button>
<button id="step" class="bg-blue-500 hover:bg-blue-600 text-white px-4 py-2 rounded-lg flex items-center gap-2">
<i class="fas fa-step-forward"></i> Step
</button>
<button id="clear" class="bg-gray-500 hover:bg-gray-600 text-white px-4 py-2 rounded-lg flex items-center gap-2">
<i class="fas fa-trash"></i> Clear
</button>
<button id="random" class="bg-purple-500 hover:bg-purple-600 text-white px-4 py-2 rounded-lg flex items-center gap-2">
<i class="fas fa-random"></i> Random
</button>
</div>
<div class="bg-white rounded-lg shadow-lg p-4">
<h2 class="text-xl font-semibold text-gray-800 mb-3">Rules</h2>
<ul class="list-disc pl-5 text-gray-700 space-y-1">
<li>Any live cell with fewer than two live neighbors dies (underpopulation).</li>
<li>Any live cell with two or three live neighbors lives on to the next generation.</li>
<li>Any live cell with more than three live neighbors dies (overpopulation).</li>
<li>Any dead cell with exactly three live neighbors becomes a live cell (reproduction).</li>
</ul>
</div>
</div>
<div class="lg:w-1/4">
<div class="bg-white rounded-lg shadow-lg p-4 mb-4">
<h2 class="text-xl font-semibold text-gray-800 mb-3">Controls</h2>
<div class="space-y-2">
<div class="flex items-center justify-between">
<span class="text-gray-700">Speed:</span>
<input type="range" id="speed" min="50" max="1000" value="200" class="w-32">
<span id="speed-value" class="text-gray-700">200ms</span>
</div>
<div class="flex items-center justify-between">
<span class="text-gray-700">Grid Size:</span>
<input type="range" id="size" min="10" max="50" value="30" class="w-32">
<span id="size-value" class="text-gray-700">30x30</span>
</div>
</div>
</div>
<div class="bg-white rounded-lg shadow-lg p-4">
<h2 class="text-xl font-semibold text-gray-800 mb-3">Patterns</h2>
<div class="grid grid-cols-2 gap-2">
<button class="pattern-btn bg-indigo-100 hover:bg-indigo-200 text-indigo-800 px-3 py-2 rounded" data-pattern="glider">
Glider
</button>
<button class="pattern-btn bg-indigo-100 hover:bg-indigo-200 text-indigo-800 px-3 py-2 rounded" data-pattern="blinker">
Blinker
</button>
<button class="pattern-btn bg-indigo-100 hover:bg-indigo-200 text-indigo-800 px-3 py-2 rounded" data-pattern="random">
Random
</button>
<button class="pattern-btn bg-indigo-100 hover:bg-indigo-200 text-indigo-800 px-3 py-2 rounded" data-pattern="gosper">
Gosper Glider Gun
</button>
<button class="pattern-btn bg-indigo-100 hover:bg-indigo-200 text-indigo-800 px-3 py-2 rounded" data-pattern="spaceship">
Lightweight Spaceship
</button>
<button class="pattern-btn bg-indigo-100 hover:bg-indigo-200 text-indigo-800 px-3 py-2 rounded" data-pattern="pulsar">
Pulsar
</button>
</div>
</div>
<div class="bg-white rounded-lg shadow-lg p-4 mt-4">
<h2 class="text-xl font-semibold text-gray-800 mb-3">Stats</h2>
<div class="space-y-2">
<div class="flex justify-between">
<span class="text-gray-700">Generation:</span>
<span id="generation" class="font-medium">0</span>
</div>
<div class="flex justify-between">
<span class="text-gray-700">Live Cells:</span>
<span id="live-cells" class="font-medium">0</span>
</div>
</div>
</div>
</div>
</div>
</div>
<script>
document.addEventListener('DOMContentLoaded', () => {
// Game state
let grid = [];
let nextGrid = [];
let rows = 30;
let cols = 30;
let generation = 0;
let isRunning = false;
let intervalId = null;
let speed = 200;
// DOM elements
const gridElement = document.getElementById('grid');
const startButton = document.getElementById('start');
const stopButton = document.getElementById('stop');
const stepButton = document.getElementById('step');
const clearButton = document.getElementById('clear');
const randomButton = document.getElementById('random');
const speedInput = document.getElementById('speed');
const speedValue = document.getElementById('speed-value');
const sizeInput = document.getElementById('size');
const sizeValue = document.getElementById('size-value');
const generationDisplay = document.getElementById('generation');
const liveCellsDisplay = document.getElementById('live-cells');
const patternButtons = document.querySelectorAll('.pattern-btn');
// Initialize the grid
function initializeGrid() {
grid = [];
nextGrid = [];
for (let i = 0; i < rows; i++) {
grid[i] = [];
nextGrid[i] = [];
for (let j = 0; j < cols; j++) {
grid[i][j] = 0;
nextGrid[i][j] = 0;
}
}
renderGrid();
}
// Render the grid to the DOM
function renderGrid() {
gridElement.innerHTML = '';
gridElement.style.gridTemplateColumns = `repeat(${cols}, 22px)`;
if (window.innerWidth >= 768) {
gridElement.style.gridTemplateColumns = `repeat(${cols}, 27px)`;
}
let liveCells = 0;
for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
const cell = document.createElement('div');
cell.className = `cell ${grid[i][j] ? 'alive' : 'dead'}`;
cell.dataset.row = i;
cell.dataset.col = j;
cell.addEventListener('click', () => {
grid[i][j] = grid[i][j] ? 0 : 1;
cell.className = `cell ${grid[i][j] ? 'alive' : 'dead'}`;
updateStats();
});
gridElement.appendChild(cell);
if (grid[i][j]) {
liveCells++;
}
}
}
liveCellsDisplay.textContent = liveCells;
}
// Compute the next generation
function computeNextGeneration() {
for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
const neighbors = countNeighbors(i, j);
// Apply Conway's rules
if (grid[i][j] === 1) {
// Cell is alive
if (neighbors < 2 || neighbors > 3) {
nextGrid[i][j] = 0; // Dies
} else {
nextGrid[i][j] = 1; // Lives on
}
} else {
// Cell is dead
if (neighbors === 3) {
nextGrid[i][j] = 1; // Reproduction
} else {
nextGrid[i][j] = 0; // Stays dead
}
}
}
}
// Swap grids
const temp = grid;
grid = nextGrid;
nextGrid = temp;
generation++;
generationDisplay.textContent = generation;
renderGrid();
}
// Count live neighbors for a cell
function countNeighbors(row, col) {
let count = 0;
for (let i = -1; i <= 1; i++) {
for (let j = -1; j <= 1; j++) {
if (i === 0 && j === 0) continue; // Skip the cell itself
const newRow = row + i;
const newCol = col + j;
// Check boundaries
if (newRow >= 0 && newRow < rows && newCol >= 0 && newCol < cols) {
count += grid[newRow][newCol];
}
}
}
return count;
}
// Start the simulation
function startSimulation() {
if (!isRunning) {
isRunning = true;
intervalId = setInterval(computeNextGeneration, speed);
startButton.disabled = true;
stopButton.disabled = false;
}
}
// Stop the simulation
function stopSimulation() {
if (isRunning) {
isRunning = false;
clearInterval(intervalId);
startButton.disabled = false;
stopButton.disabled = true;
}
}
// Step through one generation
function stepSimulation() {
if (!isRunning) {
computeNextGeneration();
}
}
// Clear the grid
function clearGrid() {
stopSimulation();
generation = 0;
generationDisplay.textContent = generation;
for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
grid[i][j] = 0;
}
}
renderGrid();
}
// Randomize the grid
function randomizeGrid() {
stopSimulation();
generation = 0;
generationDisplay.textContent = generation;
for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
grid[i][j] = Math.random() > 0.7 ? 1 : 0;
}
}
renderGrid();
}
// Update stats
function updateStats() {
let liveCells = 0;
for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
if (grid[i][j]) {
liveCells++;
}
}
}
liveCellsDisplay.textContent = liveCells;
}
// Load a pattern
function loadPattern(pattern) {
clearGrid();
const centerRow = Math.floor(rows / 2);
const centerCol = Math.floor(cols / 2);
switch (pattern) {
case 'glider':
// Glider pattern
grid[centerRow][centerCol] = 1;
grid[centerRow][centerCol + 1] = 1;
grid[centerRow][centerCol + 2] = 1;
grid[centerRow - 1][centerCol + 2] = 1;
grid[centerRow - 2][centerCol + 1] = 1;
break;
case 'blinker':
// Blinker pattern
grid[centerRow][centerCol] = 1;
grid[centerRow][centerCol + 1] = 1;
grid[centerRow][centerCol - 1] = 1;
break;
case 'gosper':
// Gosper Glider Gun (simplified)
if (rows >= 20 && cols >= 40) {
const startRow = centerRow - 5;
const startCol = centerCol - 10;
// Left square
grid[startRow][startCol + 1] = 1;
grid[startRow + 1][startCol + 1] = 1;
grid[startRow][startCol + 2] = 1;
grid[startRow + 1][startCol + 2] = 1;
// Right square
grid[startRow + 8][startCol + 35] = 1;
grid[startRow + 8][startCol + 36] = 1;
grid[startRow + 9][startCol + 35] = 1;
grid[startRow + 9][startCol + 36] = 1;
// Gun parts
grid[startRow + 2][startCol + 11] = 1;
grid[startRow + 3][startCol + 11] = 1;
grid[startRow + 4][startCol + 11] = 1;
grid[startRow + 2][startCol + 12] = 1;
grid[startRow + 3][startCol + 12] = 1;
grid[startRow + 4][startCol + 12] = 1;
grid[startRow + 1][startCol + 13] = 1;
grid[startRow + 5][startCol + 13] = 1;
grid[startRow][startCol + 14] = 1;
grid[startRow + 6][startCol + 14] = 1;
grid[startRow][startCol + 15] = 1;
grid[startRow + 6][startCol + 15] = 1;
grid[startRow + 1][startCol + 17] = 1;
grid[startRow + 5][startCol + 17] = 1;
grid[startRow + 2][startCol + 18] = 1;
grid[startRow + 3][startCol + 18] = 1;
grid[startRow + 4][startCol + 18] = 1;
grid[startRow + 3][startCol + 19] = 1;
grid[startRow + 2][startCol + 21] = 1;
grid[startRow + 3][startCol + 21] = 1;
grid[startRow + 4][startCol + 21] = 1;
}
break;
case 'spaceship':
// Lightweight spaceship
grid[centerRow][centerCol] = 1;
grid[centerRow][centerCol + 3] = 1;
grid[centerRow + 1][centerCol + 4] = 1;
grid[centerRow + 2][centerCol] = 1;
grid[centerRow + 2][centerCol + 4] = 1;
grid[centerRow + 3][centerCol + 1] = 1;
grid[centerRow + 3][centerCol + 2] = 1;
grid[centerRow + 3][centerCol + 3] = 1;
grid[centerRow + 3][centerCol + 4] = 1;
break;
case 'pulsar':
// Pulsar pattern (needs at least 15x15 grid)
if (rows >= 15 && cols >= 15) {
const startRow = centerRow - 6;
const startCol = centerCol - 6;
// Top left block
grid[startRow + 2][startCol + 4] = 1;
grid[startRow + 2][startCol + 5] = 1;
grid[startRow + 2][startCol + 6] = 1;
grid[startRow + 7][startCol + 4] = 1;
grid[startRow + 7][startCol + 5] = 1;
grid[startRow + 7][startCol + 6] = 1;
// Top right block
grid[startRow + 2][startCol + 10] = 1;
grid[startRow + 2][startCol + 11] = 1;
grid[startRow + 2][startCol + 12] = 1;
grid[startRow + 7][startCol + 10] = 1;
grid[startRow + 7][startCol + 11] = 1;
grid[startRow + 7][startCol + 12] = 1;
// Bottom left block
grid[startRow + 4][startCol + 2] = 1;
grid[startRow + 5][startCol + 2] = 1;
grid[startRow + 6][startCol + 2] = 1;
grid[startRow + 4][startCol + 7] = 1;
grid[startRow + 5][startCol + 7] = 1;
grid[startRow + 6][startCol + 7] = 1;
// Bottom right block
grid[startRow + 10][startCol + 2] = 1;
grid[startRow + 11][startCol + 2] = 1;
grid[startRow + 12][startCol + 2] = 1;
grid[startRow + 10][startCol + 7] = 1;
grid[startRow + 11][startCol + 7] = 1;
grid[startRow + 12][startCol + 7] = 1;
// Center blocks
grid[startRow + 4][startCol + 4] = 1;
grid[startRow + 4][startCol + 5] = 1;
grid[startRow + 4][startCol + 6] = 1;
grid[startRow + 5][startCol + 4] = 1;
grid[startRow + 5][startCol + 6] = 1;
grid[startRow + 6][startCol + 4] = 1;
grid[startRow + 6][startCol + 5] = 1;
grid[startRow + 6][startCol + 6] = 1;
grid[startRow + 4][startCol + 10] = 1;
grid[startRow + 4][startCol + 11] = 1;
grid[startRow + 4][startCol + 12] = 1;
grid[startRow + 5][startCol + 10] = 1;
grid[startRow + 5][startCol + 12] = 1;
grid[startRow + 6][startCol + 10] = 1;
grid[startRow + 6][startCol + 11] = 1;
grid[startRow + 6][startCol + 12] = 1;
grid[startRow + 10][startCol + 4] = 1;
grid[startRow + 10][startCol + 5] = 1;
grid[startRow + 10][startCol + 6] = 1;
grid[startRow + 11][startCol + 4] = 1;
grid[startRow + 11][startCol + 6] = 1;
grid[startRow + 12][startCol + 4] = 1;
grid[startRow + 12][startCol + 5] = 1;
grid[startRow + 12][startCol + 6] = 1;
grid[startRow + 10][startCol + 10] = 1;
grid[startRow + 10][startCol + 11] = 1;
grid[startRow + 10][startCol + 12] = 1;
grid[startRow + 11][startCol + 10] = 1;
grid[startRow + 11][startCol + 12] = 1;
grid[startRow + 12][startCol + 10] = 1;
grid[startRow + 12][startCol + 11] = 1;
grid[startRow + 12][startCol + 12] = 1;
}
break;
case 'random':
randomizeGrid();
return;
}
renderGrid();
}
// Change grid size
function changeGridSize() {
rows = parseInt(sizeInput.value);
cols = parseInt(sizeInput.value);
sizeValue.textContent = `${rows}x${cols}`;
stopSimulation();
initializeGrid();
}
// Change simulation speed
function changeSpeed() {
speed = parseInt(speedInput.value);
speedValue.textContent = `${speed}ms`;
if (isRunning) {
stopSimulation();
startSimulation();
}
}
// Event listeners
startButton.addEventListener('click', startSimulation);
stopButton.addEventListener('click', stopSimulation);
stepButton.addEventListener('click', stepSimulation);
clearButton.addEventListener('click', clearGrid);
randomButton.addEventListener('click', randomizeGrid);
speedInput.addEventListener('input', changeSpeed);
sizeInput.addEventListener('input', changeGridSize);
patternButtons.forEach(button => {
button.addEventListener('click', () => {
loadPattern(button.dataset.pattern);
});
});
// Initialize
initializeGrid();
stopButton.disabled = true;
});
</script>
<p style="border-radius: 8px; text-align: center; font-size: 12px; color: #fff; margin-top: 16px;position: fixed; left: 8px; bottom: 8px; z-index: 10; background: rgba(0, 0, 0, 0.8); padding: 4px 8px;">Made with <img src="https://enzostvs-deepsite.hf.space/logo.svg" alt="DeepSite Logo" style="width: 16px; height: 16px; vertical-align: middle;display:inline-block;margin-right:3px;filter:brightness(0) invert(1);"><a href="https://enzostvs-deepsite.hf.space" style="color: #fff;text-decoration: underline;" target="_blank" >DeepSite</a> - <a href="https://enzostvs-deepsite.hf.space?remix=upe12345/deepsite-testing" style="color: #fff;text-decoration: underline;" target="_blank" >🧬 Remix</a></p></body>
</html>