Skip to content

Commit

Permalink
Merge pull request #142 from Corppet/dev
Browse files Browse the repository at this point in the history
Battleship cells use their own enums as the casted type.
  • Loading branch information
charlestian23 authored Jun 17, 2022
2 parents e60e195 + 0a22b0d commit f5e2e44
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 34 deletions.
28 changes: 26 additions & 2 deletions src/main/java/edu/rpi/legup/puzzle/battleship/BattleShip.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import edu.rpi.legup.model.Puzzle;
import edu.rpi.legup.model.gameboard.Board;
import edu.rpi.legup.model.gameboard.PuzzleElement;
import edu.rpi.legup.model.rules.ContradictionRule;

public class BattleShip extends Puzzle {
public BattleShip() {
Expand All @@ -15,9 +17,13 @@ public BattleShip() {
this.factory = new BattleShipCellFactory();
}

/**
* Initializes the game board. Called by the invoker of the class
*/
@Override
public void initializeView() {
boardView = new BattleShipView((BattleShipBoard) currentBoard);
addBoardListener(boardView);
}

@Override
Expand All @@ -33,9 +39,27 @@ public Board generatePuzzle(int difficulty) {
*/
@Override
public boolean isBoardComplete(Board board) {
return false;
BattleShipBoard battleShipBoard = (BattleShipBoard) board;

for (ContradictionRule rule : contradictionRules) {
if (rule.checkContradiction(battleShipBoard) == null) {
return false;
}
}
for (PuzzleElement data : battleShipBoard.getPuzzleElements()) {
BattleShipCell cell = (BattleShipCell) data;
if (cell.getType() == BattleShipType.UNKNOWN) {
return false;
}
}
return true;
}

/**
* Callback for when the board puzzleElement changes
*
* @param board the board that has changed
*/
@Override
public void onBoardChange(Board board) {

Expand Down Expand Up @@ -98,7 +122,7 @@ public void importPuzzle(String fileName) {
// int y = Integer.valueOf(cells.item(i).getAttributes().getNamedItem("y").getNodeValue());
// String value = cells.item(i).getAttributes().getNamedItem("value").getNodeValue().toUpperCase();
//
// BattleShipCell cell = new BattleShipCell(BattleShipCellType.valueOf(value).ordinal(), new Point(x, y));
// BattleShipCell cell = new BattleShipCell(BattleShipType.valueOf(value).ordinal(), new Point(x, y));
// battleShipBoard.setCell(x, y, cell);
// cell.setModifiable(false);
// cell.setGiven(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@

import java.awt.*;

public class BattleShipCell extends GridCell<Integer> {
public class BattleShipCell extends GridCell<BattleShipType> {

/**
* BattleShipCell Constructor - creates a BattleShipCell from the specified value and location
*
* @param value value of the BattleShipCell
* @param location position of the BattleShipCell
*/
public BattleShipCell(int value, Point location) {
public BattleShipCell(BattleShipType value, Point location) {
super(value, location);
}

Expand All @@ -21,8 +21,8 @@ public BattleShipCell(int value, Point location) {
*
* @return type of BattleShipCell
*/
public BattleShipCellType getType() {
return BattleShipCellType.getType(getData());
public BattleShipType getType() {
return data;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ public void changeCell(MouseEvent e, PuzzleElement data) {
if (e.isControlDown()) {
this.boardView.getSelectionPopupMenu().show(boardView, this.boardView.getCanvas().getX() + e.getX(), this.boardView.getCanvas().getY() + e.getY());
} else {
if (cell.getData() == BattleShipCellType.SHIP_SEGMENT_MIDDLE.value) {
cell.setData(BattleShipCellType.UNKNOWN.value);
if (cell.getData() == BattleShipType.SHIP_SEGMENT_MIDDLE) {
cell.setData(BattleShipType.UNKNOWN);
} else {
cell.setData(cell.getData() + 1);
cell.setData(BattleShipType.getType(cell.getData().value + 1));
}
}
} else if (e.getButton() == MouseEvent.BUTTON3) {
if (cell.getData() == BattleShipCellType.UNKNOWN.value) {
cell.setData(BattleShipCellType.SHIP_SEGMENT_MIDDLE.value);
if (cell.getData() == BattleShipType.UNKNOWN) {
cell.setData(BattleShipType.SHIP_SEGMENT_MIDDLE);
} else {
cell.setData(cell.getData() - 1);
cell.setData(BattleShipType.getType(cell.getData().value - 1));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,25 @@ public class BattleShipCellFactory extends ElementFactory {
* @throws InvalidFileFormatException
*/
@Override
public PuzzleElement importCell(Node node, Board board) throws InvalidFileFormatException {
public PuzzleElement<BattleShipType> importCell(Node node, Board board) throws InvalidFileFormatException {
try {
BattleShipBoard battleShipBoard = (BattleShipBoard) board;
int width = battleShipBoard.getWidth();
int height = battleShipBoard.getHeight();
NamedNodeMap attributeList = node.getAttributes();
if (node.getNodeName().equalsIgnoreCase("cell")) {

int value = Integer.valueOf(attributeList.getNamedItem("value").getNodeValue());
int x = Integer.valueOf(attributeList.getNamedItem("x").getNodeValue());
int y = Integer.valueOf(attributeList.getNamedItem("y").getNodeValue());
int value = Integer.parseInt(attributeList.getNamedItem("value").getNodeValue());
int x = Integer.parseInt(attributeList.getNamedItem("x").getNodeValue());
int y = Integer.parseInt(attributeList.getNamedItem("y").getNodeValue());
if (x >= width || y >= height) {
throw new InvalidFileFormatException("BattleShip Factory: cell location out of bounds");
}
if (value < 0 || value > 3) {
throw new InvalidFileFormatException("BattleShip Factory: cell unknown value");
}

BattleShipCell cell = new BattleShipCell(value, new Point(x, y));
BattleShipCell cell = new BattleShipCell(BattleShipType.getType(value), new Point(x, y));
cell.setIndex(y * height + x);
return cell;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

public class BattleShipClue extends PuzzleElement {

private BattleShipCellType type;
private BattleShipType type;

public BattleShipClue(int value, int index, BattleShipCellType type) {
public BattleShipClue(int value, int index, BattleShipType type) {
super(value);
this.index = index;
this.type = type;
Expand Down Expand Up @@ -42,11 +42,11 @@ public Integer getData() {
return (Integer) super.getData();
}

public BattleShipCellType getType() {
public BattleShipType getType() {
return type;
}

public void setType(BattleShipCellType type) {
public void setType(BattleShipType type) {
this.type = type;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public BattleShipElementView(BattleShipCell cell) {
@Override
public void drawElement(Graphics2D graphics2D) {
BattleShipCell cell = (BattleShipCell) puzzleElement;
BattleShipCellType type = cell.getType();
BattleShipType type = cell.getType();

switch (type) {
case UNKNOWN:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,10 @@ public void initializeBoard(Node node) throws InvalidFileFormatException {
int height = battleShipBoard.getHeight();

for (int i = 0; i < elementDataList.getLength(); i++) {
BattleShipCell cell = (BattleShipCell) puzzle.getFactory().importCell(elementDataList.item(i), battleShipBoard);
BattleShipCell cell = (BattleShipCell) puzzle.getFactory().importCell(
elementDataList.item(i), battleShipBoard);
Point loc = cell.getLocation();
if (cell.getData() != 0) {
if (cell.getData() != BattleShipType.getType(0)) {
cell.setModifiable(false);
cell.setGiven(true);
}
Expand All @@ -62,7 +63,7 @@ public void initializeBoard(Node node) throws InvalidFileFormatException {
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if (battleShipBoard.getCell(x, y) == null) {
BattleShipCell cell = new BattleShipCell(BattleShipCellType.UNKNOWN.value, new Point(x, y));
BattleShipCell cell = new BattleShipCell(BattleShipType.UNKNOWN, new Point(x, y));
cell.setIndex(y * height + x);
cell.setModifiable(true);
battleShipBoard.setCell(x, y, cell);
Expand Down Expand Up @@ -106,7 +107,7 @@ public void initializeBoard(Node node) throws InvalidFileFormatException {
if (battleShipBoard.getEast().get(index - 1) != null) {
throw new InvalidFileFormatException("BattleShip Importer: duplicate clue index");
}
battleShipBoard.getEast().set(index - 1, new BattleShipClue(value, index, BattleShipCellType.CLUE_EAST));
battleShipBoard.getEast().set(index - 1, new BattleShipClue(value, index, BattleShipType.CLUE_EAST));
}

for (int i = 0; i < southClues.getLength(); i++) {
Expand All @@ -121,7 +122,7 @@ public void initializeBoard(Node node) throws InvalidFileFormatException {
if (battleShipBoard.getSouth().get(index - 1) != null) {
throw new InvalidFileFormatException("BattleShip Importer: duplicate clue index");
}
battleShipBoard.getSouth().set(index - 1, new BattleShipClue(value, index, BattleShipCellType.CLUE_SOUTH));
battleShipBoard.getSouth().set(index - 1, new BattleShipClue(value, index, BattleShipType.CLUE_SOUTH));
}

puzzle.setCurrentBoard(battleShipBoard);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
package edu.rpi.legup.puzzle.battleship;

public enum BattleShipCellType {
public enum BattleShipType {
UNKNOWN, WATER, SHIP_SEGMENT_UNKNOWN, SHIP_SIZE_1, SHIP_SEGMENT_TOP,
SHIP_SEGMENT_RIGHT, SHIP_SEGMENT_BOTTOM, SHIP_SEGMENT_LEFT,
SHIP_SEGMENT_MIDDLE, CLUE_NORTH, CLUE_EAST, CLUE_SOUTH, CLUE_WEST;

public int value;

BattleShipCellType() {
BattleShipType() {
this.value = this.ordinal();
}

public static BattleShipCellType getType(int value) {
BattleShipCellType[] vals = values();
/**
* Gets the enum of this BattleShipType
*
* @return enum equivalent BattleShipType of integer value
*/
public static BattleShipType getType(int value) {
BattleShipType[] vals = values();
if (value >= 0 && value < vals.length) {
return vals[value];
}
Expand Down
20 changes: 18 additions & 2 deletions src/main/java/edu/rpi/legup/puzzle/nurikabe/NurikabeCell.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,21 @@

public class NurikabeCell extends GridCell<Integer> {

public NurikabeCell(int valueInt, Point location) {
super(valueInt, location);
/**
* NurikabeCell Constructor - creates a NurikabeCell from the specified value and location
*
* @param value value of the NurikabeCell
* @param location position of the NurikabeCell
*/
public NurikabeCell(int value, Point location) {
super(value, location);
}

/**
* Gets the type of this NurikabeCell
*
* @return type of NurikabeCell
*/
public NurikabeType getType() {
switch (data) {
case -2:
Expand All @@ -26,6 +37,11 @@ public NurikabeType getType() {
return null;
}

/**
* Performs a deep copy on the NurikabeCell
*
* @return a new copy of the NurikabeCell that is independent of this one
*/
@Override
public NurikabeCell copy() {
NurikabeCell copy = new NurikabeCell(data, (Point) location.clone());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
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);
UNKNOWN(0), TREE(1), GRASS(2), TENT(3),
CLUE_NORTH(-1), CLUE_EAST(-2), CLUE_SOUTH(-3), CLUE_WEST(-4);

public int value;

Expand Down

0 comments on commit f5e2e44

Please sign in to comment.