Advertisement
sci4me

RIP John Conway

Nov 1st, 2019
367
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. GRID_WIDTH :: WIDTH / CELL_SIZE;
  2. GRID_HEIGHT :: HEIGHT / CELL_SIZE;
  3. CELL_COUNT :: GRID_WIDTH * GRID_HEIGHT;
  4. GRID_COUNT :: CELL_COUNT / 2;
  5.  
  6. Cell :: bit_field {
  7. a : 1,
  8. an : 3,
  9. b : 1,
  10. bn : 3
  11. }
  12.  
  13. grid : [GRID_COUNT]Cell;
  14. LUT : [256]Cell;
  15.  
  16. init :: proc() {
  17. for i in 0..255 {
  18. a := transmute(Cell) u8(i);
  19.  
  20. b : Cell;
  21.  
  22. b.an = 0;
  23. b.bn = 0;
  24.  
  25. if a.a == 1 {
  26. b.a = a.an == 2 || a.an == 3;
  27. } else {
  28. b.a = a.an == 3;
  29. }
  30.  
  31. if a.b == 1 {
  32. b.b = a.bn == 2 || a.bn == 3;
  33. } else {
  34. b.b = a.bn == 3;
  35. }
  36.  
  37. LUT[i] = b;
  38. }
  39.  
  40. for i in 0..<GRID_COUNT {
  41. grid[i].a = rand.float32() < 0.5 ? 1 : 0;
  42. grid[i].b = rand.float32() < 0.5 ? 1 : 0;
  43. }
  44. }
  45.  
  46. compute_neighbors :: proc() {
  47. for y in 0..<GRID_HEIGHT {
  48. for x in 0..<GRID_WIDTH {
  49. n := 0;
  50.  
  51. for j in -1..1 {
  52. for i in -1..1 {
  53. if i == 0 && j == 0 do continue;
  54.  
  55. k := x + i;
  56. l := y + j;
  57.  
  58. if k < 0 do k += GRID_WIDTH;
  59. if l < 0 do l += GRID_HEIGHT;
  60. if k >= GRID_WIDTH do k -= GRID_WIDTH;
  61. if l >= GRID_HEIGHT do l -= GRID_HEIGHT;
  62.  
  63. m := k + l * GRID_WIDTH;
  64. c := grid[m / 2];
  65. if m % 2 == 0 {
  66. if c.a == 1 do n += 1;
  67. } else {
  68. if c.b == 1 do n += 1;
  69. }
  70. }
  71. }
  72.  
  73. assert(n >= 0 && n <= 8);
  74.  
  75. m := x + y * GRID_WIDTH;
  76. if m % 2 == 0 {
  77. grid[m / 2].an = n;
  78. } else {
  79. grid[m / 2].bn = n;
  80. }
  81. }
  82. }
  83. }
  84.  
  85. flip :: proc() {
  86. for i in 0..<GRID_COUNT {
  87. fmt.print(grid[i], " -> ");
  88. grid[i] = LUT[transmute(u8)grid[i]];
  89. fmt.println(grid[i]);
  90. }
  91. }
  92.  
  93. tick :: proc() {
  94. compute_neighbors();
  95. flip();
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement