algoalgo-world
algoalgo-world/life
life/default

生命游戏

类型
二维元胞自动机
看的邻居
摩尔邻域,八格
规则
B3/S23
状态
2

选一种语言就能看到代码

def live_neighbors(cells, w, h, x, y):
    total = 0
    for dy in (-1, 0, 1):
        for dx in (-1, 0, 1):
            if dx == 0 and dy == 0:
                continue
            total += cells[(y + dy) % h * w + (x + dx) % w]
    return total


def step(cells, w, h):
    out = [0] * (w * h)
    for y in range(h):
        for x in range(w):
            live = live_neighbors(cells, w, h, x, y)
            alive = cells[y * w + x] == 1
            out[y * w + x] = 1 if live == 3 or (alive and live == 2) else 0
    return out
function liveNeighbors(cells, w, h, x, y) {
  let total = 0;
  for (let dy = -1; dy <= 1; dy++) {
    for (let dx = -1; dx <= 1; dx++) {
      if (dx === 0 && dy === 0) continue;
      const ny = (y + dy + h) % h;
      const nx = (x + dx + w) % w;
      total += cells[ny * w + nx];
    }
  }
  return total;
}

function step(cells, w, h) {
  const out = new Uint8Array(w * h);
  for (let y = 0; y < h; y++) {
    for (let x = 0; x < w; x++) {
      const live = liveNeighbors(cells, w, h, x, y);
      const alive = cells[y * w + x] === 1;
      out[y * w + x] = live === 3 || (alive && live === 2) ? 1 : 0;
    }
  }
  return out;
}
static int live_neighbors(const char cells[], int w, int h, int x, int y) {
    int total = 0;
    for (int dy = -1; dy <= 1; dy++) {
        for (int dx = -1; dx <= 1; dx++) {
            if (dx == 0 && dy == 0) continue;
            int ny = (y + dy + h) % h;
            int nx = (x + dx + w) % w;
            total += cells[ny * w + nx];
        }
    }
    return total;
}

void step(const char cells[], char out[], int w, int h) {
    for (int y = 0; y < h; y++) {
        for (int x = 0; x < w; x++) {
            int live = live_neighbors(cells, w, h, x, y);
            int alive = cells[y * w + x] == 1;
            out[y * w + x] = (live == 3 || (alive && live == 2)) ? 1 : 0;
        }
    }
}
static int live_neighbors(const std::vector<char>& cells, int w, int h,
                          int x, int y) {
    int total = 0;
    for (int dy = -1; dy <= 1; dy++) {
        for (int dx = -1; dx <= 1; dx++) {
            if (dx == 0 && dy == 0) continue;
            total += cells[((y + dy + h) % h) * w + (x + dx + w) % w];
        }
    }
    return total;
}

std::vector<char> step(const std::vector<char>& cells, int w, int h) {
    std::vector<char> out(cells.size(), 0);
    for (int y = 0; y < h; y++) {
        for (int x = 0; x < w; x++) {
            int live = live_neighbors(cells, w, h, x, y);
            bool alive = cells[y * w + x] == 1;
            out[y * w + x] = (live == 3 || (alive && live == 2)) ? 1 : 0;
        }
    }
    return out;
}
static int LiveNeighbors(byte[] cells, int w, int h, int x, int y) {
    int total = 0;
    for (int dy = -1; dy <= 1; dy++) {
        for (int dx = -1; dx <= 1; dx++) {
            if (dx == 0 && dy == 0) continue;
            total += cells[((y + dy + h) % h) * w + (x + dx + w) % w];
        }
    }
    return total;
}

static byte[] Step(byte[] cells, int w, int h) {
    byte[] next = new byte[w * h];
    for (int y = 0; y < h; y++) {
        for (int x = 0; x < w; x++) {
            int live = LiveNeighbors(cells, w, h, x, y);
            bool alive = cells[y * w + x] == 1;
            next[y * w + x] = (byte)(live == 3 || (alive && live == 2) ? 1 : 0);
        }
    }
    return next;
}
static int liveNeighbors(byte[] cells, int w, int h, int x, int y) {
    int total = 0;
    for (int dy = -1; dy <= 1; dy++) {
        for (int dx = -1; dx <= 1; dx++) {
            if (dx == 0 && dy == 0) continue;
            total += cells[((y + dy + h) % h) * w + (x + dx + w) % w];
        }
    }
    return total;
}

static byte[] step(byte[] cells, int w, int h) {
    byte[] out = new byte[w * h];
    for (int y = 0; y < h; y++) {
        for (int x = 0; x < w; x++) {
            int live = liveNeighbors(cells, w, h, x, y);
            boolean alive = cells[y * w + x] == 1;
            out[y * w + x] = (byte) (live == 3 || (alive && live == 2) ? 1 : 0);
        }
    }
    return out;
}
先跑一遍,再读源码