From 83748256108d4f6e6c2e63ace7b7511f96353c5d Mon Sep 17 00:00:00 2001 From: LordBaryhobal Date: Tue, 17 Dec 2024 14:47:32 +0100 Subject: [PATCH] added ex P bis --- src/exercises/ex_p_bis/Ansi.java | 13 +++++ src/exercises/ex_p_bis/ChristmasTree.java | 51 +++++++++++++++++++ .../ex_p_bis/ChristmasTreeTester.java | 32 ++++++++++++ src/exercises/ex_p_bis/Light.java | 31 +++++++++++ 4 files changed, 127 insertions(+) create mode 100644 src/exercises/ex_p_bis/Ansi.java create mode 100644 src/exercises/ex_p_bis/ChristmasTree.java create mode 100644 src/exercises/ex_p_bis/ChristmasTreeTester.java create mode 100644 src/exercises/ex_p_bis/Light.java diff --git a/src/exercises/ex_p_bis/Ansi.java b/src/exercises/ex_p_bis/Ansi.java new file mode 100644 index 0000000..65c2865 --- /dev/null +++ b/src/exercises/ex_p_bis/Ansi.java @@ -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"; +} diff --git a/src/exercises/ex_p_bis/ChristmasTree.java b/src/exercises/ex_p_bis/ChristmasTree.java new file mode 100644 index 0000000..f5c9211 --- /dev/null +++ b/src/exercises/ex_p_bis/ChristmasTree.java @@ -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 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); + } +} diff --git a/src/exercises/ex_p_bis/ChristmasTreeTester.java b/src/exercises/ex_p_bis/ChristmasTreeTester.java new file mode 100644 index 0000000..742eb7f --- /dev/null +++ b/src/exercises/ex_p_bis/ChristmasTreeTester.java @@ -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); + } + } +} diff --git a/src/exercises/ex_p_bis/Light.java b/src/exercises/ex_p_bis/Light.java new file mode 100644 index 0000000..845a65c --- /dev/null +++ b/src/exercises/ex_p_bis/Light.java @@ -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; + } +}