a world destruction algorithm in javascript

To create a world destruction algorithm in JavaScript, we'll need to create a simulation that models the effects of various destructive events. Here's an example algorithm that simulates an asteroid impact:

index.tsx
// Define the world parameters
const worldWidth = 1000;
const worldHeight = 1000;

// Define the asteroid parameters
const asteroidSize = 50;
const asteroidSpeed = 10;
const asteroidDamage = 100;

// Define the world array
const world = Array.from({ length: worldWidth }, () => Array.from({ length: worldHeight }, () => 0));

// Add an asteroid to the world
let asteroidX = Math.floor(Math.random() * worldWidth);
let asteroidY = Math.floor(Math.random() * worldHeight);
world[asteroidX][asteroidY] = asteroidSize;

// Define the simulation loop
const updateWorld = () => {
  // Move the asteroid
  asteroidX -= asteroidSpeed;
  
  // Check if the asteroid has hit the world
  if (asteroidX < 0 || asteroidX >= worldWidth || asteroidY < 0 || asteroidY >= worldHeight) {
    console.log("The asteroid missed the world!");
    clearInterval(simulationInterval);
    return;
  }
  
  // Check if the asteroid has hit any buildings
  const damageRadius = asteroidSize / 2;
  for (let x = asteroidX - damageRadius; x <= asteroidX + damageRadius; x++) {
    for (let y = asteroidY - damageRadius; y <= asteroidY + damageRadius; y++) {
      if (x >= 0 && x < worldWidth && y >= 0 && y < worldHeight && world[x][y] > 0) {
        world[x][y] -= asteroidDamage;
        if (world[x][y] < 0) {
          world[x][y] = 0;
        }
      }
    }
  }
  
  // Print the current state of the world
  console.clear();
  for (let y = 0; y < worldHeight; y++) {
    let row = "";
    for (let x = 0; x < worldWidth; x++) {
      if (world[x][y] >= 75) {
        row += "B"; // Building with health >= 75
      } else if (world[x][y] >= 50) {
        row += "b"; // Building with health >= 50
      } else if (world[x][y] >= 25) {
        row += "c"; // Building with health >= 25
      } else if (world[x][y] > 0) {
        row += "d"; // Building with health < 25
      } else {
        row += "."; // Empty space
      }
    }
    console.log(row);
  }
};

// Start the simulation loop
const simulationInterval = setInterval(updateWorld, 100);
2030 chars
66 lines

The above algorithm models the effects of an asteroid impact on a world represented as a 2D array. The algorithm randomly generates an asteroid and moves it across the world until it hits a building or misses the world entirely. If the asteroid hits a building, it damages the building and reduces its health. The algorithm then prints the current state of the world to the console, with each building represented by a letter ("B" for high health, "b" for medium health, "c" for low health, and "d" for very low health).

gistlibby LogSnag