From 523736ce294e9bfa5cf53ad5c0a7ffb999f7f101 Mon Sep 17 00:00:00 2001 From: "Mr. J" <45538844+Ultraviolet-Ninja@users.noreply.github.com> Date: Sat, 22 Jan 2022 14:03:23 -0600 Subject: [PATCH 01/12] Update README.md --- README.md | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ce32eed..5eb18e6 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,29 @@ # Bomb Squad -*If there's a module I want to solve, I can put it here* +*If there's a module I want to solve, I'm putting it here* ## Introduction The purpose of this project is as an extension to the Gradle Centurion to solve more modules than those on the Centurion This also helped me understand how to set up a multi module project with Gradle +## Technologies +- Java 16 +- Gradle 7.3.3 + +### Plugins +- Jacoco +- JavaFX +- Pitest +- Palantir Docker +- Breadmoirai GitHub Release +### Dependencies +- MaterialFX ver 11.12.0 +- JFoenix ver 9.0.4 +- JavaTuple ver 1.2 +- JGraphT ver. 1.5.1 +- OpenCSV ver. 5.5.2 +### Other Technologies +- Circle CI +- LGTM Code Quality + +## Status - Setting Up +Currently transferring over material from the Gradle Centurion project and organizing it in a multi module project From b287c4fe764551b4b4ab1be009e9d5f6725ba383 Mon Sep 17 00:00:00 2001 From: "Mr. J" <45538844+Ultraviolet-Ninja@users.noreply.github.com> Date: Sat, 22 Jan 2022 20:11:08 -0600 Subject: [PATCH 02/12] Update README.md --- README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/README.md b/README.md index 5eb18e6..3796847 100644 --- a/README.md +++ b/README.md @@ -13,8 +13,6 @@ This also helped me understand how to set up a multi module project with Gradle - Jacoco - JavaFX - Pitest -- Palantir Docker -- Breadmoirai GitHub Release ### Dependencies - MaterialFX ver 11.12.0 - JFoenix ver 9.0.4 From 4528da03fab6044ea2fbbcdb669badf0adb33c74 Mon Sep 17 00:00:00 2001 From: "Mr. J" <45538844+Ultraviolet-Ninja@users.noreply.github.com> Date: Sun, 23 Jan 2022 20:04:01 -0600 Subject: [PATCH 03/12] README.md additions --- README.md | 2 +- build.gradle | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 3796847..a351ad9 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # Bomb Squad *If there's a module I want to solve, I'm putting it here* ## Introduction -The purpose of this project is as an extension to the Gradle Centurion to solve more modules than those on the Centurion +The purpose of this project is as an extension to the [Gradle Centurion](https://github.com/Ultraviolet-Ninja/GradleCenturion) to solve more modules than those on the Centurion This also helped me understand how to set up a multi module project with Gradle diff --git a/build.gradle b/build.gradle index 1becfc7..e712ddb 100644 --- a/build.gradle +++ b/build.gradle @@ -33,8 +33,8 @@ allprojects { implementation 'org.jetbrains:annotations:22.0.0' testImplementation 'org.testng:testng:7.5' - testImplementation 'org.slf4j:slf4j-api:1.7.32' - testImplementation 'org.slf4j:slf4j-simple:1.7.32' + testImplementation 'org.slf4j:slf4j-api:1.7.33' + testImplementation 'org.slf4j:slf4j-simple:1.7.33' testImplementation 'nl.jqno.equalsverifier:equalsverifier:3.8.2' } } @@ -53,7 +53,7 @@ application { } javafx { - version = '16' + version = '17.0.1' modules = ['javafx.controls', 'javafx.fxml'] } From 6aa90b862cec98a1fde6b2ec7a5daff91e1f1e24 Mon Sep 17 00:00:00 2001 From: "Mr. J" <45538844+Ultraviolet-Ninja@users.noreply.github.com> Date: Sun, 23 Jan 2022 21:31:38 -0600 Subject: [PATCH 04/12] Update README.md --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index a351ad9..4d1866a 100644 --- a/README.md +++ b/README.md @@ -14,9 +14,9 @@ This also helped me understand how to set up a multi module project with Gradle - JavaFX - Pitest ### Dependencies -- MaterialFX ver 11.12.0 -- JFoenix ver 9.0.4 -- JavaTuple ver 1.2 +- MaterialFX ver. 11.12.0 +- JFoenix ver. 9.0.4 +- JavaTuple ver. 1.2 - JGraphT ver. 1.5.1 - OpenCSV ver. 5.5.2 ### Other Technologies From 7052d36a5473fe8899a8f85ad7fde09e50686e45 Mon Sep 17 00:00:00 2001 From: "Mr. J" <45538844+Ultraviolet-Ninja@users.noreply.github.com> Date: Sun, 23 Jan 2022 21:34:01 -0600 Subject: [PATCH 05/12] Update README.md --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 4d1866a..7cfdab8 100644 --- a/README.md +++ b/README.md @@ -14,11 +14,11 @@ This also helped me understand how to set up a multi module project with Gradle - JavaFX - Pitest ### Dependencies -- MaterialFX ver. 11.12.0 -- JFoenix ver. 9.0.4 -- JavaTuple ver. 1.2 -- JGraphT ver. 1.5.1 -- OpenCSV ver. 5.5.2 +- MaterialFX ver. 11.12.0 +- JFoenix ver. 9.0.4 +- JavaTuple ver. 1.2 +- JGraphT ver. 1.5.1 +- OpenCSV ver. 5.5.2 ### Other Technologies - Circle CI - LGTM Code Quality From fd1c87d9bc3240ffd9d4413210cbf212f04053b6 Mon Sep 17 00:00:00 2001 From: "Mr. J" <45538844+Ultraviolet-Ninja@users.noreply.github.com> Date: Sat, 29 Jan 2022 10:50:29 -0600 Subject: [PATCH 06/12] Added some controllers to the front end --- core/src/main/java/module-info.java | 5 + .../ab/alphabet/AlphabetController.java | 86 ++++++ .../ab/astrology/AstrologyController.java | 105 +++++++ src/main/resources/bomb/fxml/ab/alphabet.fxml | 77 +++++ .../resources/bomb/fxml/ab/astrology.fxml | 289 ++++++++++++++++++ 5 files changed, 562 insertions(+) create mode 100644 src/main/java/bomb/modules/ab/alphabet/AlphabetController.java create mode 100644 src/main/java/bomb/modules/ab/astrology/AstrologyController.java create mode 100644 src/main/resources/bomb/fxml/ab/alphabet.fxml create mode 100644 src/main/resources/bomb/fxml/ab/astrology.fxml diff --git a/core/src/main/java/module-info.java b/core/src/main/java/module-info.java index 5b15aa2..ce89150 100644 --- a/core/src/main/java/module-info.java +++ b/core/src/main/java/module-info.java @@ -10,6 +10,11 @@ exports core.bomb; exports core.bomb.abstractions; exports core.bomb.enumerations; + exports core.bomb.modules.ab.alphabet; + exports core.bomb.modules.ab.astrology; + exports core.bomb.modules.ab.bitwise; + exports core.bomb.modules.ab.blind_alley; + exports core.bomb.modules.ab.boolean_venn; exports core.bomb.modules.dh.hexamaze; exports core.bomb.modules.dh.hexamaze.hexalgorithm.storage; exports core.bomb.modules.s.simon; diff --git a/src/main/java/bomb/modules/ab/alphabet/AlphabetController.java b/src/main/java/bomb/modules/ab/alphabet/AlphabetController.java new file mode 100644 index 0000000..169dc5f --- /dev/null +++ b/src/main/java/bomb/modules/ab/alphabet/AlphabetController.java @@ -0,0 +1,86 @@ +package bomb.modules.ab.alphabet; + +import core.bomb.abstractions.Resettable; +import core.bomb.modules.ab.alphabet.Alphabet; +import io.github.palexdev.materialfx.controls.MFXTextField; +import javafx.event.EventHandler; +import javafx.fxml.FXML; +import javafx.scene.input.KeyEvent; +import tools.pattern.facade.FacadeFX; + +import java.util.HashMap; +import java.util.Map; + +import static javafx.scene.control.Alert.AlertType.ERROR; +import static tools.pattern.factory.TextFormatterFactory.createOneLetterFormatter; + +public class AlphabetController implements Resettable { + private final Map stateMap; + + @FXML + private MFXTextField firstInput, secondInput, thirdInput, forthInput, + firstOutput, secondOutput, thirdOutput, forthOutput; + + public AlphabetController() { + stateMap = new HashMap<>(); + } + + public void initialize() { + stateMap.put(firstInput, secondInput); + stateMap.put(secondInput, thirdInput); + stateMap.put(thirdInput, forthInput); + stateMap.put(forthInput, null); + for (MFXTextField input : stateMap.keySet()) { + input.setTextFormatter(createOneLetterFormatter()); + input.setOnKeyReleased(createActionHandler()); + } + } + + private EventHandler createActionHandler() { + return event -> { + MFXTextField source = (MFXTextField) event.getSource(); + boolean canGetResults = false; + if (!source.getText().isEmpty()) { + canGetResults = moveToLastEmptyTextField(source); + } + if (canGetResults) getResults(); + }; + } + + private boolean moveToLastEmptyTextField(MFXTextField source) { + do { + source = stateMap.get(source); + } while (source != null && !source.getText().isEmpty()); + + if (source != null) { + source.requestFocus(); + return false; + } + return true; + } + + private void getResults() { + String inputText = firstInput.getText() + secondInput.getText() + thirdInput.getText() + forthInput.getText(); + try { + String results = Alphabet.order(inputText); + firstOutput.setText(String.valueOf(results.charAt(0))); + secondOutput.setText(String.valueOf(results.charAt(1))); + thirdOutput.setText(String.valueOf(results.charAt(2))); + forthOutput.setText(String.valueOf(results.charAt(3))); + } catch (IllegalArgumentException arg) { + FacadeFX.setAlert(ERROR, arg.getMessage()); + } + } + + @FXML + private void resetPuzzle() { + reset(); + } + + @Override + public void reset() { + MFXTextField[] fields = {firstInput, secondInput, thirdInput, forthInput, firstOutput, secondOutput, + thirdOutput, forthOutput}; + FacadeFX.clearMultipleTextFields(fields); + } +} diff --git a/src/main/java/bomb/modules/ab/astrology/AstrologyController.java b/src/main/java/bomb/modules/ab/astrology/AstrologyController.java new file mode 100644 index 0000000..4d25633 --- /dev/null +++ b/src/main/java/bomb/modules/ab/astrology/AstrologyController.java @@ -0,0 +1,105 @@ +package bomb.modules.ab.astrology; + +import core.bomb.abstractions.Resettable; +import core.bomb.modules.ab.astrology.Astrology; +import core.bomb.modules.ab.astrology.AstrologySymbol; +import io.github.palexdev.materialfx.controls.MFXButton; +import io.github.palexdev.materialfx.controls.MFXTextField; +import javafx.fxml.FXML; +import javafx.scene.control.Alert; +import javafx.scene.control.ToggleGroup; +import tools.pattern.facade.FacadeFX; + +import java.util.Arrays; + +public class AstrologyController implements Resettable { + private final AstrologySymbol[] astrologySymbolBuffer; + + @FXML + private MFXButton resetButton; + + @FXML + private MFXTextField omenTextField; + + @FXML + private ToggleGroup elementGroup, celestialGroup, zodiacGroup; + + public AstrologyController() { + astrologySymbolBuffer = new AstrologySymbol[3]; + } + + @FXML + private void determineElement() { + enableResetButton(); + setBufferLocation(elementGroup, AstrologySymbol.getElementalSymbols(), Astrology.ELEMENT_INDEX); + } + + @FXML + private void determineCelestial() { + enableResetButton(); + setBufferLocation(celestialGroup, AstrologySymbol.getCelestialSymbols(), Astrology.CELESTIAL_INDEX); + } + + @FXML + private void determineZodiac() { + enableResetButton(); + setBufferLocation(zodiacGroup, AstrologySymbol.getZodiacSymbols(), Astrology.ZODIAC_INDEX); + } + + private void enableResetButton() { + if (resetButton.isDisable()) + FacadeFX.enable(resetButton); + } + + private void setBufferLocation(ToggleGroup currentGroup, AstrologySymbol[] searchArray, int index) { + if (currentGroup.getSelectedToggle() == null) { + astrologySymbolBuffer[index] = null; + disableResetButton(); + return; + } + + String toggleName = FacadeFX.getToggleName(currentGroup).toUpperCase(); + for (int i = 0; i < searchArray.length; i++) { + if (toggleName.equals(searchArray[i].name())) { + astrologySymbolBuffer[index] = searchArray[i]; + i = searchArray.length; + } + } + + if (astrologySymbolBuffer[Astrology.ELEMENT_INDEX] != null && + astrologySymbolBuffer[Astrology.CELESTIAL_INDEX] != null && + astrologySymbolBuffer[Astrology.ZODIAC_INDEX] != null) + processBuffer(); + } + + private void disableResetButton() { + resetButton.setDisable( + astrologySymbolBuffer[Astrology.ELEMENT_INDEX] == null && + astrologySymbolBuffer[Astrology.CELESTIAL_INDEX] == null && + astrologySymbolBuffer[Astrology.ZODIAC_INDEX] == null + ); + } + + private void processBuffer() { + try { + omenTextField.setText(Astrology.calculate(astrologySymbolBuffer)); + } catch (IllegalArgumentException illegal) { + FacadeFX.setAlert(Alert.AlertType.ERROR, illegal.getMessage()); + } + } + + @FXML + private void resetModule() { + Arrays.fill(astrologySymbolBuffer, null); + FacadeFX.disable(resetButton); + FacadeFX.clearText(omenTextField); + FacadeFX.resetToggleGroup(elementGroup); + FacadeFX.resetToggleGroup(celestialGroup); + FacadeFX.resetToggleGroup(zodiacGroup); + } + + @Override + public void reset() { + resetModule(); + } +} diff --git a/src/main/resources/bomb/fxml/ab/alphabet.fxml b/src/main/resources/bomb/fxml/ab/alphabet.fxml new file mode 100644 index 0000000..8e2d914 --- /dev/null +++ b/src/main/resources/bomb/fxml/ab/alphabet.fxml @@ -0,0 +1,77 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/resources/bomb/fxml/ab/astrology.fxml b/src/main/resources/bomb/fxml/ab/astrology.fxml new file mode 100644 index 0000000..05e323d --- /dev/null +++ b/src/main/resources/bomb/fxml/ab/astrology.fxml @@ -0,0 +1,289 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + From bee25ab6d845f777597cd784548626049781f34d Mon Sep 17 00:00:00 2001 From: "Mr. J" <45538844+Ultraviolet-Ninja@users.noreply.github.com> Date: Fri, 18 Feb 2022 11:18:51 -0600 Subject: [PATCH 07/12] Upgrade to Gradle 7.4 --- README.md | 4 +--- gradle/wrapper/gradle-wrapper.properties | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 7cfdab8..367af0d 100644 --- a/README.md +++ b/README.md @@ -7,12 +7,10 @@ This also helped me understand how to set up a multi module project with Gradle ## Technologies - Java 16 -- Gradle 7.3.3 +- Gradle 7.4 ### Plugins -- Jacoco - JavaFX -- Pitest ### Dependencies - MaterialFX ver. 11.12.0 - JFoenix ver. 9.0.4 diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 2e6e589..41dfb87 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists From ce84ca3e328f1579e9f6ca14b8161189513ad9d0 Mon Sep 17 00:00:00 2001 From: "Mr. J" <45538844+Ultraviolet-Ninja@users.noreply.github.com> Date: Sat, 19 Feb 2022 11:25:10 -0600 Subject: [PATCH 08/12] Optimized CSVReader objects --- .../ColoredSwitchGraphFactory.java | 18 ++++++----- .../c/colored_switches/ColoredSwitches.java | 7 +---- .../bomb/modules/dh/hexamaze/Hexamaze.java | 3 +- .../hexalgorithm/factory/MazeFactory.java | 26 ++++++++++------ .../hexalgorithm/maze_finding/MazeSearch.java | 5 ++-- .../hexamaze/hexalgorithm/storage/Maze.java | 12 ++------ .../bomb/modules/il/ice_cream/Person.java | 30 +++++++++++-------- .../modules/m/murder/LocationMapFactory.java | 30 +++++++++++-------- .../core/bomb/modules/m/murder/Murder.java | 13 ++------ .../data/structures/queue/BufferedQueue.java | 2 +- .../factory/MorseCodeGraphFactory.java | 19 ++++++------ 11 files changed, 84 insertions(+), 81 deletions(-) diff --git a/core/src/main/java/core/bomb/modules/c/colored_switches/ColoredSwitchGraphFactory.java b/core/src/main/java/core/bomb/modules/c/colored_switches/ColoredSwitchGraphFactory.java index 734f9d5..946f593 100644 --- a/core/src/main/java/core/bomb/modules/c/colored_switches/ColoredSwitchGraphFactory.java +++ b/core/src/main/java/core/bomb/modules/c/colored_switches/ColoredSwitchGraphFactory.java @@ -1,11 +1,13 @@ package core.bomb.modules.c.colored_switches; import com.opencsv.CSVReader; +import org.jetbrains.annotations.NotNull; import org.jgrapht.Graph; import org.jgrapht.graph.DefaultEdge; import org.jgrapht.graph.SimpleDirectedGraph; import tools.filter.Regex; +import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; @@ -18,18 +20,20 @@ public class ColoredSwitchGraphFactory { private static final byte OUTGOING_STATE = 1, COLOR_CONDITIONS = 2, SWITCH_TO_FLIP = 3; private static final String FILENAME = "graph.csv"; - public static Graph makeGraph() throws IOException { + public static @NotNull Graph makeGraph() throws IllegalStateException { return buildGraph(createFromFile()); } - private static List createFromFile() throws IOException { + private static List createFromFile() throws IllegalStateException { List output = new ArrayList<>(); - CSVReader csvReader = createReader(); Regex connectionFinder = new Regex("\\[(\\d{1,2})\\((\\d{1,3})\\)([1-5])]"); - csvReader.forEach(record -> output.add(buildNode(record, connectionFinder))); - csvReader.close(); - return output; + try (CSVReader csvReader = createReader()) { + csvReader.forEach(record -> output.add(buildNode(record, connectionFinder))); + return output; + } catch (IOException e) { + throw new IllegalStateException(e); + } } private static Graph buildGraph(List nodeList) { @@ -50,7 +54,7 @@ private static Graph buildGraph(List producePreemptiveMoveList(byte startingState) throws IllegalArgumentException { diff --git a/core/src/main/java/core/bomb/modules/dh/hexamaze/Hexamaze.java b/core/src/main/java/core/bomb/modules/dh/hexamaze/Hexamaze.java index a72a484..cc91632 100644 --- a/core/src/main/java/core/bomb/modules/dh/hexamaze/Hexamaze.java +++ b/core/src/main/java/core/bomb/modules/dh/hexamaze/Hexamaze.java @@ -48,7 +48,8 @@ public class Hexamaze extends Widget { @NotNull Grid, @Nullable String, @Nullable Integer, - @Nullable List>solve(@NotNull List nodeList) throws IllegalArgumentException { + @Nullable List> solve(@NotNull List nodeList) + throws IllegalArgumentException, IllegalStateException { Maze maze = new Maze(); Grid original = new Grid(new HexagonalPlane(nodeList)); diff --git a/core/src/main/java/core/bomb/modules/dh/hexamaze/hexalgorithm/factory/MazeFactory.java b/core/src/main/java/core/bomb/modules/dh/hexamaze/hexalgorithm/factory/MazeFactory.java index 054bb2e..124a245 100644 --- a/core/src/main/java/core/bomb/modules/dh/hexamaze/hexalgorithm/factory/MazeFactory.java +++ b/core/src/main/java/core/bomb/modules/dh/hexamaze/hexalgorithm/factory/MazeFactory.java @@ -6,10 +6,13 @@ import com.opencsv.CSVReader; import com.opencsv.exceptions.CsvException; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.io.Reader; import java.util.Arrays; import java.util.EnumSet; import java.util.List; @@ -20,22 +23,27 @@ import static core.bomb.modules.dh.hexamaze.hexalgorithm.storage.HexNode.HexShape.LEFT_TRIANGLE; import static core.bomb.modules.dh.hexamaze.hexalgorithm.storage.HexNode.HexShape.RIGHT_TRIANGLE; import static core.bomb.modules.dh.hexamaze.hexalgorithm.storage.HexNode.HexShape.UP_TRIANGLE; +import static java.nio.charset.StandardCharsets.UTF_8; import static java.util.Arrays.stream; import static java.util.stream.Collectors.toCollection; @SuppressWarnings("ConstantConditions") public class MazeFactory { - public static @NotNull List createMaze() throws IOException, CsvException { + public static @NotNull List createMaze() throws IllegalStateException { InputStream in = MazeFactory.class.getResourceAsStream("maze.csv"); - CSVReader csvReader = new CSVReader(new InputStreamReader(in)); - return csvReader.readAll().stream() - .flatMap(Arrays::stream) - .map(line -> line.split(" ")) - .map(data -> new HexNode(decodeShape(data[1]), decodeWalls(data[0]))) - .toList(); + Reader reader = new BufferedReader(new InputStreamReader(in, UTF_8)); + try (CSVReader csvReader = new CSVReader(reader)) { + return csvReader.readAll().stream() + .flatMap(Arrays::stream) + .map(line -> line.split(" ")) + .map(data -> new HexNode(decodeShape(data[1]), decodeWalls(data[0]))) + .toList(); + } catch (CsvException | IOException e) { + throw new IllegalStateException(e); + } } - public static HexShape decodeShape(String code) { + public static @Nullable HexShape decodeShape(@NotNull String code) { return switch(code) { case "c" -> CIRCLE; case "h" -> HEXAGON; @@ -47,7 +55,7 @@ public static HexShape decodeShape(String code) { }; } - public static EnumSet decodeWalls(String code) { + public static @NotNull EnumSet decodeWalls(@NotNull String code) { HexWall[] allWalls = HexWall.values(); return stream(code.split("")) diff --git a/core/src/main/java/core/bomb/modules/dh/hexamaze/hexalgorithm/maze_finding/MazeSearch.java b/core/src/main/java/core/bomb/modules/dh/hexamaze/hexalgorithm/maze_finding/MazeSearch.java index 01333c5..65650b6 100644 --- a/core/src/main/java/core/bomb/modules/dh/hexamaze/hexalgorithm/maze_finding/MazeSearch.java +++ b/core/src/main/java/core/bomb/modules/dh/hexamaze/hexalgorithm/maze_finding/MazeSearch.java @@ -15,7 +15,7 @@ import static core.bomb.modules.dh.hexamaze.hexalgorithm.storage.AbstractHexagon.calculateColumnLengthArray; public class MazeSearch { - public static final int ROTATION_COUNT = 6; + private static final int ROTATION_COUNT = 6; public static Optional search(@NotNull Maze maze, @NotNull Grid grid) { int gridSpan = grid.getHexagon().getSpan(); @@ -139,8 +139,7 @@ private static List convertToHexShapes(HexagonalPlane structure) { .toList(); } - private static void moveToNextSegment(BufferedQueue> pillar, - HexagonalPlane copy) { + private static void moveToNextSegment(BufferedQueue> pillar, HexagonalPlane copy) { BufferedQueue> copiedQueues = copy.getBufferedQueues(); for (BufferedQueue column : copiedQueues) column.removeFirst(); diff --git a/core/src/main/java/core/bomb/modules/dh/hexamaze/hexalgorithm/storage/Maze.java b/core/src/main/java/core/bomb/modules/dh/hexamaze/hexalgorithm/storage/Maze.java index 890f99f..8432c07 100644 --- a/core/src/main/java/core/bomb/modules/dh/hexamaze/hexalgorithm/storage/Maze.java +++ b/core/src/main/java/core/bomb/modules/dh/hexamaze/hexalgorithm/storage/Maze.java @@ -1,20 +1,12 @@ package core.bomb.modules.dh.hexamaze.hexalgorithm.storage; import core.bomb.modules.dh.hexamaze.hexalgorithm.factory.MazeFactory; -import com.opencsv.exceptions.CsvException; - -import java.io.IOException; public class Maze extends AbstractHexagon { private static final int FULL_MAZE_SIDE_LENGTH = 12; - public Maze() throws IllegalArgumentException { + public Maze() throws IllegalArgumentException, IllegalStateException { super(new HexagonalPlane(FULL_MAZE_SIDE_LENGTH)); - - try { - hexagon.readInNodeList(MazeFactory.createMaze()); - } catch (IOException | CsvException e) { - throw new IllegalArgumentException(e); - } + hexagon.readInNodeList(MazeFactory.createMaze()); } } diff --git a/core/src/main/java/core/bomb/modules/il/ice_cream/Person.java b/core/src/main/java/core/bomb/modules/il/ice_cream/Person.java index e40f272..953c823 100644 --- a/core/src/main/java/core/bomb/modules/il/ice_cream/Person.java +++ b/core/src/main/java/core/bomb/modules/il/ice_cream/Person.java @@ -2,13 +2,16 @@ import com.opencsv.CSVReader; +import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.io.Reader; import java.util.Arrays; import java.util.EnumMap; import java.util.EnumSet; +import static java.nio.charset.StandardCharsets.UTF_8; import static java.util.stream.Collectors.toCollection; @SuppressWarnings("ConstantConditions") @@ -23,22 +26,23 @@ public enum Person { } public static EnumMap> getPersonAllergens(int index) throws IllegalStateException { + int counter = 0; + Person[] people = values(); InputStream in = Person.class.getResourceAsStream(FILENAME); - CSVReader reader = new CSVReader(new InputStreamReader(in)); EnumMap> output = new EnumMap<>(Person.class); - Person[] people = values(); - int counter = 0; - for (String[] line : reader) { - output.put(people[counter++], - Arrays.stream(line[index].split("")) - .mapToInt(Integer::parseInt) - .mapToObj(Allergen::getByIndex) - .collect(toCollection(() -> EnumSet.noneOf(Allergen.class))) - ); - } - try { - reader.close(); + Reader reader = new BufferedReader(new InputStreamReader(in, UTF_8)); + try (CSVReader csvReader = new CSVReader(reader)) { + for (String[] line : csvReader) { + output.put( + people[counter++], + Arrays.stream(line[index].split("")) + .mapToInt(Integer::parseInt) + .mapToObj(Allergen::getByIndex) + .collect(toCollection(() -> EnumSet.noneOf(Allergen.class))) + ); + } + return output; } catch (IOException e) { throw new IllegalStateException(e); diff --git a/core/src/main/java/core/bomb/modules/m/murder/LocationMapFactory.java b/core/src/main/java/core/bomb/modules/m/murder/LocationMapFactory.java index 6b387d9..7ce2939 100644 --- a/core/src/main/java/core/bomb/modules/m/murder/LocationMapFactory.java +++ b/core/src/main/java/core/bomb/modules/m/murder/LocationMapFactory.java @@ -4,13 +4,16 @@ import com.opencsv.exceptions.CsvException; import org.javatuples.Pair; +import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.io.Reader; import java.util.Arrays; import java.util.EnumMap; import java.util.List; +import static java.nio.charset.StandardCharsets.UTF_8; import static java.util.stream.Collectors.toList; @SuppressWarnings("ConstantConditions") @@ -18,7 +21,7 @@ public class LocationMapFactory { private static final String FILENAME = "location_list.csv"; static Pair>, EnumMap>> createMaps() - throws IOException, CsvException { + throws IllegalStateException { EnumMap> suspectMap = new EnumMap<>(Suspect.class); EnumMap> weaponMap = new EnumMap<>(Weapon.class); @@ -37,17 +40,20 @@ static Pair>, EnumMap>> c return new Pair<>(suspectMap, weaponMap); } - private static List> createLocationLists() throws IOException, CsvException { + private static List> createLocationLists() throws IllegalStateException { InputStream in = Location.class.getResourceAsStream(FILENAME); - CSVReader reader = new CSVReader(new InputStreamReader(in)); - - return reader.readAll() - .stream() - .map(Arrays::stream) - .map(stream -> stream - .map(Location::valueOf) - .collect(toList()) - ) - .collect(toList()); + Reader reader = new BufferedReader(new InputStreamReader(in, UTF_8)); + try (CSVReader csvReader = new CSVReader(reader)) { + return csvReader.readAll() + .stream() + .map(Arrays::stream) + .map(stream -> stream + .map(Location::valueOf) + .toList() + ) + .toList(); + } catch (IOException | CsvException e) { + throw new IllegalStateException(e); + } } } diff --git a/core/src/main/java/core/bomb/modules/m/murder/Murder.java b/core/src/main/java/core/bomb/modules/m/murder/Murder.java index 7c277b2..f908b92 100644 --- a/core/src/main/java/core/bomb/modules/m/murder/Murder.java +++ b/core/src/main/java/core/bomb/modules/m/murder/Murder.java @@ -1,12 +1,10 @@ package core.bomb.modules.m.murder; import core.bomb.Widget; -import com.opencsv.exceptions.CsvException; import org.javatuples.Pair; import org.javatuples.Triplet; import org.jetbrains.annotations.NotNull; -import java.io.IOException; import java.util.EnumMap; import java.util.EnumSet; import java.util.List; @@ -30,16 +28,11 @@ public class Murder extends Widget { public static @NotNull String solve(@NotNull Location bodyFoundRoom, @NotNull EnumSet possibleWeapons, - @NotNull EnumSet possibleSuspects) throws IllegalArgumentException { + @NotNull EnumSet possibleSuspects) throws IllegalStateException { validateInput(possibleWeapons, possibleSuspects, bodyFoundRoom); - Pair>, EnumMap>> mapPair; - - try { - mapPair = LocationMapFactory.createMaps(); - } catch (IOException | CsvException e) { - throw new IllegalArgumentException(e); - } + Pair>, EnumMap>> mapPair = + LocationMapFactory.createMaps(); EnumMap locationsToSuspect = getLocationsToType(mapPair.getValue0(), possibleSuspects, getSuspectRow(bodyFoundRoom) - 1); diff --git a/utility/src/main/java/tools/data/structures/queue/BufferedQueue.java b/utility/src/main/java/tools/data/structures/queue/BufferedQueue.java index b494852..0b618d7 100644 --- a/utility/src/main/java/tools/data/structures/queue/BufferedQueue.java +++ b/utility/src/main/java/tools/data/structures/queue/BufferedQueue.java @@ -74,7 +74,7 @@ public E removeFirst() { public List removeCount(int count) throws IllegalArgumentException { if (count >= dataDeque.size() || count <= 0) throw new IllegalArgumentException("Invalid count"); - List removalList = new ArrayList<>(); + List removalList = new ArrayList<>(count); for (int i = 0; i < count; i++) removalList.add(dataDeque.pollFirst()); diff --git a/utility/src/main/java/tools/pattern/factory/MorseCodeGraphFactory.java b/utility/src/main/java/tools/pattern/factory/MorseCodeGraphFactory.java index 97cf655..5eee949 100644 --- a/utility/src/main/java/tools/pattern/factory/MorseCodeGraphFactory.java +++ b/utility/src/main/java/tools/pattern/factory/MorseCodeGraphFactory.java @@ -3,9 +3,11 @@ import com.opencsv.CSVReader; import tools.data.structures.graph.ListGraph; +import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.io.Reader; import static java.nio.charset.StandardCharsets.UTF_8; @@ -18,17 +20,16 @@ private MorseCodeGraphFactory() {} public static ListGraph createGraph() throws IllegalStateException { ListGraph graph = new ListGraph<>(true); InputStream in = MorseCodeGraphFactory.class.getResourceAsStream(FILENAME); - CSVReader reader = new CSVReader(new InputStreamReader(in, UTF_8)); - - for (String[] line : reader) { - String letters = line[1]; - for (String letter : letters.split("_")) { - graph.addEdge(line[0], letter); + Reader reader = new BufferedReader(new InputStreamReader(in, UTF_8)); + + try (CSVReader csvReader = new CSVReader(reader)) { + for (String[] line : csvReader) { + String[] alphaNumericChars = line[1].split("_"); + for (String s : alphaNumericChars) { + graph.addEdge(line[0], s); + } } - } - try { - reader.close(); return graph; } catch (IOException e) { throw new IllegalStateException(e); From 74bdb05888ccaff01a662d292ef45e47e37bd88e Mon Sep 17 00:00:00 2001 From: "Mr. J" <45538844+Ultraviolet-Ninja@users.noreply.github.com> Date: Sat, 19 Feb 2022 15:26:10 -0600 Subject: [PATCH 09/12] Testing --- .../main/java/core/bomb/modules/il/ice_cream/Allergen.java | 1 - core/src/main/java/core/bomb/modules/il/ice_cream/Person.java | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/core/bomb/modules/il/ice_cream/Allergen.java b/core/src/main/java/core/bomb/modules/il/ice_cream/Allergen.java index e65eee4..2aabcfa 100644 --- a/core/src/main/java/core/bomb/modules/il/ice_cream/Allergen.java +++ b/core/src/main/java/core/bomb/modules/il/ice_cream/Allergen.java @@ -1,6 +1,5 @@ package core.bomb.modules.il.ice_cream; -import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; diff --git a/core/src/main/java/core/bomb/modules/il/ice_cream/Person.java b/core/src/main/java/core/bomb/modules/il/ice_cream/Person.java index 953c823..b936059 100644 --- a/core/src/main/java/core/bomb/modules/il/ice_cream/Person.java +++ b/core/src/main/java/core/bomb/modules/il/ice_cream/Person.java @@ -31,8 +31,8 @@ public static EnumMap> getPersonAllergens(int index) t InputStream in = Person.class.getResourceAsStream(FILENAME); EnumMap> output = new EnumMap<>(Person.class); - Reader reader = new BufferedReader(new InputStreamReader(in, UTF_8)); - try (CSVReader csvReader = new CSVReader(reader)) { + try (Reader reader = new BufferedReader(new InputStreamReader(in, UTF_8)); + CSVReader csvReader = new CSVReader(reader)) { for (String[] line : csvReader) { output.put( people[counter++], From c96e25c324778d23d136342015335366e715a2f0 Mon Sep 17 00:00:00 2001 From: "Mr. J" <45538844+Ultraviolet-Ninja@users.noreply.github.com> Date: Sat, 19 Feb 2022 15:51:47 -0600 Subject: [PATCH 10/12] Rewrote CSVReader calls --- .../ColoredSwitchGraphFactory.java | 15 ++++----------- .../hexalgorithm/factory/MazeFactory.java | 6 ++---- .../core/bomb/modules/il/ice_cream/Person.java | 5 +---- .../bomb/modules/m/murder/LocationMapFactory.java | 7 ++----- .../pattern/factory/MorseCodeGraphFactory.java | 5 +---- 5 files changed, 10 insertions(+), 28 deletions(-) diff --git a/core/src/main/java/core/bomb/modules/c/colored_switches/ColoredSwitchGraphFactory.java b/core/src/main/java/core/bomb/modules/c/colored_switches/ColoredSwitchGraphFactory.java index 946f593..a47cdf1 100644 --- a/core/src/main/java/core/bomb/modules/c/colored_switches/ColoredSwitchGraphFactory.java +++ b/core/src/main/java/core/bomb/modules/c/colored_switches/ColoredSwitchGraphFactory.java @@ -7,15 +7,14 @@ import org.jgrapht.graph.SimpleDirectedGraph; import tools.filter.Regex; -import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; -import java.io.Reader; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +@SuppressWarnings("ConstantConditions") public class ColoredSwitchGraphFactory { private static final byte OUTGOING_STATE = 1, COLOR_CONDITIONS = 2, SWITCH_TO_FLIP = 3; private static final String FILENAME = "graph.csv"; @@ -25,10 +24,11 @@ public class ColoredSwitchGraphFactory { } private static List createFromFile() throws IllegalStateException { - List output = new ArrayList<>(); + List output = new ArrayList<>(32); Regex connectionFinder = new Regex("\\[(\\d{1,2})\\((\\d{1,3})\\)([1-5])]"); + InputStream in = ColoredSwitchGraphFactory.class.getResourceAsStream(FILENAME); - try (CSVReader csvReader = createReader()) { + try (CSVReader csvReader = new CSVReader(new InputStreamReader(in))) { csvReader.forEach(record -> output.add(buildNode(record, connectionFinder))); return output; } catch (IOException e) { @@ -51,13 +51,6 @@ private static Graph buildGraph(List createMaze() throws IllegalStateException { InputStream in = MazeFactory.class.getResourceAsStream("maze.csv"); - Reader reader = new BufferedReader(new InputStreamReader(in, UTF_8)); - try (CSVReader csvReader = new CSVReader(reader)) { + + try (CSVReader csvReader = new CSVReader(new InputStreamReader(in, UTF_8))) { return csvReader.readAll().stream() .flatMap(Arrays::stream) .map(line -> line.split(" ")) diff --git a/core/src/main/java/core/bomb/modules/il/ice_cream/Person.java b/core/src/main/java/core/bomb/modules/il/ice_cream/Person.java index b936059..396a2d6 100644 --- a/core/src/main/java/core/bomb/modules/il/ice_cream/Person.java +++ b/core/src/main/java/core/bomb/modules/il/ice_cream/Person.java @@ -2,11 +2,9 @@ import com.opencsv.CSVReader; -import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; -import java.io.Reader; import java.util.Arrays; import java.util.EnumMap; import java.util.EnumSet; @@ -31,8 +29,7 @@ public static EnumMap> getPersonAllergens(int index) t InputStream in = Person.class.getResourceAsStream(FILENAME); EnumMap> output = new EnumMap<>(Person.class); - try (Reader reader = new BufferedReader(new InputStreamReader(in, UTF_8)); - CSVReader csvReader = new CSVReader(reader)) { + try (CSVReader csvReader = new CSVReader(new InputStreamReader(in, UTF_8))) { for (String[] line : csvReader) { output.put( people[counter++], diff --git a/core/src/main/java/core/bomb/modules/m/murder/LocationMapFactory.java b/core/src/main/java/core/bomb/modules/m/murder/LocationMapFactory.java index 7ce2939..5e08f8b 100644 --- a/core/src/main/java/core/bomb/modules/m/murder/LocationMapFactory.java +++ b/core/src/main/java/core/bomb/modules/m/murder/LocationMapFactory.java @@ -4,17 +4,14 @@ import com.opencsv.exceptions.CsvException; import org.javatuples.Pair; -import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; -import java.io.Reader; import java.util.Arrays; import java.util.EnumMap; import java.util.List; import static java.nio.charset.StandardCharsets.UTF_8; -import static java.util.stream.Collectors.toList; @SuppressWarnings("ConstantConditions") public class LocationMapFactory { @@ -42,8 +39,8 @@ static Pair>, EnumMap>> c private static List> createLocationLists() throws IllegalStateException { InputStream in = Location.class.getResourceAsStream(FILENAME); - Reader reader = new BufferedReader(new InputStreamReader(in, UTF_8)); - try (CSVReader csvReader = new CSVReader(reader)) { + + try (CSVReader csvReader = new CSVReader(new InputStreamReader(in, UTF_8))) { return csvReader.readAll() .stream() .map(Arrays::stream) diff --git a/utility/src/main/java/tools/pattern/factory/MorseCodeGraphFactory.java b/utility/src/main/java/tools/pattern/factory/MorseCodeGraphFactory.java index 5eee949..1c73336 100644 --- a/utility/src/main/java/tools/pattern/factory/MorseCodeGraphFactory.java +++ b/utility/src/main/java/tools/pattern/factory/MorseCodeGraphFactory.java @@ -3,11 +3,9 @@ import com.opencsv.CSVReader; import tools.data.structures.graph.ListGraph; -import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; -import java.io.Reader; import static java.nio.charset.StandardCharsets.UTF_8; @@ -20,9 +18,8 @@ private MorseCodeGraphFactory() {} public static ListGraph createGraph() throws IllegalStateException { ListGraph graph = new ListGraph<>(true); InputStream in = MorseCodeGraphFactory.class.getResourceAsStream(FILENAME); - Reader reader = new BufferedReader(new InputStreamReader(in, UTF_8)); - try (CSVReader csvReader = new CSVReader(reader)) { + try (CSVReader csvReader = new CSVReader(new InputStreamReader(in, UTF_8))) { for (String[] line : csvReader) { String[] alphaNumericChars = line[1].split("_"); for (String s : alphaNumericChars) { From df83dd78abcedfd2b6bbb7c83099cc7a51c61517 Mon Sep 17 00:00:00 2001 From: "Mr. J" <45538844+Ultraviolet-Ninja@users.noreply.github.com> Date: Sat, 19 Feb 2022 20:40:39 -0600 Subject: [PATCH 11/12] Minor update --- README.md | 2 +- build.gradle | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 367af0d..5737cac 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ ## Introduction The purpose of this project is as an extension to the [Gradle Centurion](https://github.com/Ultraviolet-Ninja/GradleCenturion) to solve more modules than those on the Centurion -This also helped me understand how to set up a multi module project with Gradle +This also helped me understand how to set up a multi-module project with Gradle ## Technologies - Java 16 diff --git a/build.gradle b/build.gradle index 9bb7edd..1a9e8c0 100644 --- a/build.gradle +++ b/build.gradle @@ -33,7 +33,7 @@ allprojects { implementation 'org.jetbrains:annotations:22.0.0' testImplementation 'org.testng:testng:7.5' - testImplementation 'org.slf4j:slf4j-api:1.7.35' + testImplementation 'org.slf4j:slf4j-api:1.7.36' testImplementation 'org.slf4j:slf4j-simple:1.7.36' testImplementation 'nl.jqno.equalsverifier:equalsverifier:3.9' } From 6bb8210404ff4fab9cb34d810452c3403540b5bc Mon Sep 17 00:00:00 2001 From: "Mr. J" <45538844+Ultraviolet-Ninja@users.noreply.github.com> Date: Mon, 28 Feb 2022 14:35:09 -0600 Subject: [PATCH 12/12] Added changes from GradleCenturion --- .../modules/ab/boolean_venn/BooleanVenn.java | 139 +++++------------- .../bomb/modules/dh/fast_math/FastMath.java | 39 +++-- .../test/java/core/bomb/BombSimulations.java | 2 +- .../java/tools/data/structures/trie/Trie.java | 2 +- .../main/java/tools/logic/LogicOperator.java | 55 +++++-- 5 files changed, 105 insertions(+), 132 deletions(-) diff --git a/core/src/main/java/core/bomb/modules/ab/boolean_venn/BooleanVenn.java b/core/src/main/java/core/bomb/modules/ab/boolean_venn/BooleanVenn.java index 286bfc5..d5e6f65 100644 --- a/core/src/main/java/core/bomb/modules/ab/boolean_venn/BooleanVenn.java +++ b/core/src/main/java/core/bomb/modules/ab/boolean_venn/BooleanVenn.java @@ -2,21 +2,14 @@ import core.bomb.Widget; import org.intellij.lang.annotations.Language; -import tools.filter.Regex; import tools.logic.LogicOperator; import org.jetbrains.annotations.NotNull; import static tools.filter.RegexFilter.LOGIC_REGEX; import static tools.filter.RegexFilter.LOGIC_SYMBOL_FILTER; import static tools.filter.RegexFilter.filter; -import static tools.logic.LogicOperator.AND; -import static tools.logic.LogicOperator.IMPLIED_BY; -import static tools.logic.LogicOperator.IMPLIES; -import static tools.logic.LogicOperator.NAND; -import static tools.logic.LogicOperator.NOR; -import static tools.logic.LogicOperator.OR; +import static tools.logic.LogicOperator.LOGIC_SYMBOL_TO_ENUM_MAP; import static tools.logic.LogicOperator.XNOR; -import static tools.logic.LogicOperator.XOR; /** * This class deals with the Boolean Venn Diagram module. @@ -30,21 +23,6 @@ public class BooleanVenn extends Widget { @Language("regexp") private static final String AB_PRIORITY_PATTERN, BC_PRIORITY_PATTERN; - static { - AB_PRIORITY_PATTERN = "\\(A" + LOGIC_REGEX + "B\\)" + LOGIC_REGEX + "C"; - BC_PRIORITY_PATTERN = "A" + LOGIC_REGEX + "\\(B" + LOGIC_REGEX + "C\\)"; - - TEST_CASES = new boolean[][]{ - {false, false, false}, - {false, false, true}, - {false, true, false}, - {true, false, false}, - {false, true, true}, - {true, false, true}, - {true, true, false}, - {true, true, true}}; - } - /** * Turns the String operation into a String code for the Venn Diagram to decode by choosing * the correct method depending on which side of the operation has the priority, that being @@ -58,113 +36,70 @@ public class BooleanVenn extends Widget { public static @NotNull String resultCode(@NotNull String operation) throws IllegalArgumentException { if (operation.isEmpty()) throw new IllegalArgumentException("Cannot have empty String"); return checkFormat(operation) ? - interpretAB(operation) : - interpretBC(operation); + interpretABPriority(operation) : + interpretBCPriority(operation); } - /** - * Checks the formatting of the equation to see if it fits either of (AB)C or A(BC) - * with logic symbols in between AB and BC - * - * @param equation The valid or invalid equation - * @return Whether the equation matches the - * @throws IllegalArgumentException Format mismatch for the input equation - */ private static boolean checkFormat(String equation) throws IllegalArgumentException { - String abPriority = filter(equation, new Regex(AB_PRIORITY_PATTERN)); - String bcPriority = filter(equation, new Regex(BC_PRIORITY_PATTERN)); + boolean doesMatchABPriority = equation.matches(AB_PRIORITY_PATTERN); - if (XNOR.test(abPriority.isEmpty(), bcPriority.isEmpty())) - throw new IllegalArgumentException("Format mismatch!!"); - return !abPriority.isEmpty(); + if (XNOR.test(doesMatchABPriority, equation.matches(BC_PRIORITY_PATTERN))) + throw new IllegalArgumentException("Format given does not match the format specified"); + return doesMatchABPriority; } - /** - * Interprets (AB)C - * - * @param operation The appropriate equation - * @return A String code that represents the state of each Venn Diagram section - * The output order is not, c, b, a, bc, ac, ab, all - */ - private static String interpretAB(String operation) { + private static String interpretABPriority(String operation) { String logicSymbols = filter(operation, LOGIC_SYMBOL_FILTER); StringBuilder builder = new StringBuilder(); - boolean[] priorityCases = priorityOutputs(logicSymbols.substring(0, 1), A + B); + boolean[] priorityCases = priorityOutputs(logicSymbols.substring(0, 1), B); for (int i = 0; i < TEST_CASES.length; i++) - builder.append(outsideOutputs(logicSymbols.substring(1), priorityCases[i], TEST_CASES[i][C])); + builder.append(appendCharacter(logicSymbols.substring(1), priorityCases[i], TEST_CASES[i][C])); return builder.toString(); } - /** - * Interprets A(BC) - * - * @param operation The appropriate equation - * @return A String code that represents the state of each Venn Diagram section - * The output order is not, c, b, a, bc, ac, ab, all - */ - private static String interpretBC(String operation) { + private static String interpretBCPriority(String operation) { String logicSymbols = filter(operation, LOGIC_SYMBOL_FILTER); StringBuilder builder = new StringBuilder(); boolean[] priorityCases = priorityOutputs(logicSymbols.substring(1), B + C); for (int i = 0; i < TEST_CASES.length; i++) - builder.append(outsideOutputs(logicSymbols.substring(0, 1), TEST_CASES[i][A], priorityCases[i])); + builder.append(appendCharacter(logicSymbols.substring(0, 1), TEST_CASES[i][A], priorityCases[i])); return builder.toString(); } - /** - * Performs the operation on the two variables inside the original equation's () - * and returns the outputs from those test cases - * - * @param func The logic selector - * @param priorityNum The determining number to reflect whether the method call came from ab or bc - * @return A set of booleans that reflect all test cases possible for the operation inside the () - */ - private static boolean[] priorityOutputs(String func, int priorityNum) { + private static boolean[] priorityOutputs(String textOperator, int priorityNum) { boolean[] out = new boolean[TEST_CASES.length]; - if (priorityNum == 1) { - for (int i = 0; i < TEST_CASES.length; i++) - out[i] = functionMap(func, TEST_CASES[i][A], TEST_CASES[i][B]); - } else { - for (int i = 0; i < TEST_CASES.length; i++) - out[i] = functionMap(func, TEST_CASES[i][B], TEST_CASES[i][C]); + LogicOperator operator = LOGIC_SYMBOL_TO_ENUM_MAP.get(textOperator); + + boolean isPriorityOnFirstTwo = priorityNum == B; + int firstIndex = isPriorityOnFirstTwo ? A : B; + int secondIndex = isPriorityOnFirstTwo ? B : C; + + for (int i = 0; i < TEST_CASES.length; i++) { + out[i] = operator.test(TEST_CASES[i][firstIndex], TEST_CASES[i][secondIndex]); } + return out; } - /** - * Returns 0 or 1 based on the boolean operation that gets past in - * - * @param func The logic selector - * @param x 1st bit - * @param y 2nd bit - * @return 1 or 0 based on their respective booleans - */ - private static String outsideOutputs(String func, boolean x, boolean y) { - return functionMap(func, x, y) ? "1" : "0"; + private static char appendCharacter(String textOperator, boolean x, boolean y) { + return LOGIC_SYMBOL_TO_ENUM_MAP.get(textOperator).test(x, y) ? '1' : '0'; } - /** - * Selects the bitwise operation to be executed - * - * @param func The number selector - * @param x 1st bit - * @param y 2nd bit - * @return The result of the operation - */ - private static boolean functionMap(String func, boolean x, boolean y) { - LogicOperator operator = switch (func) { - case "∧" -> AND; - case "∨" -> OR; - case "↓" -> NOR; - case "⊻" -> XOR; - case "|" -> NAND; - case "↔" -> XNOR; - case "→" -> IMPLIES; - default -> IMPLIED_BY; - }; + static { + AB_PRIORITY_PATTERN = "\\(A" + LOGIC_REGEX + "B\\)" + LOGIC_REGEX + "C"; + BC_PRIORITY_PATTERN = "A" + LOGIC_REGEX + "\\(B" + LOGIC_REGEX + "C\\)"; - return operator.test(x, y); + TEST_CASES = new boolean[][]{ + {false, false, false}, + {false, false, true}, + {false, true, false}, + {true, false, false}, + {false, true, true}, + {true, false, true}, + {true, true, false}, + {true, true, true} + }; } } diff --git a/core/src/main/java/core/bomb/modules/dh/fast_math/FastMath.java b/core/src/main/java/core/bomb/modules/dh/fast_math/FastMath.java index 2d5a996..d780e94 100644 --- a/core/src/main/java/core/bomb/modules/dh/fast_math/FastMath.java +++ b/core/src/main/java/core/bomb/modules/dh/fast_math/FastMath.java @@ -12,33 +12,42 @@ public class FastMath extends Widget { private static final int[][] INTERNAL_GRID; + /** + * Turns the 2 letter combination of input turns it to a 2 number combination based on certain edgework + * and what letters are set as input + * + * @param letters The 2 letters that must be input (Case insensitive) + * @return The combination of numbers that the expert gives back to the defuser + * @throws IllegalArgumentException If the input doesn't match specifications OR the serial code isn't given + */ public static @NotNull String solve(@NotNull String letters) throws IllegalArgumentException { checkSerialCode(); letters = letters.toUpperCase(); if (!letters.matches("[ABCDEGKNPSTXZ]{2}")) throw new IllegalArgumentException("Input 2 of the following letters: [A B C D E G K N P S T X Z]"); + int preconditions = evaluateEdgework(); - int leftNum = translateLetter(letters.substring(0, 1)); - int rightNum = translateLetter(letters.substring(1)); + int leftNum = translateLetter(letters.charAt(0)); + int rightNum = translateLetter(letters.charAt(1)); String outputValue = String.valueOf(postConditions(INTERNAL_GRID[leftNum][rightNum] + preconditions)); return (outputValue.length() == 1 ? "0" : "") + outputValue; } - private static int translateLetter(String letter) throws IllegalArgumentException { + private static int translateLetter(char letter) { return switch (letter) { - case "A" -> 0; - case "B" -> 1; - case "C" -> 2; - case "D" -> 3; - case "E" -> 4; - case "G" -> 5; - case "K" -> 6; - case "N" -> 7; - case "P" -> 8; - case "S" -> 9; - case "T" -> 10; - case "X" -> 11; + case 'A' -> 0; + case 'B' -> 1; + case 'C' -> 2; + case 'D' -> 3; + case 'E' -> 4; + case 'G' -> 5; + case 'K' -> 6; + case 'N' -> 7; + case 'P' -> 8; + case 'S' -> 9; + case 'T' -> 10; + case 'X' -> 11; default -> 12; }; } diff --git a/core/src/test/java/core/bomb/BombSimulations.java b/core/src/test/java/core/bomb/BombSimulations.java index e0105b5..3b114d4 100644 --- a/core/src/test/java/core/bomb/BombSimulations.java +++ b/core/src/test/java/core/bomb/BombSimulations.java @@ -23,7 +23,7 @@ public static void thanksBobCenturion() { Widget.setDoubleAs(4); Widget.setNumHolders(2); Widget.setIndicator(ON, BOB); - Widget.setSerialCode("ag42w5"); + Widget.setSerialCode("AG4KW5"); centurionDefaults(); } diff --git a/utility/src/main/java/tools/data/structures/trie/Trie.java b/utility/src/main/java/tools/data/structures/trie/Trie.java index 5457123..376ad59 100644 --- a/utility/src/main/java/tools/data/structures/trie/Trie.java +++ b/utility/src/main/java/tools/data/structures/trie/Trie.java @@ -25,7 +25,7 @@ public Trie(@NotNull Collection startWords) { public void addWords(@NotNull Collection words) { for (String word : words) - addWord(word); + addWord(word.toLowerCase(), 0, root); } public void addWord(final @NotNull String word) { diff --git a/utility/src/main/java/tools/logic/LogicOperator.java b/utility/src/main/java/tools/logic/LogicOperator.java index d516351..bea5fd9 100644 --- a/utility/src/main/java/tools/logic/LogicOperator.java +++ b/utility/src/main/java/tools/logic/LogicOperator.java @@ -1,58 +1,87 @@ package tools.logic; +import java.util.EnumSet; +import java.util.Map; +import java.util.TreeMap; + +import static java.util.function.UnaryOperator.identity; +import static java.util.stream.Collectors.toMap; + public enum LogicOperator implements BooleanOperation { - AND { + NOT { @Override public boolean test(boolean bitOne, boolean bitTwo) { - return bitOne && bitTwo; + return !bitOne; } }, - OR { + AND("∧") { @Override public boolean test(boolean bitOne, boolean bitTwo) { - return bitOne || bitTwo; + return bitOne && bitTwo; } }, - XOR { + OR("∨") { @Override public boolean test(boolean bitOne, boolean bitTwo) { - return ((bitOne && !bitTwo) || (!bitOne && bitTwo)); + return bitOne || bitTwo; } }, - NOT { + XOR("⊻") { @Override public boolean test(boolean bitOne, boolean bitTwo) { - return !bitOne; + return ((bitOne && !bitTwo) || (!bitOne && bitTwo)); } }, - NOR { + NOR("↓") { @Override public boolean test(boolean bitOne, boolean bitTwo) { return !OR.test(bitOne, bitTwo); } }, - NAND { + NAND("|") { @Override public boolean test(boolean bitOne, boolean bitTwo) { return !AND.test(bitOne, bitTwo); } }, - XNOR { + XNOR("↔") { @Override public boolean test(boolean bitOne, boolean bitTwo) { return !XOR.test(bitOne, bitTwo); } }, - IMPLIES { + IMPLIES("→") { @Override public boolean test(boolean bitOne, boolean bitTwo) { return !(bitOne && !bitTwo); } }, - IMPLIED_BY { + IMPLIED_BY("←") { @Override public boolean test(boolean bitOne, boolean bitTwo) { return !(!bitOne && bitTwo); } + }; + + private final String symbol; + + LogicOperator() { + symbol = null; + } + + LogicOperator(String symbol) { + this.symbol = symbol; + } + + public static final Map LOGIC_SYMBOL_TO_ENUM_MAP; + + static { + LOGIC_SYMBOL_TO_ENUM_MAP = new TreeMap<>(EnumSet.range(AND, IMPLIED_BY) + .stream() + .collect(toMap( + logicSymbol -> logicSymbol.symbol, + identity() + )) + ); } }