From 9d9e3acb59e1abd6d6b4181dd2398885d8668e0f Mon Sep 17 00:00:00 2001 From: pitbull51067 <103721450+pitbull51067@users.noreply.github.com> Date: Fri, 24 Feb 2023 16:29:06 -0500 Subject: [PATCH] Issue #139: Made Tree Tent use TreeTentType (#464) * Changes to `TreeTent`. * Changes to `TreeTent`. --------- Co-authored-by: Ivan Ho <41582274+Corppet@users.noreply.github.com> --- .../legup/puzzle/treetent/ClueCommand.java | 4 +- .../legup/puzzle/treetent/TreeTentCell.java | 45 ++++++------------- .../puzzle/treetent/TreeTentCellFactory.java | 2 +- .../puzzle/treetent/TreeTentController.java | 8 ++-- .../puzzle/treetent/TreeTentExporter.java | 2 +- .../puzzle/treetent/TreeTentImporter.java | 6 +-- .../legup/puzzle/treetent/TreeTentType.java | 21 ++++++--- .../treetent/rules/EmptyFieldDirectRule.java | 2 +- .../treetent/rules/FillinRowCaseRule.java | 4 +- .../rules/FinishWithGrassDirectRule.java | 2 +- .../rules/FinishWithTentsDirectRule.java | 2 +- .../rules/LastCampingSpotDirectRule.java | 2 +- .../SurroundTentWithGrassDirectRule.java | 2 +- .../treetent/rules/TentOrGrassCaseRule.java | 4 +- 14 files changed, 48 insertions(+), 58 deletions(-) diff --git a/src/main/java/edu/rpi/legup/puzzle/treetent/ClueCommand.java b/src/main/java/edu/rpi/legup/puzzle/treetent/ClueCommand.java index 7ada10a07..30af5af51 100644 --- a/src/main/java/edu/rpi/legup/puzzle/treetent/ClueCommand.java +++ b/src/main/java/edu/rpi/legup/puzzle/treetent/ClueCommand.java @@ -70,7 +70,7 @@ public void executeCommand() { for (TreeTentCell cell : tempList) { cell = (TreeTentCell) board.getPuzzleElement(cell); - cell.setData(TreeTentType.GRASS.value); + cell.setData(TreeTentType.GRASS); board.addModifiedData(cell); finalTran.propagateChange(cell); @@ -170,7 +170,7 @@ public void undoCommand() { for (TreeTentCell cell : tempList) { cell = (TreeTentCell) board.getPuzzleElement(cell); - cell.setData(TreeTentType.UNKNOWN.value); + cell.setData(TreeTentType.UNKNOWN); board.removeModifiedData(cell); final TreeTentCell finalCell = cell; diff --git a/src/main/java/edu/rpi/legup/puzzle/treetent/TreeTentCell.java b/src/main/java/edu/rpi/legup/puzzle/treetent/TreeTentCell.java index bab45f4ab..290e0858d 100644 --- a/src/main/java/edu/rpi/legup/puzzle/treetent/TreeTentCell.java +++ b/src/main/java/edu/rpi/legup/puzzle/treetent/TreeTentCell.java @@ -6,46 +6,27 @@ import java.awt.*; import java.awt.event.MouseEvent; -public class TreeTentCell extends GridCell { +public class TreeTentCell extends GridCell { - public TreeTentCell(int valueInt, Point location) { - super(valueInt, location); + public TreeTentCell(TreeTentType value, Point location) { + super(value, location); } public TreeTentType getType() { - switch (data) { - case 0: - return TreeTentType.UNKNOWN; - case 1: - return TreeTentType.TREE; - case 2: - return TreeTentType.GRASS; - case 3: - return TreeTentType.TENT; - default: - return null; - } + return data; } @Override public void setType(Element e, MouseEvent m) { - if (e.getElementName().equals("Unknown Tile")) { - this.data = 0; - } - else { - if (e.getElementName().equals("Tree Tile")) { - this.data = 1; - } - else { - if (e.getElementName().equals("Grass Tile")) { - this.data = 2; - } - else { - if (e.getElementName().equals("Tent Tile")) { - this.data = 3; - } - } - } + switch (e.getElementName()) { + case "Unknown Tile": + this.data = TreeTentType.UNKNOWN; + case "Tree Tile": + this.data = TreeTentType.TREE; + case "Grass Tile": + this.data = TreeTentType.GRASS; + case "Tent Tile": + this.data = TreeTentType.TENT; } } diff --git a/src/main/java/edu/rpi/legup/puzzle/treetent/TreeTentCellFactory.java b/src/main/java/edu/rpi/legup/puzzle/treetent/TreeTentCellFactory.java index 9543c89dc..e5e7603a6 100644 --- a/src/main/java/edu/rpi/legup/puzzle/treetent/TreeTentCellFactory.java +++ b/src/main/java/edu/rpi/legup/puzzle/treetent/TreeTentCellFactory.java @@ -38,7 +38,7 @@ public PuzzleElement importCell(Node node, Board board) throws InvalidFileFormat throw new InvalidFileFormatException("TreeTent Factory: cell unknown value"); } - TreeTentCell cell = new TreeTentCell(value, new Point(x, y)); + TreeTentCell cell = new TreeTentCell(TreeTentType.valueOf(value), new Point(x, y)); cell.setIndex(y * height + x); return cell; } diff --git a/src/main/java/edu/rpi/legup/puzzle/treetent/TreeTentController.java b/src/main/java/edu/rpi/legup/puzzle/treetent/TreeTentController.java index 41ec8a854..1a5416c0d 100644 --- a/src/main/java/edu/rpi/legup/puzzle/treetent/TreeTentController.java +++ b/src/main/java/edu/rpi/legup/puzzle/treetent/TreeTentController.java @@ -111,11 +111,11 @@ public TreeTentController() { public void changeCell(MouseEvent e, PuzzleElement element) { TreeTentCell cell = (TreeTentCell) element; if (e.getButton() == MouseEvent.BUTTON1) { - if (cell.getData() == 0) { + if (cell.getData() == TreeTentType.UNKNOWN) { element.setData(2); } else { - if (cell.getData() == 2) { + if (cell.getData() == TreeTentType.GRASS) { element.setData(3); } else { @@ -125,11 +125,11 @@ public void changeCell(MouseEvent e, PuzzleElement element) { } else { if (e.getButton() == MouseEvent.BUTTON3) { - if (cell.getData() == 0) { + if (cell.getData() == TreeTentType.UNKNOWN) { element.setData(3); } else { - if (cell.getData() == 2) { + if (cell.getData() == TreeTentType.GRASS) { element.setData(0); } else { diff --git a/src/main/java/edu/rpi/legup/puzzle/treetent/TreeTentExporter.java b/src/main/java/edu/rpi/legup/puzzle/treetent/TreeTentExporter.java index 510114e46..828a5ffe0 100644 --- a/src/main/java/edu/rpi/legup/puzzle/treetent/TreeTentExporter.java +++ b/src/main/java/edu/rpi/legup/puzzle/treetent/TreeTentExporter.java @@ -32,7 +32,7 @@ protected org.w3c.dom.Element createBoardElement(Document newDocument) { org.w3c.dom.Element cellsElement = newDocument.createElement("cells"); for (PuzzleElement puzzleElement : board.getPuzzleElements()) { TreeTentCell cell = (TreeTentCell) puzzleElement; - if (cell.getData() != 0) { + if (cell.getData() != TreeTentType.UNKNOWN) { org.w3c.dom.Element cellElement = puzzle.getFactory().exportCell(newDocument, puzzleElement); cellsElement.appendChild(cellElement); } diff --git a/src/main/java/edu/rpi/legup/puzzle/treetent/TreeTentImporter.java b/src/main/java/edu/rpi/legup/puzzle/treetent/TreeTentImporter.java index 935d8f980..acd094a1b 100644 --- a/src/main/java/edu/rpi/legup/puzzle/treetent/TreeTentImporter.java +++ b/src/main/java/edu/rpi/legup/puzzle/treetent/TreeTentImporter.java @@ -27,7 +27,7 @@ public void initializeBoard(int rows, int columns) { for (int y = 0; y < rows; y++) { for (int x = 0; x < columns; x++) { if (treeTentBoard.getCell(x, y) == null) { - TreeTentCell cell = new TreeTentCell(0, new Point(x, y)); + TreeTentCell cell = new TreeTentCell(TreeTentType.UNKNOWN, new Point(x, y)); cell.setIndex(y * columns + x); cell.setModifiable(true); treeTentBoard.setCell(x, y, cell); @@ -88,7 +88,7 @@ public void initializeBoard(Node node) throws InvalidFileFormatException { for (int i = 0; i < elementDataList.getLength(); i++) { TreeTentCell cell = (TreeTentCell) puzzle.getFactory().importCell(elementDataList.item(i), treeTentBoard); Point loc = cell.getLocation(); - if (cell.getData() != 0) { + if (cell.getData() != TreeTentType.UNKNOWN) { cell.setModifiable(false); cell.setGiven(true); } @@ -98,7 +98,7 @@ public void initializeBoard(Node node) throws InvalidFileFormatException { for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { if (treeTentBoard.getCell(x, y) == null) { - TreeTentCell cell = new TreeTentCell(0, new Point(x, y)); + TreeTentCell cell = new TreeTentCell(TreeTentType.UNKNOWN, new Point(x, y)); cell.setIndex(y * height + x); cell.setModifiable(true); treeTentBoard.setCell(x, y, cell); diff --git a/src/main/java/edu/rpi/legup/puzzle/treetent/TreeTentType.java b/src/main/java/edu/rpi/legup/puzzle/treetent/TreeTentType.java index 5faa88c59..890cdfe29 100644 --- a/src/main/java/edu/rpi/legup/puzzle/treetent/TreeTentType.java +++ b/src/main/java/edu/rpi/legup/puzzle/treetent/TreeTentType.java @@ -1,12 +1,21 @@ package edu.rpi.legup.puzzle.treetent; -public enum TreeTentType { - UNKNOWN(0), TREE(1), GRASS(2), TENT(3), - CLUE_NORTH(-1), CLUE_EAST(-2), CLUE_SOUTH(-3), CLUE_WEST(-4); +import edu.rpi.legup.puzzle.masyu.MasyuType; - public int value; +public enum TreeTentType { + UNKNOWN, TREE, GRASS, TENT, + CLUE_NORTH, CLUE_EAST, CLUE_SOUTH, CLUE_WEST; - TreeTentType(int value) { - this.value = value; + public static TreeTentType valueOf(int num) { + switch (num) { + case 1: + return TREE; + case 2: + return GRASS; + case 3: + return TENT; + default: + return UNKNOWN; + } } } \ No newline at end of file diff --git a/src/main/java/edu/rpi/legup/puzzle/treetent/rules/EmptyFieldDirectRule.java b/src/main/java/edu/rpi/legup/puzzle/treetent/rules/EmptyFieldDirectRule.java index f57602114..bd2642497 100644 --- a/src/main/java/edu/rpi/legup/puzzle/treetent/rules/EmptyFieldDirectRule.java +++ b/src/main/java/edu/rpi/legup/puzzle/treetent/rules/EmptyFieldDirectRule.java @@ -72,7 +72,7 @@ public Board getDefaultBoard(TreeNode node) { for (PuzzleElement element : treeTentBoard.getPuzzleElements()) { TreeTentCell cell = (TreeTentCell) element; if (cell.getType() == TreeTentType.UNKNOWN && isForced(treeTentBoard, cell)) { - cell.setData(TreeTentType.GRASS.value); + cell.setData(TreeTentType.GRASS); treeTentBoard.addModifiedData(cell); } } diff --git a/src/main/java/edu/rpi/legup/puzzle/treetent/rules/FillinRowCaseRule.java b/src/main/java/edu/rpi/legup/puzzle/treetent/rules/FillinRowCaseRule.java index b762c7875..e88e321c4 100644 --- a/src/main/java/edu/rpi/legup/puzzle/treetent/rules/FillinRowCaseRule.java +++ b/src/main/java/edu/rpi/legup/puzzle/treetent/rules/FillinRowCaseRule.java @@ -90,12 +90,12 @@ private ArrayList genCombRecursive(TreeTentBoard iBoard, List getCases(Board board, PuzzleElement puzzleElement) { ArrayList cases = new ArrayList<>(); Board case1 = board.copy(); PuzzleElement data1 = case1.getPuzzleElement(puzzleElement); - data1.setData(TreeTentType.TENT.value); + data1.setData(TreeTentType.TENT); case1.addModifiedData(data1); cases.add(case1); Board case2 = board.copy(); PuzzleElement data2 = case2.getPuzzleElement(puzzleElement); - data2.setData(TreeTentType.GRASS.value); + data2.setData(TreeTentType.GRASS); case2.addModifiedData(data2); cases.add(case2);