about summary refs log tree commit diff stats
path: root/linux/123slice.subx
Commit message (Expand)AuthorAgeFilesLines
* 7842 - new directory organizationKartik K. Agaram2021-03-031-0/+1220
15' href='#n15'>15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87






















































































                                                                                                                          
'use strict'

const b = {
    curry: function (fn) {
        const curried = (...args) => {
            if (args.length >= fn.length)
                return fn(...args)
            else
                return (...rest) => curried(...args, ...rest)
        }
        return curried
    },
    pipe: (...fns) => (value) => fns.reduce((acc, fn) => fn(acc), value),
};

const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const startPauseButton = document.getElementById('startPauseButton');

canvas.width = window.innerWidth;
canvas.height = window.innerHeight;

const cellSize = 10;
const rows = Math.floor(canvas.height / cellSize);
const cols = Math.floor(canvas.width / cellSize);

let grid = initializeGrid();
let animationId = null;

function initializeGrid() {
  return Array.from({ length: rows }, () => Array.from({ length: cols }, () => Math.random() > 0.8 ? 1 : 0));
}

const drawCell = b.curry((ctx, cellSize, x, y, cell) => {
  ctx.fillStyle = cell ? 'black' : 'white';
  ctx.fillRect(x * cellSize, y * cellSize, cellSize, cellSize);
  ctx.strokeStyle = 'gray';
  ctx.strokeRect(x * cellSize, y * cellSize, cellSize, cellSize);
});

const drawGrid = (ctx, cellSize, grid) => {
  ctx.clearRect(0, 0, canvas.width, canvas.height);
  grid.forEach((row, y) => row.forEach((cell, x) => drawCell(ctx, cellSize, x, y, cell)));
};

const getNeighbors = (grid, x, y) => [
  grid[y - 1]?.[x - 1], grid[y - 1]?.[x], grid[y - 1]?.[x + 1],
  grid[y]?.[x - 1], /* cell */ grid[y]?.[x + 1],
  grid[y + 1]?.[x - 1], grid[y + 1]?.[x], grid[y + 1]?.[x + 1]
].filter(Boolean).reduce((acc, val) => acc + val, 0);

const nextCellState = b.curry((cell, neighbors) => (cell && neighbors === 2) || neighbors === 3 ? 1 : 0);

const nextGridState = (grid) => grid.map((row, y) => row.map((cell, x) => nextCellState(cell, getNeighbors(grid, x, y))));

const animate = () => {
  drawGrid(ctx, cellSize, grid);
  grid = nextGridState(grid);
  animationId = requestAnimationFrame(animate);
};

const toggleAnimation = () => {
  if (animationId) {
    cancelAnimationFrame(animationId);
    animationId = null;
    startPauseButton.textContent = 'Start';
  } else {
    animate();
    startPauseButton.textContent = 'Pause';
  }
};

startPauseButton.addEventListener('click', toggleAnimation);

canvas.addEventListener('click', (event) => {
  const rect = canvas.getBoundingClientRect();
  const x = Math.floor((event.clientX - rect.left) / cellSize);
  const y = Math.floor((event.clientY - rect.top) / cellSize);
  grid[y][x] = grid[y][x] ? 0 : 1;
  drawGrid(ctx, cellSize, grid);
});

const startGame = b.pipe(initializeGrid, (grid) => {
  drawGrid(ctx, cellSize, grid);
  return grid;
});
grid = startGame();