From dcb957307cca2220d39b4d0a32aef209200f8686 Mon Sep 17 00:00:00 2001 From: ZevCe <157339070+ZevCe@users.noreply.github.com> Date: Tue, 16 Apr 2024 16:57:42 -0400 Subject: [PATCH] Last minute comments (#810) --- .../legup/puzzle/thermometer/Thermometer.java | 3 ++ .../puzzle/thermometer/ThermometerBoard.java | 18 ++++++---- .../puzzle/thermometer/ThermometerCell.java | 2 ++ .../thermometer/ThermometerController.java | 4 +-- .../thermometer/ThermometerElementView.java | 4 +++ .../thermometer/ThermometerImporter.java | 35 ++++++++++++++----- .../puzzle/thermometer/ThermometerVial.java | 7 ++-- .../puzzle/thermometer/ThermometerView.java | 9 +++-- 8 files changed, 58 insertions(+), 24 deletions(-) diff --git a/src/main/java/edu/rpi/legup/puzzle/thermometer/Thermometer.java b/src/main/java/edu/rpi/legup/puzzle/thermometer/Thermometer.java index 40c8a8461..698a5d078 100644 --- a/src/main/java/edu/rpi/legup/puzzle/thermometer/Thermometer.java +++ b/src/main/java/edu/rpi/legup/puzzle/thermometer/Thermometer.java @@ -3,6 +3,7 @@ import edu.rpi.legup.model.Puzzle; import edu.rpi.legup.model.gameboard.Board; +//basically just copy-pasted from dev guide on wiki public class Thermometer extends Puzzle { public Thermometer() { super(); @@ -11,6 +12,8 @@ public Thermometer() { this.importer = new ThermometerImporter(this); this.exporter = new ThermometerExporter(this); + //we do not have a thermometerCellFactory class as + //thermometerVial has its own thermometerCell factory method } /** Initializes the game board. Called by the invoker of the class */ diff --git a/src/main/java/edu/rpi/legup/puzzle/thermometer/ThermometerBoard.java b/src/main/java/edu/rpi/legup/puzzle/thermometer/ThermometerBoard.java index f2de37902..08e617286 100644 --- a/src/main/java/edu/rpi/legup/puzzle/thermometer/ThermometerBoard.java +++ b/src/main/java/edu/rpi/legup/puzzle/thermometer/ThermometerBoard.java @@ -1,7 +1,6 @@ package edu.rpi.legup.puzzle.thermometer; import edu.rpi.legup.model.gameboard.GridBoard; -import edu.rpi.legup.model.gameboard.GridCell; import java.awt.*; import java.util.ArrayList; @@ -22,7 +21,8 @@ public class ThermometerBoard extends GridBoard{ public ThermometerBoard(int width, int height) { super(width, height); - //filling the arrays with zeros, so they can be properly updated later + //initializing the row/col number arrays with zeros, so they can be + //easily updated using the setRow/ColNumber functions colNumbers = new ArrayList<>(); for (int i = 0; i < width-1; i++) { ThermometerCell cell = new ThermometerCell(new Point(i, height-1), @@ -40,12 +40,13 @@ public ThermometerBoard(int width, int height) { this.setCell(width-1, i, cell); } - //setting a dummy cell so board doesnt have null cells + //setting a dummy cell so board doesn't have null cells dummyCell = new ThermometerCell(new Point(width-1, height-1), ThermometerType.UNKNOWN, ThermometerFill.UNKNOWN, -1); dummyCell.setIndex((height-1) * height + width); this.setCell(width-1, height-1, dummyCell); + //creating our empty vial of thermometers to add to thermometerVials = new ArrayList<>(); } @@ -58,7 +59,7 @@ public ArrayList getVials() { } - //our getters for row/col numbers with simple input verification + //our setters for row/col numbers with simple input verification public boolean setRowNumber(int row, int num) { //first check is to verify we are updating an element in range //second check is to verify the new number can be achieved by the puzzle @@ -78,11 +79,13 @@ public boolean setColNumber(int col, int num) { return false; } - //basic accessors, probably fine as is + //basic accessors for row/col numbers public int getRowNumber(int row){ + if (row < 0 || row >= rowNumbers.size()) return -1; return rowNumbers.get(row).getRotation(); } public int getColNumber(int col){ + if (col < 0 || col >= rowNumbers.size()) return -1; return colNumbers.get(col).getRotation(); } @@ -92,7 +95,10 @@ public int getColNumber(int col){ //we all suck at programming so instead of using provided array list - //instead just trusting vials to store the cells for us + //we use our own array lists to keep track of the vials + //marginally useful because it means we are guaranteed to get a + //thermometer cell when calling get cell, but using some type casting + //this override function could very likely be refactored out @Override public ThermometerCell getCell(int x, int y) { for(ThermometerVial vial: this.thermometerVials) { diff --git a/src/main/java/edu/rpi/legup/puzzle/thermometer/ThermometerCell.java b/src/main/java/edu/rpi/legup/puzzle/thermometer/ThermometerCell.java index 748e2ba78..87cc69875 100644 --- a/src/main/java/edu/rpi/legup/puzzle/thermometer/ThermometerCell.java +++ b/src/main/java/edu/rpi/legup/puzzle/thermometer/ThermometerCell.java @@ -5,6 +5,8 @@ import java.awt.Point; public class ThermometerCell extends GridCell { + + //information about the cell needed to display it private ThermometerType type; private ThermometerFill fill; private int rotation; diff --git a/src/main/java/edu/rpi/legup/puzzle/thermometer/ThermometerController.java b/src/main/java/edu/rpi/legup/puzzle/thermometer/ThermometerController.java index b2128da28..31a80e2af 100644 --- a/src/main/java/edu/rpi/legup/puzzle/thermometer/ThermometerController.java +++ b/src/main/java/edu/rpi/legup/puzzle/thermometer/ThermometerController.java @@ -7,12 +7,12 @@ public class ThermometerController extends ElementController { + //method for updating thermometer cells since number cells have unknown for + //their fill type we don't need to worry about end user modifying them with this @Override public void changeCell(MouseEvent e, PuzzleElement data) { ThermometerCell cell = (ThermometerCell) data; -// System.out.println("Sanity Check"); - if (e.getButton() == MouseEvent.BUTTON1) { if (e.isControlDown()) { this.boardView.getSelectionPopupMenu().show(boardView, this.boardView.getCanvas().getX() + e.getX(), this.boardView.getCanvas().getY() + e.getY()); diff --git a/src/main/java/edu/rpi/legup/puzzle/thermometer/ThermometerElementView.java b/src/main/java/edu/rpi/legup/puzzle/thermometer/ThermometerElementView.java index 39bd1716b..99d04c1e9 100644 --- a/src/main/java/edu/rpi/legup/puzzle/thermometer/ThermometerElementView.java +++ b/src/main/java/edu/rpi/legup/puzzle/thermometer/ThermometerElementView.java @@ -8,6 +8,7 @@ public class ThermometerElementView extends GridElementView { + //mixture of stuff stolen from tree tent and dev guide private static final Font FONT = new Font("TimesRoman", Font.BOLD, 16); private static final Color FONT_COLOR = Color.BLACK; @@ -20,6 +21,8 @@ public ThermometerCell getPuzzleElement() { return (ThermometerCell) super.getPuzzleElement(); } + //method for drawing a thermometer cell + //basically copy/pasted from tree tent drawing tent images @Override public void drawElement(Graphics2D graphics2D) { @@ -41,6 +44,7 @@ public void drawElement(Graphics2D graphics2D) { graphics2D.drawRect(location.x, location.y, size.width, size.height); } + //modified code from tree trent to display images private Image imageSrc(ThermometerType t, ThermometerFill f, int r) { //will have a 36 switch case at end to determine which image gets opened diff --git a/src/main/java/edu/rpi/legup/puzzle/thermometer/ThermometerImporter.java b/src/main/java/edu/rpi/legup/puzzle/thermometer/ThermometerImporter.java index 9a251e78d..d9c227a9f 100644 --- a/src/main/java/edu/rpi/legup/puzzle/thermometer/ThermometerImporter.java +++ b/src/main/java/edu/rpi/legup/puzzle/thermometer/ThermometerImporter.java @@ -10,31 +10,34 @@ import static java.lang.Math.min; public class ThermometerImporter extends PuzzleImporter { + + //basic stuff stolen from dev guide/filled in by default public ThermometerImporter(Thermometer thermometer) { super(thermometer); } - @Override public boolean acceptsRowsAndColumnsInput() { return false; } - @Override public boolean acceptsTextInput() { return false; } - @Override - public void initializeBoard(int rows, int columns) { - - } + public void initializeBoard(int rows, int columns) {} + //method for initializing board from an xml file which has + //a provided width/height @Override public void initializeBoard(Node node) throws InvalidFileFormatException { + //sticking everything in a try statement because god has forsaken everyone try { + //checking basic formatting of file if (!node.getNodeName().equalsIgnoreCase("board")) { throw new InvalidFileFormatException("thermometer Importer: cannot find board puzzleElement"); } + + //getting the list of vials to turn into real vials Element boardElement = (Element) node; if (boardElement.getElementsByTagName("vials").getLength() == 0) { throw new InvalidFileFormatException("thermometer Importer: no puzzleElement found for board"); @@ -42,17 +45,22 @@ public void initializeBoard(Node node) throws InvalidFileFormatException { Element dataElement = (Element) boardElement.getElementsByTagName("vials").item(0); NodeList elementDataList = dataElement.getElementsByTagName("vial"); + //checking both a width and height were provided for the board ThermometerBoard thermometerBoard = null; if (!boardElement.getAttribute("width").isEmpty() && !boardElement.getAttribute("height").isEmpty()) { + + //grabbing the height/width of the board int width = Integer.parseInt(boardElement.getAttribute("width")); int height = Integer.parseInt(boardElement.getAttribute("height")); + //grabbing the lists of rowNumbers/colNumbers Element rowElement = (Element) boardElement.getElementsByTagName("rowNumbers").item(0); NodeList rowNodeList = rowElement.getElementsByTagName("row"); Element colElement = (Element) boardElement.getElementsByTagName("colNumbers").item(0); NodeList colNodeList = colElement.getElementsByTagName("col"); + //checking that the number of row and col numbers agrees with height/width of board if (colNodeList.getLength() != width) { throw new InvalidFileFormatException("Mismatch between width and number of colNums.\n colNodeList.length:" + colNodeList.getLength() + " width:" + width); } @@ -60,13 +68,18 @@ public void initializeBoard(Node node) throws InvalidFileFormatException { throw new InvalidFileFormatException("thermometer Importer: no rowNumbers found for board"); } + //finally creating our thermometer board, we add one to the size since row/col numbers + //are considered cells on the grid thermometerBoard = new ThermometerBoard(width + 1, height + 1); + //adding row and column numbers to our board importRowColNums(rowNodeList, colNodeList, thermometerBoard); } else { throw new InvalidFileFormatException("thermometer Importer: invalid board height/width"); } + //grabbing height/width from board, need to subtract 1 + //because grids height/width is 1 bigger than number of vials on board int width = thermometerBoard.getWidth()-1; int height = thermometerBoard.getHeight()-1; @@ -75,7 +88,7 @@ public void initializeBoard(Node node) throws InvalidFileFormatException { importThermometerVial(elementDataList.item(i), thermometerBoard); } - //verifying all vials were used + //verifying all vial cells were filled by vials for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { if (thermometerBoard.getCell(x, y) == null) { @@ -97,6 +110,8 @@ public void initializeBoard(String[] statements) throws UnsupportedOperationExce private void importRowColNums(NodeList rowNodes, NodeList colNodes, ThermometerBoard board) throws InvalidFileFormatException { + //going through our list or row nodes grabbed from the xml file and + //then calling the thermometer boards setRowNumber function to update the value for (int i = 0; i < rowNodes.getLength(); i++) { Node node = rowNodes.item(i); int rowNum = Integer.parseInt(node.getAttributes().getNamedItem("value").getNodeValue()); @@ -105,6 +120,7 @@ private void importRowColNums(NodeList rowNodes, NodeList colNodes, ThermometerB } } + //same process but for col numbers for (int i = 0; i < colNodes.getLength(); i++) { Node node = colNodes.item(i); int colNum = Integer.parseInt(node.getAttributes().getNamedItem("value").getNodeValue()); @@ -115,13 +131,16 @@ private void importRowColNums(NodeList rowNodes, NodeList colNodes, ThermometerB } private void importThermometerVial(Node node, ThermometerBoard board) throws InvalidFileFormatException{ + //head is the top of the thermometer and tip is the end of the thermometer + //thermometers in the xml are specified only by their head and tip cells int headX = Integer.parseInt(node.getAttributes().getNamedItem("headx").getNodeValue()); int headY = Integer.parseInt(node.getAttributes().getNamedItem("heady").getNodeValue()); int tipX = Integer.parseInt(node.getAttributes().getNamedItem("tailx").getNodeValue()); int tipY = Integer.parseInt(node.getAttributes().getNamedItem("taily").getNodeValue()); + //making sure we can add the vial before doing so if(verifyVial(headX, headY, tipX, tipY, board)) { - System.out.println("Vial successfully created"); + //adding the vial to the board board.addVial(new ThermometerVial(headX, headY, tipX, tipY, board)); } else { diff --git a/src/main/java/edu/rpi/legup/puzzle/thermometer/ThermometerVial.java b/src/main/java/edu/rpi/legup/puzzle/thermometer/ThermometerVial.java index 2fa6c1c08..d941da11c 100644 --- a/src/main/java/edu/rpi/legup/puzzle/thermometer/ThermometerVial.java +++ b/src/main/java/edu/rpi/legup/puzzle/thermometer/ThermometerVial.java @@ -3,12 +3,12 @@ import java.awt.*; import java.util.ArrayList; -import static java.lang.Math.*; - public class ThermometerVial { private ArrayList cells; public ThermometerVial(int headX, int headY, int tipX, int tipY, ThermometerBoard board) { + //basic constructor, instantiating our members field and then + //calling helper function to do all the heavy lifting cells = new ArrayList(); fillData(headX, headY, tipX, tipY, board); } @@ -61,7 +61,7 @@ private void addCell(int x, int y, ThermometerType t, int rotation, ThermometerB } - //TODO DOES NOT WORK AS INTENDED + //TODO (probably) DOES NOT WORK AS INTENDED // BECAUSE MOST RULES GET A PUZZLE ELEMENT PASSED IN AND WEIRD // TYPE CASTING STUFF, PAY ATTENTION TO THIS WHEN WE START // DEBUGGING RULES @@ -75,6 +75,7 @@ public boolean containsCell(ThermometerCell cell){ return false; } + //basic accessors public ThermometerCell getHead(){return cells.getFirst();} public ThermometerCell getTail(){return cells.getLast();} public ArrayList getCells(){return cells;} diff --git a/src/main/java/edu/rpi/legup/puzzle/thermometer/ThermometerView.java b/src/main/java/edu/rpi/legup/puzzle/thermometer/ThermometerView.java index b48fd7d4a..b628f9790 100644 --- a/src/main/java/edu/rpi/legup/puzzle/thermometer/ThermometerView.java +++ b/src/main/java/edu/rpi/legup/puzzle/thermometer/ThermometerView.java @@ -1,19 +1,17 @@ package edu.rpi.legup.puzzle.thermometer; import edu.rpi.legup.controller.BoardController; -import edu.rpi.legup.model.gameboard.GridCell; -import edu.rpi.legup.model.gameboard.PuzzleElement; import edu.rpi.legup.ui.boardview.GridBoardView; -import javax.imageio.ImageIO; import java.awt.*; -import java.io.IOException; public class ThermometerView extends GridBoardView { public ThermometerView(ThermometerBoard board) { super(new BoardController(), new ThermometerController(), board.getDimension()); + //loop for displaying the vial cells + //stolen largely from dev guide for(ThermometerVial vial : board.getVials()) { for(ThermometerCell cell : vial.getCells()) { Point loc = cell.getLocation(); @@ -25,7 +23,7 @@ public ThermometerView(ThermometerBoard board) { } } - + //loop for displaying row numbers, same as above for(ThermometerCell rowNum : board.getRowNumbers()) { Point loc = rowNum.getLocation(); ThermometerNumberView numberView = new ThermometerNumberView(rowNum); @@ -35,6 +33,7 @@ public ThermometerView(ThermometerBoard board) { elementViews.add(numberView); } + //loop for displaying col numbers, also same as above for(ThermometerCell colNum : board.getColNumbers()) { Point loc = colNum.getLocation(); ThermometerNumberView numberView = new ThermometerNumberView(colNum);