added ex P bis
This commit is contained in:
parent
bc4dfb6ef0
commit
8374825610
13
src/exercises/ex_p_bis/Ansi.java
Normal file
13
src/exercises/ex_p_bis/Ansi.java
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
package exercises.ex_p_bis;
|
||||||
|
|
||||||
|
public class Ansi {
|
||||||
|
public static final String RESET = "\u001B[0m";
|
||||||
|
public static final String BLACK = "\u001B[30m";
|
||||||
|
public static final String RED = "\u001B[31m";
|
||||||
|
public static final String GREEN = "\u001B[32m";
|
||||||
|
public static final String YELLOW = "\u001B[33m";
|
||||||
|
public static final String BLUE = "\u001B[34m";
|
||||||
|
public static final String PURPLE = "\u001B[35m";
|
||||||
|
public static final String CYAN = "\u001B[36m";
|
||||||
|
public static final String WHITE = "\u001B[37m";
|
||||||
|
}
|
51
src/exercises/ex_p_bis/ChristmasTree.java
Normal file
51
src/exercises/ex_p_bis/ChristmasTree.java
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
package exercises.ex_p_bis;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class ChristmasTree {
|
||||||
|
private static final String[] lines = {
|
||||||
|
" ★ ★ ★",
|
||||||
|
" ★ ╱╲ ",
|
||||||
|
" ★ ╱ ╲ ★ ",
|
||||||
|
" ╱ ╲ ",
|
||||||
|
" ★ ╱ ╲ ★ ",
|
||||||
|
" ╱ ╲ ",
|
||||||
|
" ╱─ ─╲ ",
|
||||||
|
" ╱ ╲ ★ ",
|
||||||
|
" ★ ╱ ╲ ",
|
||||||
|
" ╱────┐ ┌────╲ ",
|
||||||
|
" ★ │ │ ★ ",
|
||||||
|
};
|
||||||
|
public static final int W = 20;
|
||||||
|
public static final int H = 11;
|
||||||
|
private final Map<Integer, Light> lights = new HashMap<>();
|
||||||
|
|
||||||
|
public void addLight(int x, int y, Light light) {
|
||||||
|
lights.put(x + y * W, light);
|
||||||
|
light.setTree(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized void show() {
|
||||||
|
String result = "";
|
||||||
|
for (int y=0; y < H; y++) {
|
||||||
|
String line = "";
|
||||||
|
for (int x=0; x < W; x++) {
|
||||||
|
String c = String.valueOf(lines[y].charAt(x));
|
||||||
|
if (c.equals("★")) {
|
||||||
|
c = Ansi.YELLOW + c + Ansi.RESET;
|
||||||
|
}
|
||||||
|
Light light = lights.get(x + y * W);
|
||||||
|
if (light != null) {
|
||||||
|
c = light.apply(c);
|
||||||
|
}
|
||||||
|
line += c;
|
||||||
|
}
|
||||||
|
if (y != 0) {
|
||||||
|
result += "\n";
|
||||||
|
}
|
||||||
|
result += line;
|
||||||
|
}
|
||||||
|
System.out.println(result);
|
||||||
|
}
|
||||||
|
}
|
32
src/exercises/ex_p_bis/ChristmasTreeTester.java
Normal file
32
src/exercises/ex_p_bis/ChristmasTreeTester.java
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
package exercises.ex_p_bis;
|
||||||
|
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
public class ChristmasTreeTester {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
ChristmasTree tree = new ChristmasTree();
|
||||||
|
|
||||||
|
Light light1 = new Light(Ansi.RED);
|
||||||
|
Light light2 = new Light(Ansi.RED);
|
||||||
|
Light light3 = new Light(Ansi.GREEN);
|
||||||
|
Light light4 = new Light(Ansi.GREEN);
|
||||||
|
Light light5 = new Light(Ansi.WHITE);
|
||||||
|
Light light6 = new Light(Ansi.WHITE);
|
||||||
|
|
||||||
|
tree.addLight(8, 3, light1);
|
||||||
|
tree.addLight(11, 5, light2);
|
||||||
|
tree.addLight(13, 6, light3);
|
||||||
|
tree.addLight(6, 7, light4);
|
||||||
|
tree.addLight(10, 2, light5);
|
||||||
|
tree.addLight(12, 8, light6);
|
||||||
|
|
||||||
|
ScheduledExecutorService executor = Executors.newScheduledThreadPool(5);
|
||||||
|
Light[] lights = {light1, light2, light3, light4, light5, light6};
|
||||||
|
for (int i = 0; i < 6; i++) {
|
||||||
|
Light light = lights[i];
|
||||||
|
executor.scheduleAtFixedRate(light::toggle, 0, (long) (1000 + Math.random() * 2000), TimeUnit.MILLISECONDS);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
31
src/exercises/ex_p_bis/Light.java
Normal file
31
src/exercises/ex_p_bis/Light.java
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
package exercises.ex_p_bis;
|
||||||
|
|
||||||
|
public class Light {
|
||||||
|
private boolean isOn = false;
|
||||||
|
private String prefix = "";
|
||||||
|
private static final String suffix = Ansi.RESET;
|
||||||
|
private ChristmasTree tree;
|
||||||
|
|
||||||
|
public Light(String prefix) {
|
||||||
|
this.prefix = prefix;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void toggle() {
|
||||||
|
isOn = !isOn;
|
||||||
|
tree.show();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTree(ChristmasTree tree) {
|
||||||
|
this.tree = tree;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String apply(String c) {
|
||||||
|
if (isOn) {
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
if (c.equals(" ")) {
|
||||||
|
c = "⬤";
|
||||||
|
}
|
||||||
|
return prefix + c + suffix;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user