From 35de251994fcebff3e19d2a22fe03b96391b51a6 Mon Sep 17 00:00:00 2001 From: HenryCullom <54919530+HenryCullom@users.noreply.github.com> Date: Fri, 1 Jul 2022 14:59:56 -0400 Subject: [PATCH 1/2] learned how / where rules are added to board, started work on translating that to elements --- src/main/java/edu/rpi/legup/model/Puzzle.java | 22 +++++++++++++++++-- .../rpi/legup/puzzle/PuzzleElementTypes.java | 4 ++++ .../puzzle/nurikabe/elements/BlackTile.java | 9 ++++++++ .../puzzle/nurikabe/elements/NumberTile.java | 4 ++++ .../puzzle/nurikabe/elements/UnknownTile.java | 4 ++++ .../puzzle/nurikabe/elements/WhiteTile.java | 4 ++++ 6 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 src/main/java/edu/rpi/legup/puzzle/PuzzleElementTypes.java create mode 100644 src/main/java/edu/rpi/legup/puzzle/nurikabe/elements/BlackTile.java create mode 100644 src/main/java/edu/rpi/legup/puzzle/nurikabe/elements/NumberTile.java create mode 100644 src/main/java/edu/rpi/legup/puzzle/nurikabe/elements/UnknownTile.java create mode 100644 src/main/java/edu/rpi/legup/puzzle/nurikabe/elements/WhiteTile.java diff --git a/src/main/java/edu/rpi/legup/model/Puzzle.java b/src/main/java/edu/rpi/legup/model/Puzzle.java index ef683375d..b9d431d20 100644 --- a/src/main/java/edu/rpi/legup/model/Puzzle.java +++ b/src/main/java/edu/rpi/legup/model/Puzzle.java @@ -1,7 +1,6 @@ package edu.rpi.legup.model; -import edu.rpi.legup.model.elements.NonPlaceableElement; -import edu.rpi.legup.model.elements.PlaceableElement; +import edu.rpi.legup.model.elements.*; import edu.rpi.legup.model.gameboard.Board; import edu.rpi.legup.model.gameboard.ElementFactory; import edu.rpi.legup.model.observer.IBoardListener; @@ -13,6 +12,7 @@ import edu.rpi.legup.model.tree.TreeElement; import edu.rpi.legup.model.tree.TreeElementType; import edu.rpi.legup.model.tree.TreeNode; +import edu.rpi.legup.puzzle.nurikabe.NurikabeType; import edu.rpi.legup.ui.puzzleeditorui.elementsview.NonPlaceableElementPanel; import edu.rpi.legup.utility.LegupUtils; import org.w3c.dom.Document; @@ -76,6 +76,24 @@ public Puzzle() { registerRules(); } + private void registerPuzzleElements() { + ArrayList PlaceableTypes = getPlaceableElementTypes(); + ArrayList NonPlaceableTypes = getNonPlaceableElementTypes(); + } + + private ArrayList getPlaceableElementTypes() { + ArrayList r = new ArrayList(); + + + return r; + } + private ArrayList getNonPlaceableElementTypes() { + ArrayList r = new ArrayList(); + + + return r; + } + private void registerRules() { String packageName = this.getClass().getPackage().toString().replace("package ", ""); diff --git a/src/main/java/edu/rpi/legup/puzzle/PuzzleElementTypes.java b/src/main/java/edu/rpi/legup/puzzle/PuzzleElementTypes.java new file mode 100644 index 000000000..f2ddb056c --- /dev/null +++ b/src/main/java/edu/rpi/legup/puzzle/PuzzleElementTypes.java @@ -0,0 +1,4 @@ +package edu.rpi.legup.puzzle; + +public enum PuzzleElementTypes { +} diff --git a/src/main/java/edu/rpi/legup/puzzle/nurikabe/elements/BlackTile.java b/src/main/java/edu/rpi/legup/puzzle/nurikabe/elements/BlackTile.java new file mode 100644 index 000000000..4b496a251 --- /dev/null +++ b/src/main/java/edu/rpi/legup/puzzle/nurikabe/elements/BlackTile.java @@ -0,0 +1,9 @@ +package edu.rpi.legup.puzzle.nurikabe.elements; + +import edu.rpi.legup.model.elements.PlaceableElement; + +public class BlackTile extends PlaceableElement { + public BlackTile() { + super("NURI-PLAC-0001", "Black Tile", "The black tile", "edu/rpi/legup/images/nurikabe/rules/BetweenRegions.png"); + } +} diff --git a/src/main/java/edu/rpi/legup/puzzle/nurikabe/elements/NumberTile.java b/src/main/java/edu/rpi/legup/puzzle/nurikabe/elements/NumberTile.java new file mode 100644 index 000000000..b226d3851 --- /dev/null +++ b/src/main/java/edu/rpi/legup/puzzle/nurikabe/elements/NumberTile.java @@ -0,0 +1,4 @@ +package edu.rpi.legup.puzzle.nurikabe.elements; + +public class NumberTile { +} diff --git a/src/main/java/edu/rpi/legup/puzzle/nurikabe/elements/UnknownTile.java b/src/main/java/edu/rpi/legup/puzzle/nurikabe/elements/UnknownTile.java new file mode 100644 index 000000000..37f4af07a --- /dev/null +++ b/src/main/java/edu/rpi/legup/puzzle/nurikabe/elements/UnknownTile.java @@ -0,0 +1,4 @@ +package edu.rpi.legup.puzzle.nurikabe.elements; + +public class UnknownTile { +} diff --git a/src/main/java/edu/rpi/legup/puzzle/nurikabe/elements/WhiteTile.java b/src/main/java/edu/rpi/legup/puzzle/nurikabe/elements/WhiteTile.java new file mode 100644 index 000000000..693f72640 --- /dev/null +++ b/src/main/java/edu/rpi/legup/puzzle/nurikabe/elements/WhiteTile.java @@ -0,0 +1,4 @@ +package edu.rpi.legup.puzzle.nurikabe.elements; + +public class WhiteTile { +} From 1268de2f229b3fdce58e5183a75e13b5e1126549 Mon Sep 17 00:00:00 2001 From: HenryCullom <54919530+HenryCullom@users.noreply.github.com> Date: Fri, 15 Jul 2022 14:21:31 -0400 Subject: [PATCH 2/2] added puzzle element classes to nurikabe added registerPuzzleElements method to Puzzle class. still won't show elements in UI --- src/main/java/edu/rpi/legup/model/Puzzle.java | 54 ++++++++++++++----- .../edu/rpi/legup/model/elements/Element.java | 1 + .../legup/model/elements/RegisterElement.java | 11 ++++ .../puzzle/nurikabe/elements/NumberTile.java | 7 ++- .../puzzle/nurikabe/elements/UnknownTile.java | 7 ++- .../puzzle/nurikabe/elements/WhiteTile.java | 7 ++- 6 files changed, 72 insertions(+), 15 deletions(-) create mode 100644 src/main/java/edu/rpi/legup/model/elements/RegisterElement.java diff --git a/src/main/java/edu/rpi/legup/model/Puzzle.java b/src/main/java/edu/rpi/legup/model/Puzzle.java index 4750da80d..3b641bec5 100644 --- a/src/main/java/edu/rpi/legup/model/Puzzle.java +++ b/src/main/java/edu/rpi/legup/model/Puzzle.java @@ -16,7 +16,7 @@ import edu.rpi.legup.ui.puzzleeditorui.elementsview.NonPlaceableElementPanel; import edu.rpi.legup.utility.LegupUtils; import org.w3c.dom.Document; -import org.w3c.dom.Element; +import edu.rpi.legup.model.elements.Element; import org.w3c.dom.Node; import edu.rpi.legup.save.InvalidFileFormatException; import edu.rpi.legup.ui.boardview.BoardView; @@ -74,24 +74,54 @@ public Puzzle() { this.nonPlaceableElements = new ArrayList<>(); registerRules(); + registerPuzzleElements(); } private void registerPuzzleElements() { - ArrayList PlaceableTypes = getPlaceableElementTypes(); - ArrayList NonPlaceableTypes = getNonPlaceableElementTypes(); - } + String packageName = this.getClass().getPackage().toString().replace("package ", ""); - private ArrayList getPlaceableElementTypes() { - ArrayList r = new ArrayList(); + try { + Class[] possElements = LegupUtils.getClasses(packageName); + for (Class c : possElements) { - return r; - } - private ArrayList getNonPlaceableElementTypes() { - ArrayList r = new ArrayList(); + System.out.println("possible element: " + c.getName()); + + //check that the element is not abstract + if (Modifier.isAbstract(c.getModifiers())) continue; + + for (Annotation a : c.getAnnotations()) { + if (a.annotationType() == RegisterElement.class) { + RegisterElement registerElement = (RegisterElement) a; + Constructor cons = c.getConstructor(); + try { + Element element = (Element) cons.newInstance(); + switch (element.getElementType()) { + case PLACEABLE: + this.addPlaceableElement((PlaceableElement) element); + break; + case NONPLACEABLE: + this.addNonPlaceableElement((NonPlaceableElement) element); + break; + default: + break; + } + } catch (InvocationTargetException e) { + System.out.println(" Failed "); + e.getTargetException().printStackTrace(); + } + } + } + } - return r; +// } catch (IOException | ClassNotFoundException | NoSuchMethodException | +// InstantiationException | IllegalAccessException | InvocationTargetException e) { +// LOGGER.error("Unable to find rules for " + this.getClass().getSimpleName(), e); +// } + }catch(Exception e){ + LOGGER.error("Unable to find elements for " + this.getClass().getSimpleName(), e); + } } private void registerRules() { @@ -243,7 +273,7 @@ public void importPuzzle(InputStream inputStream) throws InvalidFileFormatExcept throw new InvalidFileFormatException("Could not find file"); } - Element rootNode = document.getDocumentElement(); + org.w3c.dom.Element rootNode = document.getDocumentElement(); if (rootNode.getTagName().equals("Legup")) { Node node = rootNode.getElementsByTagName("puzzle").item(0); if (importer == null) { diff --git a/src/main/java/edu/rpi/legup/model/elements/Element.java b/src/main/java/edu/rpi/legup/model/elements/Element.java index 0fdabe383..44c088f64 100644 --- a/src/main/java/edu/rpi/legup/model/elements/Element.java +++ b/src/main/java/edu/rpi/legup/model/elements/Element.java @@ -6,6 +6,7 @@ import java.awt.*; import java.awt.image.BufferedImage; +@RegisterElement public abstract class Element { protected String elementID; protected String elementName; diff --git a/src/main/java/edu/rpi/legup/model/elements/RegisterElement.java b/src/main/java/edu/rpi/legup/model/elements/RegisterElement.java new file mode 100644 index 000000000..bda3f3070 --- /dev/null +++ b/src/main/java/edu/rpi/legup/model/elements/RegisterElement.java @@ -0,0 +1,11 @@ +package edu.rpi.legup.model.elements; + +import java.lang.annotation.*; +import java.lang.annotation.ElementType; + +@Inherited +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +public @interface RegisterElement { + +} diff --git a/src/main/java/edu/rpi/legup/puzzle/nurikabe/elements/NumberTile.java b/src/main/java/edu/rpi/legup/puzzle/nurikabe/elements/NumberTile.java index b226d3851..4444fe52c 100644 --- a/src/main/java/edu/rpi/legup/puzzle/nurikabe/elements/NumberTile.java +++ b/src/main/java/edu/rpi/legup/puzzle/nurikabe/elements/NumberTile.java @@ -1,4 +1,9 @@ package edu.rpi.legup.puzzle.nurikabe.elements; -public class NumberTile { +import edu.rpi.legup.model.elements.NonPlaceableElement; + +public class NumberTile extends NonPlaceableElement { + public NumberTile() { + super("NURI-UNPL-0001", "Number Tile", "A numbered tile", "edu/rpi/legup/images/nurikabe/rules/BetweenRegions.png"); + } } diff --git a/src/main/java/edu/rpi/legup/puzzle/nurikabe/elements/UnknownTile.java b/src/main/java/edu/rpi/legup/puzzle/nurikabe/elements/UnknownTile.java index 37f4af07a..aeb840263 100644 --- a/src/main/java/edu/rpi/legup/puzzle/nurikabe/elements/UnknownTile.java +++ b/src/main/java/edu/rpi/legup/puzzle/nurikabe/elements/UnknownTile.java @@ -1,4 +1,9 @@ package edu.rpi.legup.puzzle.nurikabe.elements; -public class UnknownTile { +import edu.rpi.legup.model.elements.NonPlaceableElement; + +public class UnknownTile extends NonPlaceableElement { + public UnknownTile() { + super("NURI-UNPL-0002", "Unknown Tile", "A blank tile", "edu/rpi/legup/images/nurikabe/rules/BetweenRegions.png"); + } } diff --git a/src/main/java/edu/rpi/legup/puzzle/nurikabe/elements/WhiteTile.java b/src/main/java/edu/rpi/legup/puzzle/nurikabe/elements/WhiteTile.java index 693f72640..116575bc2 100644 --- a/src/main/java/edu/rpi/legup/puzzle/nurikabe/elements/WhiteTile.java +++ b/src/main/java/edu/rpi/legup/puzzle/nurikabe/elements/WhiteTile.java @@ -1,4 +1,9 @@ package edu.rpi.legup.puzzle.nurikabe.elements; -public class WhiteTile { +import edu.rpi.legup.model.elements.PlaceableElement; + +public class WhiteTile extends PlaceableElement { + public WhiteTile() { + super("NURI-PLAC-0002", "White Tile", "The white tile", "edu/rpi/legup/images/nurikabe/rules/BetweenRegions.png"); + } }