generated from nighthawkcoders/spring_portfolio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LightBoard.java
180 lines (152 loc) · 6.36 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
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
}
}
}
/* 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() + "\"" +
"," +
"\"" + "isOn\": " + "\"" + lights[row][col].isOn() + "\"" +
"}," +
// 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 = 4;
final int COLS = 6;
// 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++) {
if (lights[row][col].isOn() == true) {
// 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 void turnOnLight(int row, int column) {
System.out.println("Turning on light in position " + row + ", " + column);
lights[row][column].setOn(true);
}
public void turnOffLight(int row, int column) {
System.out.println("Turning off light in position " + row + ", " + column);
lights[row][column].setOn(false);
}
public void turnAllLightsOn(){
System.out.println("turning on all lights");
for(int i = 0; i < lights.length;i++){
for(int j = 0; j <lights[i].length;j++){
lights[i][j].setOn(true);
}
}
}
public void turnAllLightsOff(){
System.out.println("turning off all lights");
for(int i = 0; i < lights.length;i++){
for(int j = 0; j <lights[i].length;j++){
lights[i][j].setOn(false);
}
}
}
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());
lightBoard.turnAllLightsOn();
System.out.println(lightBoard.toTerminal());
lightBoard.turnOffLight(2, 2);
System.out.println(lightBoard.toColorPalette());
lightBoard.turnOffLight(3, 4);
System.out.println(lightBoard.toColorPalette());
lightBoard.turnOffLight(4, 0);
System.out.println(lightBoard.toColorPalette());
lightBoard.turnOnLight(2, 2);
System.out.println(lightBoard.toColorPalette());
}
}