cyclic/default
循环元胞自动机
- 类型
- 二维元胞自动机
- 看的邻居
- 摩尔邻域,八格
- 规则
- 只要有一个邻居已是自己的下一状态,就跟着走过去
- 状态
- 8
选一种语言就能看到代码
def any_neighbor(cells, w, h, x, y, want): 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] == want: return True return False def cyclic_step(cells, w, h, states): out = [0] * (w * h) for y in range(h): for x in range(w): here = cells[y * w + x] want = (here + 1) % states if any_neighbor(cells, w, h, x, y, want): out[y * w + x] = want else: out[y * w + x] = here return out
function anyNeighbor(cells, w, h, x, y, want) { 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] === want) return true; } } return false; } function cyclicStep(cells, w, h, states) { const out = new Uint8Array(w * h); for (let y = 0; y < h; y++) { for (let x = 0; x < w; x++) { const here = cells[y * w + x]; const want = (here + 1) % states; out[y * w + x] = anyNeighbor(cells, w, h, x, y, want) ? want : here; } } return out; }
static int any_neighbor(const char cells[], int w, int h, int x, int y, int want) { 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] == want) return 1; } } return 0; } void cyclic_step(const char cells[], char out[], int w, int h, int states) { for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { int here = cells[y * w + x]; int want = (here + 1) % states; int found = any_neighbor(cells, w, h, x, y, want); out[y * w + x] = found ? want : here; } } }
static bool any_neighbor(const std::vector<char>& cells, int w, int h, int x, int y, int want) { 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] == want) return true; } } return false; } std::vector<char> cyclic_step(const std::vector<char>& cells, int w, int h, int states) { std::vector<char> out(cells.size(), 0); for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { int here = cells[y * w + x]; int want = (here + 1) % states; bool found = any_neighbor(cells, w, h, x, y, want); out[y * w + x] = found ? want : here; } } return out; }
static bool AnyNeighbor(byte[] cells, int w, int h, int x, int y, int want) { 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] == want) return true; } } return false; } static byte[] CyclicStep(byte[] cells, int w, int h, int states) { byte[] next = new byte[w * h]; for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { int here = cells[y * w + x]; int want = (here + 1) % states; bool found = AnyNeighbor(cells, w, h, x, y, want); next[y * w + x] = (byte)(found ? want : here); } } return next; }
static boolean anyNeighbor(byte[] cells, int w, int h, int x, int y, int want) { 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] == want) return true; } } return false; } static byte[] cyclicStep(byte[] cells, int w, int h, int states) { byte[] out = new byte[w * h]; for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { int here = cells[y * w + x]; int want = (here + 1) % states; boolean found = anyNeighbor(cells, w, h, x, y, want); out[y * w + x] = (byte) (found ? want : here); } } return out; }