-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconwayLogic.cpp
74 lines (64 loc) · 2.18 KB
/
conwayLogic.cpp
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
#include <cheerp/client.h> //Misc client side stuff
#include <cheerp/clientlib.h>
#include <iostream>
[[cheerp::genericjs]] int conwayKernel(int **board, int iloc, int jloc,
int rows, int cols) {
int iMinLoc = iloc > 0 ? iloc - 1 : 0;
int iMaxLoc = iloc + 1 < rows ? iloc + 2 : rows;
int jMinLoc = jloc > 0 ? jloc - 1 : 0;
int jMaxLoc = jloc + 1 < cols ? jloc + 2 : cols;
int neighborCount = 0;
// std::cout << " | kernel at: " << iloc << ", " << jloc;
// std::cout << " range: " << iMinLoc << ", " << iMaxLoc << ", " << jMinLoc
// << ", " << jMaxLoc;
for (int i = iMinLoc; i < iMaxLoc; i++) {
for (int j = jMinLoc; j < jMaxLoc; j++) {
// std::cout << " ckeck " << i << ", " << j << ": ";
neighborCount += board[i][j];
// std::cout << neighborCount << "; ";
}
}
int isAlive = board[iloc][jloc] == 1;
if (isAlive) {
return (2 < neighborCount && neighborCount < 5);
}
return neighborCount == 3;
}
[[cheerp::genericjs]] int **getNextBoard(int **board, int rows, int cols) {
int **next = (int **)malloc(rows * sizeof(int *));
for (int i = 0; i < rows; i++) {
next[i] = (int *)malloc(cols * sizeof(int));
for (int j = 0; j < cols; j++) {
next[i][j] = conwayKernel(board, i, j, rows, cols);
// std::cout << next[i][j];
}
}
// std::cout << " end\n";
return next;
}
[[cheerp::genericjs]] int **getBoardDiff(int **prev, int **next, int rows,
int cols) {
int **diff = (int **)malloc(rows * sizeof(int *));
for (int i = 0; i < rows; i++) {
diff[i] = (int *)malloc(cols * sizeof(int));
for (int j = 0; j < cols; j++) {
diff[i][j] = prev[i][j] ^ next[i][j];
}
}
return diff;
}
[[cheerp::genericjs]] int **tick(int ***boardPtr, int rows, int cols) {
int **board = *boardPtr;
// std::cout << "tick \n";
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
// std::cout << board[i][j] << ", ";
}
// std::cout << "\n";
}
int **nextBoard, **diff;
nextBoard = getNextBoard(board, rows, cols);
diff = getBoardDiff(board, nextBoard, rows, cols);
*boardPtr = nextBoard;
return diff;
}