Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- GRID_WIDTH :: WIDTH / CELL_SIZE;
- GRID_HEIGHT :: HEIGHT / CELL_SIZE;
- CELL_COUNT :: GRID_WIDTH * GRID_HEIGHT;
- GRID_COUNT :: CELL_COUNT / 2;
- Cell :: bit_field {
- a : 1,
- an : 3,
- b : 1,
- bn : 3
- }
- grid : [GRID_COUNT]Cell;
- LUT : [256]Cell;
- init :: proc() {
- for i in 0..255 {
- a := transmute(Cell) u8(i);
- b : Cell;
- b.an = 0;
- b.bn = 0;
- if a.a == 1 {
- b.a = a.an == 2 || a.an == 3;
- } else {
- b.a = a.an == 3;
- }
- if a.b == 1 {
- b.b = a.bn == 2 || a.bn == 3;
- } else {
- b.b = a.bn == 3;
- }
- LUT[i] = b;
- }
- for i in 0..<GRID_COUNT {
- grid[i].a = rand.float32() < 0.5 ? 1 : 0;
- grid[i].b = rand.float32() < 0.5 ? 1 : 0;
- }
- }
- compute_neighbors :: proc() {
- for y in 0..<GRID_HEIGHT {
- for x in 0..<GRID_WIDTH {
- n := 0;
- for j in -1..1 {
- for i in -1..1 {
- if i == 0 && j == 0 do continue;
- k := x + i;
- l := y + j;
- if k < 0 do k += GRID_WIDTH;
- if l < 0 do l += GRID_HEIGHT;
- if k >= GRID_WIDTH do k -= GRID_WIDTH;
- if l >= GRID_HEIGHT do l -= GRID_HEIGHT;
- m := k + l * GRID_WIDTH;
- c := grid[m / 2];
- if m % 2 == 0 {
- if c.a == 1 do n += 1;
- } else {
- if c.b == 1 do n += 1;
- }
- }
- }
- assert(n >= 0 && n <= 8);
- m := x + y * GRID_WIDTH;
- if m % 2 == 0 {
- grid[m / 2].an = n;
- } else {
- grid[m / 2].bn = n;
- }
- }
- }
- }
- flip :: proc() {
- for i in 0..<GRID_COUNT {
- fmt.print(grid[i], " -> ");
- grid[i] = LUT[transmute(u8)grid[i]];
- fmt.println(grid[i]);
- }
- }
- tick :: proc() {
- compute_neighbors();
- flip();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement