Skip to content

Commit

Permalink
Last minute comments (#810)
Browse files Browse the repository at this point in the history
  • Loading branch information
ZevCe authored Apr 16, 2024
1 parent 9b4d581 commit dcb9573
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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 */
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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),
Expand All @@ -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<>();
}

Expand All @@ -58,7 +59,7 @@ public ArrayList<ThermometerVial> 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
Expand All @@ -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();
}

Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import java.awt.Point;

public class ThermometerCell extends GridCell<Integer> {

//information about the cell needed to display it
private ThermometerType type;
private ThermometerFill fill;
private int rotation;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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) {

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,63 +10,76 @@
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");
}
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);
}
if (rowNodeList.getLength() != height) {
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;

Expand All @@ -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) {
Expand All @@ -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());
Expand All @@ -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());
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
import java.awt.*;
import java.util.ArrayList;

import static java.lang.Math.*;

public class ThermometerVial {
private ArrayList<ThermometerCell> 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<ThermometerCell>();
fillData(headX, headY, tipX, tipY, board);
}
Expand Down Expand Up @@ -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
Expand All @@ -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<ThermometerCell> getCells(){return cells;}
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit dcb9573

Please sign in to comment.