brians-brain/default
布莱恩的大脑
- 类型
- 二维元胞自动机
- 看的邻居
- 摩尔邻域,八格
- 规则
- 亮着的格必定转为余烬,余烬必定熄灭
- 状态
- 3
选一种语言就能看到代码
DEAD, ON, DYING = 0, 1, 2 def on_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 if cells[(y + dy) % h * w + (x + dx) % w] == ON: total += 1 return total def brains_step(cells, w, h): out = [DEAD] * (w * h) for y in range(h): for x in range(w): state = cells[y * w + x] if state == ON: out[y * w + x] = DYING elif state == DEAD and on_neighbors(cells, w, h, x, y) == 2: out[y * w + x] = ON return out
const DEAD = 0; const ON = 1; const DYING = 2; function onNeighbors(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 at = ((y + dy + h) % h) * w + ((x + dx + w) % w); if (cells[at] === ON) total++; } } return total; } function brainsStep(cells, w, h) { const out = new Uint8Array(w * h); for (let y = 0; y < h; y++) { for (let x = 0; x < w; x++) { const state = cells[y * w + x]; if (state === ON) out[y * w + x] = DYING; else if (state === DEAD && onNeighbors(cells, w, h, x, y) === 2) { out[y * w + x] = ON; } } } return out; }
#define DEAD 0 #define ON 1 #define DYING 2 static int on_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 at = ((y + dy + h) % h) * w + (x + dx + w) % w; if (cells[at] == ON) total++; } } return total; } void brains_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 state = cells[y * w + x]; int live = on_neighbors(cells, w, h, x, y); char next = DEAD; if (state == ON) next = DYING; else if (state == DEAD && live == 2) next = ON; out[y * w + x] = next; } } }
enum Cell { DEAD = 0, ON = 1, DYING = 2 }; static int on_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; int at = ((y + dy + h) % h) * w + (x + dx + w) % w; if (cells[at] == ON) total++; } } return total; } std::vector<char> brains_step(const std::vector<char>& cells, int w, int h) { std::vector<char> out(cells.size(), DEAD); for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { int state = cells[y * w + x]; if (state == ON) out[y * w + x] = DYING; else if (state == DEAD && on_neighbors(cells, w, h, x, y) == 2) out[y * w + x] = ON; } } return out; }
const int Dead = 0; const int On = 1; const int Dying = 2; static int OnNeighbors(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; int at = ((y + dy + h) % h) * w + (x + dx + w) % w; if (cells[at] == On) total++; } } return total; } static byte[] BrainsStep(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 state = cells[y * w + x]; if (state == On) next[y * w + x] = Dying; else if (state == Dead && OnNeighbors(cells, w, h, x, y) == 2) { next[y * w + x] = On; } } } return next; }
static final int DEAD = 0; static final int ON = 1; static final int DYING = 2; static int onNeighbors(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; int at = ((y + dy + h) % h) * w + (x + dx + w) % w; if (cells[at] == ON) total++; } } return total; } static byte[] brainsStep(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 state = cells[y * w + x]; if (state == ON) out[y * w + x] = DYING; else if (state == DEAD && onNeighbors(cells, w, h, x, y) == 2) { out[y * w + x] = ON; } } } return out; }