generated from nighthawkcoders/spring_portfolio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LightBoard.java
337 lines (312 loc) · 13.5 KB
/
LightBoard.java
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
package com.nighthawk.spring_portfolio.mvc.lightboard;
import lombok.Data;
@Data // Annotations to simplify writing code (ie constructors, setters)
public class LightBoard {
private Light[][] lights;
/* Initialize LightBoard and Lights */
public LightBoard(int numRows, int numCols) {
this.lights = new Light[numRows][numCols];
// 2D array nested loops, used for initialization
for (int row = 0; row < numRows; row++) {
for (int col = 0; col < numCols; col++) {
lights[row][col] = new Light(); // each cell needs to be constructed
}
}
}
public LightBoard(double[][][] rgbVals) {
this.lights = new Light[rgbVals.length][rgbVals[0].length];
// 2D array nested loops, used for initialization
for (int row = 0; row < rgbVals.length; row++) {
for (int col = 0; col < rgbVals[0].length; col++) {
lights[row][col] = new Light(rgbVals[row][col][0],rgbVals[row][col][1],rgbVals[row][col][2],rgbVals[row][col][3]); // each cell needs to be constructed
}
}
}
/* Overload constructor to take in input Light[][] */
public LightBoard(Light[][] lights) {
this.lights = lights;
}
public void lightSwitch(int row, int col) {
this.lights[row][col].lightSwitch();
}
/* Output is intended for API key/values */
public String toString() {
String outString = "[";
// 2D array nested loops, used for reference
for (int row = 0; row < lights.length; row++) {
for (int col = 0; col < lights[row].length; col++) {
outString +=
// data
"{" +
"\"row\": " + row + "," +
"\"column\": " + col + "," +
"\"light\": " + lights[row][col] + // extract toString data
"}," ;
}
}
// remove last comma, newline, add square bracket, reset color
outString = outString.substring(0,outString.length() - 1) + "]";
return outString;
}
/* Output is intended for Terminal, effects added to output */
public String toTerminal() {
String outString = "[";
// 2D array nested loops, used for reference
for (int row = 0; row < lights.length; row++) {
for (int col = 0; col < lights[row].length; col++) {
outString +=
// reset
"\033[m" +
// color
"\033[38;2;" +
lights[row][col].getRed() + ";" + // set color using getters
lights[row][col].getGreen() + ";" +
lights[row][col].getBlue() + ";" +
lights[row][col].getEffect() + "m" +
// data, extract custom getters
"{" +
"\"" + "RGB\": " + "\"" + lights[row][col].getRGB() + "\"" +
"," +
"\"" + "Effect\": " + "\"" + lights[row][col].getEffectTitle() + "\"" +
"}," +
// newline
"\n" ;
}
}
// remove last comma, newline, add square bracket, reset color
outString = outString.substring(0,outString.length() - 2) + "\033[m" + "]";
return outString;
}
/* Output is intended for Terminal, draws color palette */
public String toColorPalette() {
// block sizes
final int ROWS = 5;
final int COLS = 10;
// Build large string for entire color palette
String outString = "";
// find each row
for (int row = 0; row < lights.length; row++) {
// repeat each row for block size
for (int i = 0; i < ROWS; i++) {
// find each column
for (int col = 0; col < lights[row].length; col++) {
// repeat each column for block size
for (int j = 0; j < COLS; j++) {
// print single character, except at midpoint print color code
String c = (i == (int) (ROWS / 2) && j == (int) (COLS / 2) )
? lights[row][col].getRGB()
: (j == (int) (COLS / 2)) // nested ternary
? " ".repeat(lights[row][col].getRGB().length())
: " ";
outString +=
// reset
"\033[m" +
// color
"\033[38;2;" +
lights[row][col].getRed() + ";" +
lights[row][col].getGreen() + ";" +
lights[row][col].getBlue() + ";" +
"7m" +
// color code or blank character
c +
// reset
"\033[m";
}
}
outString += "\n";
}
}
// remove last comma, newline, add square bracket, reset color
outString += "\033[m";
return outString;
}
public String toColorPalette(int ROWS, int COLS) {
// Build large string for entire color palette
String outString = "";
// find each row
for (int row = 0; row < lights.length; row++) {
// repeat each row for block size
for (int i = 0; i < ROWS; i++) {
// find each column
for (int col = 0; col < lights[row].length; col++) {
// repeat each column for block size
for (int j = 0; j < COLS; j++) {
// print single character, except at midpoint print color code
String c = (i == (int) (ROWS / 2) && j == (int) (COLS / 2) )
? lights[row][col].getRGB()
: (j == (int) (COLS / 2)) // nested ternary
? " ".repeat(lights[row][col].getRGB().length())
: " ";
outString +=
// reset
"\033[m" +
// color
"\033[38;2;" +
lights[row][col].getRed() + ";" +
lights[row][col].getGreen() + ";" +
lights[row][col].getBlue() + ";" +
"7m" +
// color code or blank character
c +
// reset
"\033[m";
}
}
outString += "\n";
}
}
// remove last comma, newline, add square bracket, reset color
outString += "\033[m";
return outString;
}
// /* Game of Life */
// public Light[][] simulateLife() {
// // 2D array nested loops, used for reference
// for (int row = 0; row < lights.length; row++) {
// for (int col = 0; col < lights[row].length; col++) {
// // count neighbors
// int neighbors = 0;
// // check each neighbor
// for (int i = -1; i <= 1; i++) {
// for (int j = -1; j <= 1; j++) {
// // skip self
// if (i == 0 && j == 0) continue;
// // check if neighbor is alive
// if (lights[(row + i + lights.length) % lights.length][(col + j + lights[row].length) % lights[row].length].getOn()) {
// neighbors++;
// }
// }
// }
// // apply rules
// /*
// * Any live cell with two or three live neighbours survives.
// Any dead cell with three live neighbours becomes a live cell.
// All other live cells die in the next generation. Similarly, all other dead cells stay dead.
// *
// */
// if (lights[row][col].getOn()) {
// if (neighbors < 2 || neighbors > 3) {
// lights[row][col].setOn(false); // lambok method
// }
// } else {
// if (neighbors == 3) {
// lights[row][col].setOn(true);
// }
// }
// }
// }
// return lights;
// }
// // Display game of life with getOn as on/off
// public void displayLife() {
// // 2D array nested loops, used for reference
// for (int row = 0; row < lights.length; row++) {
// for (int col = 0; col < lights[row].length; col++) {
// System.out.print(lights[row][col].getOn() ? "O" : "X");
// }
// System.out.println();
// }
// }
// // Display game of life with getOn as on/off
// public void displayLifeColor() {
// // 2D array nested loops, used for reference
// for (int row = 0; row < lights.length; row++) {
// for (int col = 0; col < lights[row].length; col++) {
// if (lights[row][col].getOn()) {
// System.out.print("\033[38;2;" +
// lights[row][col].getRed() + ";" +
// lights[row][col].getGreen() + ";" +
// lights[row][col].getBlue() + ";" +
// "7m" +
// "O" +
// "\033[m");
// } else { // if not alive then don't print color
// System.out.print("X");
// }
// // System.out.print(
// // // reset
// // "\033[m" +
// // // color
// // "\033[38;2;" +
// // lights[row][col].getRed() + ";" +
// // lights[row][col].getGreen() + ";" +
// // lights[row][col].getBlue() + ";" +
// // "7m" +
// // // data
// // (lights[row][col].getOn() ? "O" : "X") +
// // // reset
// // "\033[m"
// // );
// }
// System.out.println();
// }
// }
// // next generation returns next generation of lights
// public Light[][] nextGeneration() {
// // 2D array nested loops, used for reference
// for (int row = 0; row < lights.length; row++) {
// for (int col = 0; col < lights[row].length; col++) {
// // count neighbors
// int neighbors = 0;
// // check each neighbor
// for (int i = -1; i <= 1; i++) {
// for (int j = -1; j <= 1; j++) {
// // skip self
// if (i == 0 && j == 0) continue;
// // check if neighbor is alive
// if (lights[(row + i + lights.length) % lights.length][(col + j + lights[row].length) % lights[row].length].getOn()) {
// neighbors++;
// }
// }
// }
// // apply rules
// /*
// * Any live cell with two or three live neighbours survives.
// Any dead cell with three live neighbours becomes a live cell.
// All other live cells die in the next generation. Similarly, all other dead cells stay dead.
// *
// */
// if (lights[row][col].getOn()) {
// if (neighbors < 2 || neighbors > 3) {
// lights[row][col].setOn(false); // lambok method
// }
// } else {
// if (neighbors == 3) {
// lights[row][col].setOn(true);
// }
// }
// }
// }
// return lights;
// }
static public void main(String[] args) {
// create and display LightBoard
LightBoard lightBoard = new LightBoard(5, 5);
System.out.println(lightBoard); // use toString() method
System.out.println(lightBoard.toTerminal());
System.out.println(lightBoard.toColorPalette());
// create and display LightBoard
LightBoard lightBoard2 = new LightBoard(6, 6);
for (int i = 0; i < 6; i++) {
lightBoard2.lightSwitch(2, i);
}
System.out.println(lightBoard2); // use toString() method
System.out.println(lightBoard2.toTerminal());
System.out.println(lightBoard2.toColorPalette());
// create and display Lightboard
double[] light1 = {0, 0, 0, 0};
double[] light2 = {0.1, 0.1, 0.1, 0.1};
double[] light3 = {0.2, 0.2, 0.2, 0.2};
double[] light4 = {0.3, 0.3, 0.3, 0.3};
double[] light5 = {0.4, 0.4, 0.4, 0.4};
double[] light6 = {0.5, 0.5, 0.5, 0.5};
double[] light7 = {0.6, 0.6, 0.6, 0.6};
double[] light8 = {0.7, 0.7, 0.7, 0.7};
double[] light9 = {0.8, 0.8, 0.8, 0.8};
double[][][] lights = {{light1, light2, light3}, {light4, light5, light6}, {light7, light8, light9}};
LightBoard lightBoard3 = new LightBoard(lights);
System.out.println(lightBoard3); // use toString() method
System.out.println(lightBoard3.toTerminal());
System.out.println(lightBoard3.toColorPalette(10, 10));
}
}