summaryrefslogtreecommitdiff
path: root/src/simulation/lattice_rules.test.js
blob: c2857114eb120d226756e9ee860e188c40e2baaf (plain)
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import { world_update } from '../world/world.js';
import { lattice_rules } from './lattice_rules.js';


function apply(f, n, x0) {
  if (n == 0) {
    return x0;
  } else {
    return f(apply(f, n-1, x0));
  }
}


// remove cell team information
function clean_team(lattice) {
  return lattice.map(row => row.map(cell => ({...cell, flags: {...cell.flags, team: undefined } })));
}


test("blinker", () => {
  const L = { type: 'active', flags: { team: undefined, } };
  const _ = { type: 'empty', flags: { team: undefined, } };
  const l = { type: 'active', flags: { team: undefined,  emit: [0, 0, 0, 0, 0, 0, 0, 1] } };
  const d = { type: 'empty', flags: { team: undefined,  emit: [0, 0, 0, 0, 0, 0, 0, -1] } };
  const lattice = [
    [ _, _, _, _, _ ],
    [ _, _, _, _, _ ],
    [ _, L, L, L, _ ],
    [ _, _, _, _, _ ],
    [ _, _, _, _, _ ],
  ];

  const world = { lattice, lattice_rules, agents: [], senses: [], actions: [], validity: [] };
  expect(clean_team(world_update(world).lattice)).toEqual([
    [ _, _, _, _, _ ],
    [ _, _, l, _, _ ],
    [ _, d, L, d, _ ],
    [ _, _, l, _, _ ],
    [ _, _, _, _, _ ],
  ]);
  expect(clean_team(world_update(world_update(world)).lattice)).toEqual([
    [ _, _, _, _, _ ],
    [ _, _, d, _, _ ],
    [ _, l, L, l, _ ],
    [ _, _, d, _, _ ],
    [ _, _, _, _, _ ],
  ]);
});


test("glider", () => {
  const L = { type: 'active', flags: { team: undefined, } };
  const _ = { type: 'empty', flags: { team: undefined, } };
  const l = { type: 'active', flags: { team: undefined,  emit: [0, 0, 0, 0, 0, 0, 0, 1] } };
  const d = { type: 'empty', flags: { team: undefined,  emit: [0, 0, 0, 0, 0, 0, 0, -1] } };
  const lattice = [
    [ _, _, _, _, _, _ ],
    [ _, _, _, L, _, _ ],
    [ _, L, _, L, _, _ ],
    [ _, _, L, L, _, _ ],
    [ _, _, _, _, _, _ ],
    [ _, _, _, _, _, _ ],
  ];

  const world = { lattice, lattice_rules, agents: [], senses: [], actions: [], validity: [] };
  //expect(clean_team(world_update(world).lattice)).toEqual([
  expect(clean_team(apply(world_update, 1, world).lattice)).toEqual([
    [ _, _, _, _, _, _ ],
    [ _, _, l, d, _, _ ],
    [ _, d, _, L, l, _ ],
    [ _, _, L, L, _, _ ],
    [ _, _, _, _, _, _ ],
    [ _, _, _, _, _, _ ],
  ]);
  expect(clean_team(apply(world_update, 2, world).lattice)).toEqual([
    [ _, _, _, _, _, _ ],
    [ _, _, d, l, _, _ ],
    [ _, _, _, d, L, _ ],
    [ _, _, L, L, l, _ ],
    [ _, _, _, _, _, _ ],
    [ _, _, _, _, _, _ ],
  ]);
  expect(clean_team(apply(world_update, 3, world).lattice)).toEqual([
    [ _, _, _, _, _, _ ],
    [ _, _, _, d, _, _ ],
    [ _, _, l, _, L, _ ],
    [ _, _, d, L, L, _ ],
    [ _, _, _, l, _, _ ],
    [ _, _, _, _, _, _ ],
  ]);
  expect(clean_team(apply(world_update, 4, world).lattice)).toEqual([
    [ _, _, _, _, _, _ ],
    [ _, _, _, _, _, _ ],
    [ _, _, d, _, L, _ ],
    [ _, _, l, d, L, _ ],
    [ _, _, _, L, l, _ ],
    [ _, _, _, _, _, _ ],
  ]);
});


test("beehive", () => {
  const L = { type: 'active', flags: { team: undefined, } };
  const _ = { type: 'empty', flags: { team: undefined, } };
  const lattice = [
    [ _, _, _, _, _, _ ],
    [ _, _, L, L, _, _ ],
    [ _, L, _, _, L, _ ],
    [ _, _, L, L, _, _ ],
    [ _, _, _, _, _, _ ],
  ];

  const world = { lattice, lattice_rules, agents: [], senses: [], actions: [], validity: [] };
  //expect(clean_team(world_update(world).lattice)).toEqual([
  expect(clean_team(apply(world_update, 1, world).lattice)).toEqual(lattice);
});


test("mutables are activated by neighboring actives", () => {
  const L = { type: 'active', flags: { team: undefined, } };
  const _ = { type: 'empty', flags: { team: undefined, } };
  const l = { type: 'active', flags: { team: undefined,  emit: [0, 0, 0, 0, 0, 0, 0, 1] } };
  const d = { type: 'empty', flags: { team: undefined,  emit: [0, 0, 0, 0, 0, 0, 0, -1] } };
  const M = { type: 'mutable', flags: { team: undefined, } };

  const lattice = [
    [ _, _, _, _, ],
    [ _, L, M, _, ],
    [ _, M, M, _, ],
    [ _, _, _, _, ],
  ];

  const world = { lattice, lattice_rules, agents: [], senses: [], actions: [], validity: [] };

  expect(clean_team(apply(world_update, 1, world).lattice)).toEqual([
    [ _, _, _, _, ],
    [ _, L, l, _, ],
    [ _, l, l, _, ],
    [ _, _, _, _, ],
  ]);
  expect(clean_team(apply(world_update, 2, world).lattice)).toEqual([
    [ _, _, _, _, ],
    [ _, L, L, _, ],
    [ _, L, L, _, ],
    [ _, _, _, _, ],
  ]);
});