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

Thermometer Importer Functionality + Thermometer Puzzle Factory #756

Merged
merged 12 commits into from
Mar 12, 2024
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
package edu.rpi.legup.puzzle.thermometer;

import edu.rpi.legup.model.gameboard.GridBoard;
import edu.rpi.legup.model.gameboard.PuzzleElement;
import edu.rpi.legup.puzzle.thermometer.elements.Vial;

import java.util.ArrayList;

import static edu.rpi.legup.puzzle.thermometer.elements.Vial.verifyVial;
import static edu.rpi.legup.puzzle.thermometer.ThermometerVial.verifyVial;

public class ThermometerBoard extends GridBoard{

//an array containing all of our vials on the board
private ArrayList<Vial> vials;
private ArrayList<ThermometerVial> thermometerVials;

//representations of the number requirements along rows and columns of the board
private ArrayList<Integer> colNumbers;
Expand All @@ -31,7 +29,7 @@ public ThermometerBoard(int width, int height){
rowNumbers.add(0);
}

vials = new ArrayList<>();
thermometerVials = new ArrayList<>();
}
public ThermometerBoard(int size){
super(size, size);
Expand All @@ -44,19 +42,15 @@ public ThermometerBoard(int size){
rowNumbers.add(0);
}

vials = new ArrayList<>();
thermometerVials = new ArrayList<>();
}

//setters and accessors for our array of vials
public boolean addVial(ThermometerCell headCell, ThermometerCell tipCell) {
if(verifyVial(headCell, tipCell, this)) {
vials.add(new Vial(headCell, tipCell, this));
return true;
}
return false;
public void addVial(ThermometerVial v) {
thermometerVials.add(v);
}
public ArrayList<Vial> getVials() {
return vials;
public ArrayList<ThermometerVial> getVials() {
return thermometerVials;
}


Expand Down
60 changes: 35 additions & 25 deletions src/main/java/edu/rpi/legup/puzzle/thermometer/ThermometerCell.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,51 @@
import java.awt.Point;

public class ThermometerCell extends GridCell<Integer> {
public ThermometerCell(int valueInt, Point location) {
super(valueInt, location);
private ThermometerType type;
private ThermometerFill fill;
public ThermometerCell(Point location) {
//since we do not use get/set data value int can be any value
super(0, location);
type = ThermometerType.UNKNOWN;
fill = ThermometerFill.UNKNOWN;
}




//Note: setdata does not work for our purposes
public void setType(ThermometerType t){
type = t;
}


public ThermometerType getType() {
switch (data) {
case 0:
return ThermometerType.UNKNOWN;
case 1:
return ThermometerType.HEAD;
case 2:
return ThermometerType.SHAFT;
case 3:
return ThermometerType.TIP;
}
return null;
return switch (type.ordinal()) {
case 0 -> ThermometerType.UNKNOWN;
case 1 -> ThermometerType.HEAD;
case 2 -> ThermometerType.SHAFT;
case 3 -> ThermometerType.TIP;
default -> null;
};
}

public void setFill(ThermometerFill f){
fill = f;
}

public ThermometerFill getFill() {
switch (data) {
case 0:
return ThermometerFill.UNKNOWN;
case 1:
return ThermometerFill.FILLED;
case 2:
return ThermometerFill.EMPTY;
case 3:
return ThermometerFill.BLOCKED;
}
return null;
return switch (fill.ordinal()) {
case 0 -> ThermometerFill.UNKNOWN;
case 1 -> ThermometerFill.FILLED;
case 2 -> ThermometerFill.EMPTY;
case 3 -> ThermometerFill.BLOCKED;
default -> null;
};
}

@Override
public ThermometerCell copy() {
ThermometerCell copy = new ThermometerCell(data, (Point) location.clone());
ThermometerCell copy = new ThermometerCell((Point) location.clone());
copy.setIndex(index);
copy.setModifiable(isModifiable);
copy.setGiven(isGiven);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,26 @@ public void changeCell(MouseEvent e, PuzzleElement data) {
this.boardView.getSelectionPopupMenu().show(boardView, this.boardView.getCanvas().getX() + e.getX(), this.boardView.getCanvas().getY() + e.getY());
}
else {
if (cell.getData() == ThermometerFill.EMPTY.ordinal()) {
data.setData(ThermometerFill.FILLED.ordinal());
if (cell.getFill() == ThermometerFill.EMPTY) {
cell.setFill(ThermometerFill.FILLED);
}
else if (cell.getData() == ThermometerFill.FILLED.ordinal()) {
data.setData(ThermometerFill.BLOCKED.ordinal());
else if (cell.getFill() == ThermometerFill.FILLED) {
cell.setFill(ThermometerFill.BLOCKED);
}
else {
data.setData(ThermometerFill.EMPTY.ordinal());
cell.setFill(ThermometerFill.EMPTY);
}
}
}
else if (e.getButton() == MouseEvent.BUTTON2) {
if (cell.getData() == ThermometerFill.EMPTY.ordinal()) {
data.setData(ThermometerFill.BLOCKED.ordinal());
if (cell.getFill() == ThermometerFill.EMPTY) {
cell.setFill(ThermometerFill.BLOCKED);
}
else if (cell.getData() == ThermometerFill.BLOCKED.ordinal()) {
data.setData(ThermometerFill.FILLED.ordinal());
else if (cell.getFill() == ThermometerFill.BLOCKED) {
cell.setFill(ThermometerFill.FILLED);
}
else {
data.setData(ThermometerFill.EMPTY.ordinal());
cell.setFill(ThermometerFill.EMPTY);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package edu.rpi.legup.puzzle.thermometer;

public class ThermometerExporter {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package edu.rpi.legup.puzzle.thermometer;

import edu.rpi.legup.model.PuzzleImporter;
import edu.rpi.legup.puzzle.thermometer.ThermometerVialFactory;
import edu.rpi.legup.save.InvalidFileFormatException;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import java.awt.*;

public class ThermometerImporter extends PuzzleImporter {
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) {

}

@Override
public void initializeBoard(Node node) throws InvalidFileFormatException {
try {
if (!node.getNodeName().equalsIgnoreCase("board")) {
throw new InvalidFileFormatException("thermometer Importer: cannot find board puzzleElement");
}
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("cells").item(0);
NodeList elementDataList = dataElement.getElementsByTagName("cell");

ThermometerBoard thermometerBoard = null;
if (!boardElement.getAttribute("size").isEmpty()) {
int size = Integer.parseInt(boardElement.getAttribute("size"));
thermometerBoard = new ThermometerBoard(size);
if (boardElement.getElementsByTagName("rowNumbers").getLength() != size) {
throw new InvalidFileFormatException("thermometer Importer: no rowNumbers found for board");
}
if (boardElement.getElementsByTagName("colNumbers").getLength() != size) {
throw new InvalidFileFormatException("thermometer Importer: no colNumbers found for board");
}
} else if (!boardElement.getAttribute("width").isEmpty() && !boardElement.getAttribute("height").isEmpty()) {
int width = Integer.parseInt(boardElement.getAttribute("width"));
int height = Integer.parseInt(boardElement.getAttribute("height"));
if (boardElement.getElementsByTagName("colNumbers").getLength() != width) {
throw new InvalidFileFormatException("thermometer Importer: no colNumbers found for board");
}
if (boardElement.getElementsByTagName("rowNumbers").getLength() != height) {
throw new InvalidFileFormatException("thermometer Importer: no rowNumbers found for board");
}
//TODO: potentially have to deal with size issues and non interactable cells
thermometerBoard = new ThermometerBoard(width, height);
}

if (thermometerBoard == null) {
throw new InvalidFileFormatException("thermometer Importer: invalid board dimensions");
}

int width = thermometerBoard.getWidth();
int height = thermometerBoard.getHeight();

for (int i = 0; i < elementDataList.getLength(); i++) {
ThermometerVialFactory.importThermometerVial(elementDataList.item(i), thermometerBoard);
}

//verifying all vials were used
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if (thermometerBoard.getCell(x, y) == null) {
throw new InvalidFileFormatException("thermometer Importer: invalid puzzle, unused tiles");
}
}
}

puzzle.setCurrentBoard(thermometerBoard);
} catch (NumberFormatException e) {
throw new InvalidFileFormatException("thermometer Importer: unknown value where integer expected");
}
}

@Override
public void initializeBoard(String[] statements) throws UnsupportedOperationException, IllegalArgumentException {

}
}
Original file line number Diff line number Diff line change
@@ -1,33 +1,21 @@
package edu.rpi.legup.puzzle.thermometer.elements;

import edu.rpi.legup.puzzle.thermometer.ThermometerCell;
import edu.rpi.legup.puzzle.thermometer.ThermometerBoard;
import edu.rpi.legup.puzzle.thermometer.ThermometerFill;
import edu.rpi.legup.puzzle.thermometer.ThermometerType;
import edu.rpi.legup.model.elements.PlaceableElement;
package edu.rpi.legup.puzzle.thermometer;

import java.awt.*;
import java.util.ArrayList;

import static java.lang.Math.*;

public class Vial {
public class ThermometerVial {
private ArrayList<ThermometerCell> cells;

public Vial(ThermometerCell headCell, ThermometerCell tipCell, ThermometerBoard board) {
public ThermometerVial(int headX, int headY, int tipX, int tipY, ThermometerBoard board) {
cells = new ArrayList<>();
fillData(headCell, tipCell, board);
fillData(headX, headY, tipX, tipY, board);
}

//function called by the constructor which adds in all of the cells to the array
//as well as updates their type on the board
private void fillData(ThermometerCell headCell, ThermometerCell tipCell, ThermometerBoard board) {
//shorthand for useful variables
int headX = (int) headCell.getLocation().getX();
int headY = (int) headCell.getLocation().getY();
int tipX = (int) tipCell.getLocation().getX();
int tipY = (int) tipCell.getLocation().getY();

private void fillData(int headX, int headY, int tipX, int tipY, ThermometerBoard board) {
//not totally happy with layout of code but most readable version I can think of atm
//top left coordinate is 0,0 cells are added from head to tip always
//because cells have already been verified by time constructor is called
Expand Down Expand Up @@ -65,9 +53,11 @@ else if (headX < tipX){

//helper function for adding a single cell
private void addCell(int x, int y, ThermometerType t, ThermometerBoard board){

//this very likely is not updating the data in the way we want it to
board.getCell(x, y).setData(t.ordinal());
ThermometerCell cell = new ThermometerCell(new Point(x, y));
cell.setIndex(y * board.getHeight() + x);
cell.setModifiable(true);
board.setCell(x, y, cell);
board.getCell(x, y).setType(t);
cells.add(board.getCell(x, y));
}

Expand Down Expand Up @@ -114,7 +104,7 @@ public static boolean verifyVial(ThermometerCell headCell, ThermometerCell tipCe

//verifying that every cell along path is currently unclaimed
for (int i = top; i < bottom; i++) {
if(board.getCell(headX, i).getType() != ThermometerType.UNKNOWN) return false;
if(board.getCell(headX, i) != null || board.getCell(headX, i).getType() != ThermometerType.UNKNOWN) return false;
}
}
else if (headY == tipY) {
Expand Down
Loading
Loading