Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Puzzle editor: merge work I've been doing into the actual repository #195

Merged
merged 5 commits into from
Jul 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 52 additions & 4 deletions src/main/java/edu/rpi/legup/model/Puzzle.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -13,10 +12,11 @@
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;
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;
Expand Down Expand Up @@ -74,6 +74,54 @@ public Puzzle() {
this.nonPlaceableElements = new ArrayList<>();

registerRules();
registerPuzzleElements();
}

private void registerPuzzleElements() {
String packageName = this.getClass().getPackage().toString().replace("package ", "");

try {
Class[] possElements = LegupUtils.getClasses(packageName);

for (Class c : possElements) {

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();
}
}
}
}

// } 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() {
Expand Down Expand Up @@ -225,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) {
Expand Down
1 change: 1 addition & 0 deletions src/main/java/edu/rpi/legup/model/elements/Element.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.awt.*;
import java.awt.image.BufferedImage;

@RegisterElement
public abstract class Element {
protected String elementID;
protected String elementName;
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/edu/rpi/legup/model/elements/RegisterElement.java
Original file line number Diff line number Diff line change
@@ -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 {

}
4 changes: 4 additions & 0 deletions src/main/java/edu/rpi/legup/puzzle/PuzzleElementTypes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package edu.rpi.legup.puzzle;

public enum PuzzleElementTypes {
}
Original file line number Diff line number Diff line change
@@ -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");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package edu.rpi.legup.puzzle.nurikabe.elements;

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");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package edu.rpi.legup.puzzle.nurikabe.elements;

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");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package edu.rpi.legup.puzzle.nurikabe.elements;

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");
}
}