-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcell.js
More file actions
114 lines (98 loc) · 3.86 KB
/
cell.js
File metadata and controls
114 lines (98 loc) · 3.86 KB
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
var Web3 = require("web3");
var fs = require("fs");
web3 = new Web3(new Web3.providers.HttpProvider("http://dev.gol:8080"));
var gol;
account = '';
web3.eth.getAccounts().then(
function(accounts) {
account = accounts[0];
gol = new web3.eth.Contract(
JSON.parse(fs.readFileSync('GameOfLife.abi').toString()),
fs.readFileSync('GameOfLife.addr').toString(),
{from: account}
);
function loadCell(cell) {
return new web3.eth.Contract(
JSON.parse(fs.readFileSync('Cell.abi').toString()),
cell,
{from: account}
);
}
function getCellState(cell) {
return cell.methods.getState().call(function(err, res) {
if (err) console.log("Error: " + err)
console.log("Cell State: " + res)
return res;
});
}
function getCellNeighbours(cell) {
return cell.methods.getNeighbours().call(function(err, res) {
if (err) console.log("Error: " + err);
console.log("Cell Neighbours: ");
console.log(res);
});
}
function addCellNeighbour(cell, neighbour) {
cell.methods.addNeighbour(neighbour).send({from: account}, function(err) {
if (err) console.log("Error: " + err)
getCellNeighbours(cell)
})
}
function addCell(cell) {
gol.methods.addCell(cell).send({from: account, value: 50000}, function(error, res) {
getCells()
})
}
function changeState(newState) {
gol.methods.setState(newState).send({from: account, value: 50000}, function(error) {
getCells()
});
}
function getCells() {
gol.methods.getCells().call(function(err, res) {
console.log(err, res)
});
}
cells = fs.readFileSync('Cell.addr').toString().split("\n")
rows = [[], [], [], [], []]
neighbours = {}
for (var c=0; c<cells.length;c++){
neighbours[cells[c]] = []
}
for (var r=0; r<5; r++) {
for (var c=0;c<5;c++) {
rows[r][c] = cells[r*5+c];
}
}
// Build neighbour graph.
for (var r=0;r<5;r++) {
for (var c=0;c<5;c++) {
current = cells[r*5+c];
// Add above and below
if (rows[r-1] !== void 0) neighbours[current].push(rows[r-1][c])
if (rows[r+1] !== void 0) neighbours[current].push(rows[r+1][c])
// Add left and right of us.
if (rows[r][c-1] !== void 0) neighbours[current].push(rows[r][c-1])
if (rows[r][c+1] !== void 0) neighbours[current].push(rows[r][c+1])
// Add left above and below us.
if (rows[r-1] !== void 0 && rows[r-1][c-1] !== void 0) neighbours[current].push(rows[r-1][c-1])
if (rows[r+1] !== void 0 && rows[r+1][c-1] !== void 0) neighbours[current].push(rows[r+1][c-1])
// Add right above and below us.
if (rows[r-1] !== void 0 && rows[r-1][c+1] !== void 0) neighbours[current].push(rows[r-1][c+1])
if (rows[r+1] !== void 0 && rows[r+1][c+1] !== void 0) neighbours[current].push(rows[r+1][c+1])
}
}
// Add neighbours
if (false)
for (var addr in neighbours) {
for (var i=0; i<neighbours[addr].length;i++) {
cell = loadCell(addr);
addCellNeighbour(cell, neighbours[addr][i])
}
}
// Check that all went through (manual.)
for (var addr in cells) {
cell = loadCell(addr);
getCellNeighbours(cell)
}
});