# Game of Life glider

Count the eight neighbors of each cell before updating the whole board. An empty cell is born with three neighbors. A live cell survives with two or three. After four generations the five-cell glider has shifted down and right by one cell.

## Try it

Advance four steps and compare the initial and current grids. Choose Blinker to see three cells return to the same positions every two steps.

## Interaction details

The 17×10 grid wraps at the edges. Every new cell state is calculated from the preceding generation.

## Platform controls

- iOS · Tap One step or Four steps. Choose Glider or Blinker to change the initial pattern.
- Android · Tap One step or Four steps. Choose Glider or Blinker to change the initial pattern.
- PC · Tab to One step or Four steps and press Enter. Reset restores the selected seed.

## Where to use it

Show how a local rule gives rise to moving and oscillating patterns.

## Implementation prompt

> Simulate a glider and a blinker with the same Life rule. Show the initial grid beside the current one, with one-step and four-step controls for comparing shape and position.

## Sources and credits

[Golly · QuickLife rules](<https://golly.sourceforge.io/Help/Algorithms/QuickLife.html>)

## Standalone HTML

Save the code below as an HTML file and open it in your browser. The example works offline.

```html
<!doctype html>
<html lang="en" data-motion-locales="inline">
<head><script>if(new URLSearchParams(location.search).get('reduce')==='1')document.documentElement.classList.add('motion-reduced');</script>
<meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
<title>네 세대 뒤의 글라이더</title>
<style>
*{box-sizing:border-box}
:root{font:15px/1.4 system-ui,"Malgun Gothic",sans-serif;color:#273f34;background:#eff1e8;color-scheme:light;--accent:#476749}
body{margin:0;padding:clamp(10px,3vw,26px);min-height:100svh;display:grid;align-content:center}
main{width:min(100%,740px);margin:auto}
header,.readout{display:flex;align-items:baseline;justify-content:space-between;gap:8px 18px;flex-wrap:wrap}
header{margin:0 0 9px}h1{margin:0;font-size:clamp(20px,4vw,33px);letter-spacing:-.05em;line-height:1.12}
header small{font:650 12px/1.3 ui-monospace,monospace}
figure{margin:0}canvas{display:block;width:100%;height:auto;aspect-ratio:700/250;border-radius:7px;background:#21372f}
.readout{padding:6px 0;font:650 clamp(11px,1.8vw,14px)/1.35 ui-monospace,monospace}
.controls{display:flex;gap:6px 9px;align-items:center;flex-wrap:wrap;border-top:1px solid currentColor;padding-top:8px}
button{min-height:35px;padding:7px 12px;border:1px solid currentColor;background:transparent;color:inherit;border-radius:5px;cursor:pointer;font:650 13px/1.2 system-ui,sans-serif}
button:active,button[aria-pressed="true"]{color:white;background:var(--accent);border-color:var(--accent)}
button:focus-visible,input:focus-visible{outline:3px solid var(--accent);outline-offset:3px}
label{display:flex;align-items:center;gap:7px;flex:1 1 124px;min-width:0;font-size:12px;font-weight:650}
label span{flex:none;white-space:nowrap}
input[type="range"]{accent-color:var(--accent);flex:1 1 70px;min-width:55px;width:auto;margin:0}
@media(max-width:450px){header small{font-size:10px}.readout{gap:3px 7px}button{min-height:36px;padding:6px 9px}.controls{gap:5px}.controls label{flex-basis:100%}}
</style></head>
<body><main><header><h1 data-t="title"></h1><small data-t="method"></small></header>
<figure><canvas id="board" width="700" height="250" role="img"></canvas>
<figcaption class="readout"><span id="status"></span><output id="reading"></output></figcaption></figure>
<div class="controls"><button id="glider" data-t="glider"></button><button id="blinker" data-t="blinker"></button><button id="next" data-t="step"></button><button id="four" data-t="four"></button><button id="reset" data-t="reset"></button></div></main>
<script>
'use strict';
const words={
 "title": [
  "네 세대 뒤의 글라이더",
  "A glider in four generations",
  "Un planeador en cuatro generaciones",
  "四代之后的滑翔机"
 ],
 "method": [
  "콘웨이의 라이프 게임",
  "Conway’s Game of Life",
  "Juego de la vida de Conway",
  "康威生命游戏"
 ],
 "description": [
  "왼쪽의 시작 격자와 오른쪽의 현재 격자에서 켜진 칸을 비교하는 보드",
  "Compare the occupied cells on the initial and current grids",
  "Compara las células vivas en la cuadrícula inicial y la actual",
  "比较初始网格和当前网格中的存活格子"
 ],
 "start": [
  "처음",
  "Initial",
  "Inicial",
  "初始"
 ],
 "now": [
  "현재",
  "Current",
  "Actual",
  "当前"
 ],
 "gen": [
  "세대",
  "Generation",
  "Generación",
  "代数"
 ],
 "cells": [
  "살아 있는 칸",
  "Live cells",
  "Células vivas",
  "存活格"
 ],
 "glider": [
  "글라이더",
  "Glider",
  "Planeador",
  "滑翔机"
 ],
 "blinker": [
  "깜빡이",
  "Blinker",
  "Parpadeador",
  "闪烁器"
 ],
 "step": [
  "한 세대",
  "One step",
  "Un paso",
  "下一代"
 ],
 "four": [
  "네 세대",
  "Four steps",
  "Cuatro pasos",
  "前进四代"
 ],
 "reset": [
  "처음으로",
  "Reset",
  "Reiniciar",
  "重置"
 ]
};
const languages=["ko","en","es","zh-Hans"];
const params=new URLSearchParams(location.search);
const preferred=params.get('lang')||document.documentElement.lang;
const code=languages.includes(preferred)?preferred:'ko';
const tr=k=>words[k][languages.indexOf(code)];
const by=id=>document.getElementById(id);
document.documentElement.lang=code;
document.title=tr('title');
document.querySelectorAll('[data-t]').forEach(el=>el.textContent=tr(el.dataset.t));
const board=by('board'),ctx=board.getContext('2d');
board.setAttribute('aria-label',tr('description'));
const reduced=()=>params.get('reduce')==='1'||document.documentElement.classList.contains('motion-reduced')||matchMedia('(prefers-reduced-motion: reduce)').matches;
let animation=0;
function animate(from,to,duration,render){
  cancelAnimationFrame(animation);
  if(reduced()||from===to){render(to);return;}
  const start=performance.now();
  function frame(now){
    const fraction=Math.min(1,(now-start)/duration);
    render(from+(to-from)*fraction);
    if(fraction<1)animation=requestAnimationFrame(frame);
  }
  animation=requestAnimationFrame(frame);
}

const width=17,height=10,cell=17;
const seeds={glider:[[7,3],[8,4],[6,5],[7,5],[8,5]],blinker:[[6,4],[7,4],[8,4]]};
let kind='glider',generation=0,alive=new Set(),original=new Set(),timer=0;
const idx=(x,y)=>((y+height)%height)*width+(x+width)%width;
function grid(originX,pattern,compare,label){
 ctx.fillStyle='#d6e6c1';ctx.font='600 14px ui-monospace,monospace';ctx.fillText(label,originX,29);
 for(let y=0;y<height;y++)for(let x=0;x<width;x++){
   const n=idx(x,y),u=originX+x*cell,v=42+y*cell;
   ctx.fillStyle=pattern.has(n)?(compare&&!original.has(n)?'#f1b277':'#d3eb95'):'#324b40';
   ctx.fillRect(u+1,v+1,cell-2,cell-2);
   if(compare&&original.has(n)&&!pattern.has(n)){ctx.strokeStyle='#88a687';ctx.strokeRect(u+3.5,v+3.5,cell-7,cell-7);}
 }
}
function draw(){
 ctx.fillStyle='#21372f';ctx.fillRect(0,0,700,250);
 grid(31,original,false,tr('start'));grid(379,alive,true,tr('now'));
 ctx.fillStyle='#9aad91';ctx.font='21px Georgia,serif';ctx.fillText('→',337,135);
 for(const id of ['glider','blinker'])by(id).setAttribute('aria-pressed',String(id===kind));
 by('status').textContent=tr('gen')+' '+generation;by('reading').textContent=tr('cells')+'  '+alive.size;
 board.dataset.generation=generation;board.dataset.seed=kind;board.dataset.cells=[...alive].sort((a,b)=>a-b).join(',');
}
function step(){
 const born=new Set();
 for(let y=0;y<height;y++)for(let x=0;x<width;x++){
   let count=0;for(let v=-1;v<=1;v++)for(let u=-1;u<=1;u++)if((u||v)&&alive.has(idx(x+u,y+v)))count++;
   const n=idx(x,y);
   if(count===3||(count===2&&alive.has(n)))born.add(n);
 }
 alive=born;generation++;draw();
}
function reset(k){clearTimeout(timer);kind=k;generation=0;original=new Set(seeds[k].map(([x,y])=>idx(x,y)));alive=new Set(original);draw();}
by('glider').onclick=()=>reset('glider');by('blinker').onclick=()=>reset('blinker');by('reset').onclick=()=>reset(kind);
by('next').onclick=()=>{clearTimeout(timer);step();};
by('four').onclick=()=>{
 clearTimeout(timer);
 if(reduced()){for(let i=0;i<4;i++)step();return;}
 let remaining=4;function run(){step();if(--remaining)timer=setTimeout(run,220);}run();
};
reset(kind);
</script></body></html>
```
