Here's a simple 2D grid-based zombie infection simulator in HTML, CSS, and JavaScript. The simulator generates a map with cities connected by roads, and simulates the spread of a zombie infection starting from one city.
<!DOCTYPE html>
<html>
<head>
<title>Zombie Infection Simulator</title>
<style>
/* Style the grid container */
#grid {
display: grid;
grid-template-columns: repeat(20, 20px);
grid-template-rows: repeat(20, 20px);
gap: 1px;
margin: 20px auto;
width: max-content;
}
/* Style each cell in the grid */
.cell {
width: 20px;
height: 20px;
background-color: #eaeaea;
transition: background-color 0.5s;
}
/* Style for cities */
.city {
background-color: #5062f0;
}
/* Style for roads */
.road {
background-color: #c0c0c0;
}
/* Style for infected cells */
.infected {
background-color: #f05050;
}
</style>
</head>
<body>
<h1>Zombie Infection Simulator</h1>
<div id="grid"></div>
<script>
// Grid size (number of cells in each dimension)
const gridSize = 20;
// Number of cities to generate
const cityCount = 5;
// 2D array to store grid cells
const grid = [];
// Reference to the grid container element
const gridElement = document.getElementById('grid');
// Generate the grid and display it
for (let y = 0; y < gridSize; y++) {
grid[y] = [];
for (let x = 0; x < gridSize; x++) {
// Create a cell element
const cellElement = document.createElement('div');
cellElement.classList.add('cell');
gridElement.appendChild(cellElement);
// Store cell data
grid[y][x] = {
element: cellElement,
type: 'empty', // Possible types: 'empty', 'city', 'road', 'infected'
x: x,
y: y
};
}
}
// Place cities randomly on the grid
function placeCities() {
let placed = 0;
while (placed < cityCount) {
const x = Math.floor(Math.random() * gridSize);
const y = Math.floor(Math.random() * gridSize);
if (grid[y][x].type === 'empty') {
grid[y][x].type = 'city';
grid[y][x].element.classList.add('city');
placed++;
}
}
}
placeCities();
// Get the positions of all the cities
const cities = [];
for (let y = 0; y < gridSize; y++) {
for (let x = 0; x < gridSize; x++) {
if (grid[y][x].type === 'city') {
cities.push(grid[y][x]);
}
}
}
// Connect the cities with roads
function buildRoads() {
for (let i = 0; i < cities.length - 1; i++) {
const start = cities[i];
const end = cities[i + 1];
drawRoad(start.x, start.y, end.x, end.y);
}
}
// Draw a road between two points using a simple path
function drawRoad(x0, y0, x1, y1) {
let x = x0;
let y = y0;
// Randomize the order of movement (horizontal first or vertical first)
const horizontalFirst = Math.random() < 0.5;
if (horizontalFirst) {
// Move horizontally towards x1
while (x !== x1) {
x += x < x1 ? 1 : -1;
markRoad(x, y);
}
// Move vertically towards y1
while (y !== y1) {
y += y < y1 ? 1 : -1;
markRoad(x, y);
}
} else {
// Move vertically towards y1
while (y !== y1) {
y += y < y1 ? 1 : -1;
markRoad(x, y);
}
// Move horizontally towards x1
while (x !== x1) {
x += x < x1 ? 1 : -1;
markRoad(x, y);
}
}
}
// Mark a cell as a road if it's empty
function markRoad(x, y) {
if (grid[y][x].type === 'empty') {
grid[y][x].type = 'road';
grid[y][x].element.classList.add('road');
}
}
buildRoads();
// Start the infection at the first city
function startInfection() {
const firstCity = cities[0];
grid[firstCity.y][firstCity.x].type = 'infected';
grid[firstCity.y][firstCity.x].element.classList.add('infected');
}
startInfection();
// Simulate the spread of infection over time
function spreadInfection() {
const infectedQueue = [];
// Initialize the queue with the initially infected cells
for (let y = 0; y < gridSize; y++) {
for (let x = 0; x < gridSize; x++) {
if (grid[y][x].type === 'infected') {
infectedQueue.push(grid[y][x]);
}
}
}
// Spread the infection at regular intervals
const interval = setInterval(() => {
if (infectedQueue.length === 0) {
clearInterval(interval);
return;
}
const current = infectedQueue.shift();
const neighbors = getNeighbors(current.x, current.y);
neighbors.forEach(neighbor => {
// Infect neighboring roads and cities if they are not already infected
if ((neighbor.type === 'road' || neighbor.type === 'city') && !neighbor.infected) {
neighbor.type = 'infected';
neighbor.element.classList.add('infected');
neighbor.infected = true;
infectedQueue.push(neighbor);
}
});
}, 200); // Adjust the interval (in milliseconds) to control the speed of infection spread
}
// Get neighboring cells (up, down, left, right)
function getNeighbors(x, y) {
const neighbors = [];
if (x > 0) neighbors.push(grid[y][x - 1]);
if (x < gridSize - 1) neighbors.push(grid[y][x + 1]);
if (y > 0) neighbors.push(grid[y - 1][x]);
if (y < gridSize - 1) neighbors.push(grid[y + 1][x]);
return neighbors;
}
spreadInfection();
</script>
</body>
</html>
Explanation:
-
HTML Structure:
- A
<div> with the id grid is used as a container for the grid of cells.
-
CSS Styles:
- The
#grid element is styled as a grid layout with display: grid, defining a 20x20 grid.
- Each
.cell represents a grid cell with a default background color and a transition for smooth color changes.
- Additional classes
.city, .road, and .infected define styles for cities, roads, and infected cells respectively.
-
JavaScript Logic:
-
Grid Initialization:
- A 2D array
grid is created to store cell data.
- Each cell object contains its DOM element, type, and coordinates.
-
Placing Cities:
- Random empty cells are selected to place the specified number of cities.
- These cells are marked with the type
'city' and styled accordingly.
-
Building Roads:
- The cities are connected in sequence with roads.
- The
drawRoad function creates a path between two cities by moving horizontally and vertically.
- The order of movement (horizontal then vertical or vice versa) is randomized to create varied road patterns.
-
Starting the Infection:
- The infection starts at the first city.
-
Spreading the Infection:
- A Breadth-First Search (BFS) algorithm is implemented using a queue to simulate the spread.
- Every 200 milliseconds, the infection spreads to adjacent roads and cities.
- Infected cells are marked and styled, and their neighbors are added to the queue.
-
Neighbor Detection:
- The
getNeighbors function retrieves adjacent cells (up, down, left, right) while ensuring they are within grid bounds.
Feel free to customize the simulator:
Enjoy the simulation! Watch as the infection spreads from one city to others along the connecting roads.