-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.pde
133 lines (123 loc) · 3.09 KB
/
game.pde
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
/**
* @file game.pde
* @brief Resolution of the Conway's Game of Life problem @ OBI 2024 (P2)
* @author Pedro Henrique Pinto de Oliveira
* @date 2024-06-29
*/
/* Constants */
public static final int SUCCESS = 0;
public static int WINDOW_W;
public static int WINDOW_H;
public static final int MAX_N = 50;
public static final int MAX_Q = 100;
public static final int ALIVE = 1;
public static final int DEAD = 0;
public static final int BORDER = 2;
public static final int CHAR_INT_OFFSET = 48;
int [][]m = new int[MAX_N+BORDER][MAX_N+BORDER];
int [][]m1 = new int[MAX_N+BORDER][MAX_N+BORDER];
int Q = 0;
int N;
/* Functions */
/**
* (system) Allows defining the parameters to size() with a variable.
*/
void settings()
{
WINDOW_W = (displayHeight > 1280 ? 640 : displayHeight/2);
WINDOW_H = WINDOW_W;
size(WINDOW_W, WINDOW_H);
}
/**
* (system) Runs once when the program starts.
*/
void setup()
{
fill(255);
frameRate(30);
main();
}
/**
* (user) Draws a N*N matrix
* @param N Dimensions of the cell
*/
void drawSquareMatrix()
{
int w = WINDOW_W/N;
int h = WINDOW_H/N;
for(int i = 1; i < N+1; i++){
for(int j = 1; j < N+1; j++){
fill(255 - (m[i][j]*255));
stroke(84);
rect((j-1)*w, (i-1)*h, w, h); //<>//
}
}
}
/**
* (user) Updates the cell to the next state
* @param N Dimensions of the cell
*/
void nextCellState()
{
for(int i = 1; i < N; i++){
for(int j = 1; j < N+1; j++){
int sum = (m[i-1][j-1] + m[i-1][j] + m[i-1][j+1]) + (m[i][j-1] + m[i][j+1]) + (m[i+1][j-1] + m[i+1][j] + m[i+1][j+1]);
if(m[i][j] == DEAD){
m1[i][j] = (sum == 3 ? ALIVE : DEAD);
} else if (m[i][j] == ALIVE){
m1[i][j] = ( ((sum == 3) || (sum == 2)) ? ALIVE : DEAD );
}
}
}
// Shallow copy m1 to m
for(int i = 1; i < N+1; i++){
System.arraycopy(m1[i], 0, m[i], 0, N+1);
}
drawSquareMatrix();
}
/**
* (user) Runs the algorithm routines and coordinates function calls.
*/
int main()
{
// Parse input from file
int nRowsCols, nSteps;
String[] input = loadStrings("examples/game.txt");
String tmp = String.valueOf(input[0]);
String[] tokens = tmp.split(" ");
nRowsCols = Integer.valueOf(tokens[0]); N = nRowsCols;
if(tokens.length > 1){
nSteps = Integer.valueOf(tokens[1]); Q = nSteps;
}
// Receive initial state
for(int i = 0; i < nRowsCols; i++){
for(int j = 0; j < nRowsCols; j++){
char c = input[i+1].charAt(j); // skips first input line with nRowsCols and nSteps
m[i+1][j+1] = (int)(c-CHAR_INT_OFFSET); // considers northern and western borders
}
}
// Shallow copy m to m1
for(int i = 1; i < nRowsCols+1; i++){
System.arraycopy(m[i], 0, m1[i], 0, nRowsCols+1);
}
drawSquareMatrix();
return SUCCESS;
}
/**
* (system) continuously executes the instructions within, but is user-controlled via keyboard.
*/
void draw()
{
while(Q > 0)
{
print(Q + "\n");
Q--;
delay(1000);
nextCellState();
}
if(keyPressed){
nextCellState();
delay(100);
// default frame delay of 33.3ms too small, thus provoking multiple updates on a not fast enough key press + release
}
}