about summary refs log tree commit diff stats
path: root/js/baba-yaga/scratch/baba/game-of-life.baba
blob: 2721b3e59d821f2a2e726d0e78f874d6b58954f8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Conway's Game of Life - Working Implementation

// Count neighbors for a 3x3 grid (hardcoded positions)
countNeighbors : grid row col ->
  when row is
    0 then when col is
      0 then grid.0.1 + grid.1.0 + grid.1.1
      1 then grid.0.0 + grid.0.2 + grid.1.0 + grid.1.1 + grid.1.2
      2 then grid.0.1 + grid.1.1 + grid.1.2
      _ then 0
    1 then when col is
      0 then grid.0.0 + grid.0.1 + grid.1.1 + grid.2.0 + grid.2.1
      1 then grid.0.0 + grid.0.1 + grid.0.2 + grid.1.0 + grid.1.2 + grid.2.0 + grid.2.1 + grid.2.2
      2 then grid.0.1 + grid.0.2 + grid.1.1 + grid.2.1 + grid.2.2
      _ then 0
    2 then when col is
      0 then grid.1.0 + grid.1.1 + grid.2.1
      1 then grid.1.0 + grid.1.1 + grid.1.2 + grid.2.0 + grid.2.2
      2 then grid.1.1 + grid.1.2 + grid.2.1
      _ then 0
    _ then 0;

// Apply Game of Life rules
nextCell : grid row col ->
  with (
    current : when row is
      0 then when col is 0 then grid.0.0 1 then grid.0.1 2 then grid.0.2 _ then 0
      1 then when col is 0 then grid.1.0 1 then grid.1.1 2 then grid.1.2 _ then 0
      2 then when col is 0 then grid.2.0 1 then grid.2.1 2 then grid.2.2 _ then 0
      _ then 0;
    neighbors : countNeighbors grid row col;
  ) ->
    when current is
      1 then when (neighbors = 2 or neighbors = 3) is true then 1 _ then 0
      _ then when (neighbors = 3) is true then 1 _ then 0;

// Generate next generation for 3x3 grid
step : grid -> {
  0: {
    0: nextCell grid 0 0,
    1: nextCell grid 0 1,
    2: nextCell grid 0 2
  },
  1: {
    0: nextCell grid 1 0,
    1: nextCell grid 1 1,
    2: nextCell grid 1 2
  },
  2: {
    0: nextCell grid 2 0,
    1: nextCell grid 2 1,
    2: nextCell grid 2 2
  }
};

// Blinker pattern (oscillator)
blinker : {
  0: { 0: 0, 1: 1, 2: 0 },
  1: { 0: 0, 1: 1, 2: 0 },
  2: { 0: 0, 1: 1, 2: 0 }
};

// Run simulation
io.out "Conway's Game of Life - Blinker Pattern";
io.out "Generation 0:";
io.out blinker;

gen1 : step blinker;
io.out "Generation 1:";
io.out gen1;

gen2 : step gen1;
io.out "Generation 2:";
io.out gen2;

io.out "Complete!";