Skip to content

Commit

Permalink
Merge pull request #208 from Corppet/dev
Browse files Browse the repository at this point in the history
Battleship: Adjacent Ships Contradiction Rule
  • Loading branch information
charlestian23 authored Jul 22, 2022
2 parents e467a88 + 057a662 commit f6487f5
Show file tree
Hide file tree
Showing 7 changed files with 206 additions and 41 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ mainClassName = 'edu.rpi.legup.Legup'
sourceCompatibility = 1.8

dependencies {
implementation 'org.jetbrains:annotations:20.1.0'
implementation 'org.jetbrains:annotations:20.1.0'
compile project(':legup-update')
compile 'com.google.firebase:firebase-admin:6.3.0'
compile 'org.apache.httpcomponents:httpclient:4.5.1'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,35 +35,43 @@ public void initializeBoard(int rows, int columns) {
public void initializeBoard(Node node) throws InvalidFileFormatException {
try {
if (!node.getNodeName().equalsIgnoreCase("board")) {
throw new InvalidFileFormatException("BattleShip Importer: cannot find board puzzleElement");
throw new InvalidFileFormatException("BattleShip Importer: " +
"cannot find board puzzleElement");
}
Element boardElement = (Element) node;
if (boardElement.getElementsByTagName("cells").getLength() == 0) {
throw new InvalidFileFormatException("BattleShip Importer: no puzzleElement found for board");
throw new InvalidFileFormatException("BattleShip Importer: " +
"no puzzleElement found for board");
}
Element dataElement = (Element) boardElement.getElementsByTagName("cells").item(0);
Element dataElement = (Element) boardElement.getElementsByTagName(
"cells").item(0);
NodeList elementDataList = dataElement.getElementsByTagName("cell");

BattleshipBoard battleShipBoard = null;
if (!boardElement.getAttribute("size").isEmpty()) {
int size = Integer.valueOf(boardElement.getAttribute("size"));
int size = Integer.valueOf(boardElement.getAttribute(
"size"));
battleShipBoard = new BattleshipBoard(size);
} else if (!boardElement.getAttribute("width").isEmpty() && !boardElement.getAttribute("height").isEmpty()) {
int width = Integer.valueOf(boardElement.getAttribute("width"));
int height = Integer.valueOf(boardElement.getAttribute("height"));
} else if (!boardElement.getAttribute("width").isEmpty()
&& !boardElement.getAttribute("height").isEmpty()) {
int width = Integer.valueOf(boardElement.getAttribute(
"width"));
int height = Integer.valueOf(boardElement.getAttribute(
"height"));
battleShipBoard = new BattleshipBoard(width, height);
}

if (battleShipBoard == null) {
throw new InvalidFileFormatException("BattleShip Importer: invalid board dimensions");
throw new InvalidFileFormatException("BattleShip Importer: " +
"invalid board dimensions");
}

int width = battleShipBoard.getWidth();
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() != BattleshipType.getType(0)) {
cell.setModifiable(false);
Expand All @@ -75,7 +83,8 @@ 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(BattleshipType.UNKNOWN, 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 All @@ -85,41 +94,59 @@ public void initializeBoard(Node node) throws InvalidFileFormatException {

NodeList axes = boardElement.getElementsByTagName("axis");
if (axes.getLength() != 2) {
throw new InvalidFileFormatException("BattleShip Importer: cannot find axes");
throw new InvalidFileFormatException("BattleShip Importer: " +
"cannot find axes");
}

Element axis1 = (Element) axes.item(0);
Element axis2 = (Element) axes.item(1);

if (!axis1.hasAttribute("side") || !axis1.hasAttribute("side")) {
throw new InvalidFileFormatException("BattleShip Importer: side attribute of axis not specified");
if (!axis1.hasAttribute("side") || !axis2.hasAttribute(
"side")) {
throw new InvalidFileFormatException("BattleShip Importer: " +
"side attribute of axis not specified");
}
String side1 = axis1.getAttribute("side");
String side2 = axis2.getAttribute("side");
if (side1.equalsIgnoreCase(side2) || !(side1.equalsIgnoreCase("east") || side1.equalsIgnoreCase("south")) ||
!(side2.equalsIgnoreCase("east") || side2.equalsIgnoreCase("south"))) {
throw new InvalidFileFormatException("BattleShip Importer: axes must be different and be {east | south}");
if (side1.equalsIgnoreCase(side2)
|| !(side1.equalsIgnoreCase("east")
|| side1.equalsIgnoreCase("south"))
|| !(side2.equalsIgnoreCase("east")
|| side2.equalsIgnoreCase("south"))) {
throw new InvalidFileFormatException("BattleShip Importer: " +
"axes must be different and be {east | south}");
}
NodeList eastClues = side1.equalsIgnoreCase("east") ? axis1.getElementsByTagName("clue") : axis2.getElementsByTagName("clue");
NodeList southClues = side1.equalsIgnoreCase("south") ? axis1.getElementsByTagName("clue") : axis2.getElementsByTagName("clue");

if (eastClues.getLength() != battleShipBoard.getHeight() || southClues.getLength() != battleShipBoard.getWidth()) {
throw new InvalidFileFormatException("BattleShip Importer: there must be same number of clues as the dimension of the board");
NodeList eastClues = side1.equalsIgnoreCase("east")
? axis1.getElementsByTagName("clue") :
axis2.getElementsByTagName("clue");
NodeList southClues = side1.equalsIgnoreCase("south")
? axis1.getElementsByTagName("clue") :
axis2.getElementsByTagName("clue");

if (eastClues.getLength() != battleShipBoard.getHeight()
|| southClues.getLength() != battleShipBoard.getWidth()) {
throw new InvalidFileFormatException("BattleShip Importer: " +
"there must be same number of clues as the dimension " +
"of the board");
}

for (int i = 0; i < eastClues.getLength(); i++) {
Element clue = (Element) eastClues.item(i);
int value = Integer.valueOf(clue.getAttribute("value"));
int index = BattleshipClue.colStringToColNum(clue.getAttribute("index"));
int index = BattleshipClue.colStringToColNum(
clue.getAttribute("index"));

if (index - 1 < 0 || index - 1 > battleShipBoard.getHeight()) {
throw new InvalidFileFormatException("BattleShip Importer: clue index out of bounds");
throw new InvalidFileFormatException("BattleShip Importer: " +
"clue index out of bounds");
}

if (battleShipBoard.getEast().get(index - 1) != null) {
throw new InvalidFileFormatException("BattleShip Importer: duplicate clue index");
throw new InvalidFileFormatException("BattleShip Importer: " +
"duplicate clue index");
}
battleShipBoard.getEast().set(index - 1, new BattleshipClue(value, index, BattleshipType.CLUE_EAST));
battleShipBoard.getEast().set(index - 1, new BattleshipClue(
value, index, BattleshipType.CLUE_EAST));
}

for (int i = 0; i < southClues.getLength(); i++) {
Expand All @@ -128,18 +155,22 @@ public void initializeBoard(Node node) throws InvalidFileFormatException {
int index = Integer.valueOf(clue.getAttribute("index"));

if (index - 1 < 0 || index - 1 > battleShipBoard.getWidth()) {
throw new InvalidFileFormatException("BattleShip Importer: clue index out of bounds");
throw new InvalidFileFormatException("BattleShip Importer: " +
"clue index out of bounds");
}

if (battleShipBoard.getSouth().get(index - 1) != null) {
throw new InvalidFileFormatException("BattleShip Importer: duplicate clue index");
throw new InvalidFileFormatException("BattleShip Importer: " +
"duplicate clue index");
}
battleShipBoard.getSouth().set(index - 1, new BattleshipClue(value, index, BattleshipType.CLUE_SOUTH));
battleShipBoard.getSouth().set(index - 1, new BattleshipClue(
value, index, BattleshipType.CLUE_SOUTH));
}

puzzle.setCurrentBoard(battleShipBoard);
} catch (NumberFormatException e) {
throw new InvalidFileFormatException("BattleShip Importer: unknown value where integer expected");
throw new InvalidFileFormatException("BattleShip Importer: " +
"unknown value where integer expected");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<edu.rpi.legup.Legup>
<edu.rpi.legup.puzzle name="BattleShip">
<Legup>
<puzzle name="Battleship">
<board size="10">
<cells>
<cell value="1" x="2" y="0"/>
Expand Down Expand Up @@ -33,5 +33,5 @@
<clue value="0" index="10"/>
</axis>
</board>
</edu.rpi.legup.puzzle>
</edu.rpi.legup.Legup>
</puzzle>
</Legup>
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,48 @@ public static void setUp()
@Test
public void OrthogonalAdjacentTest() throws InvalidFileFormatException
{
TestUtilities.importTestBoard("puzzles/battleship/rules/" +
"AdjacentShipsContradictionRule/OrthogonalAdjacentBoards",
TestUtilities.importTestBoard("puzzles/battleship/rules" +
"/AdjacentShipsContradictionRule/OrthogonalAdjacentBoards",
battleship);
TreeNode rootNode = battleship.getTree().getRootNode();
TreeTransition transition = rootNode.getChildren().get(0);
transition.setRule(RULE);

BattleshipBoard board = (BattleshipBoard)transition.getBoard();
BattleshipCell cell1 = board.getCell(1, 1);
BattleshipCell cell2 = board.getCell(1, 2);
BattleshipCell cell3 = board.getCell(2, 1);
BattleshipCell cell4 = board.getCell(2, 2);

Assert.assertNotNull(RULE.checkContradiction(
board));
}

@Test
public void InvalidOrthogonalAdjacentTest() throws InvalidFileFormatException
{
TestUtilities.importTestBoard("puzzles/battleship/rules" +
"/AdjacentShipsContradictionRule" +
"/InvalidOrthogonalAdjacentBoards", battleship);
TreeNode rootNode = battleship.getTree().getRootNode();
TreeTransition transition = rootNode.getChildren().get(0);
transition.setRule(RULE);

BattleshipBoard board = (BattleshipBoard)transition.getBoard();

Assert.assertNull(RULE.checkContradiction(
(BattleshipBoard)transition.getBoard()));
board));
}

@Test
public void DiagonalAdjacentTest() throws InvalidFileFormatException
{
TestUtilities.importTestBoard("puzzles/battleship/rules" +
"/AdjacentShipsContradictionRule" +
"/DiagonalAdjacentBoards", battleship);
TreeNode rootNode = battleship.getTree().getRootNode();
TreeTransition transition = rootNode.getChildren().get(0);
transition.setRule(RULE);

BattleshipBoard board = (BattleshipBoard)transition.getBoard();

Assert.assertNull(RULE.checkContradiction(
board));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<Legup>
<puzzle name="Battleship">
<board size="10">
<cells>
<cell value="3" x="3" y="3"/>
<cell value="3" x="4" y="4"/>
<cell value="3" x="2" y="3"/>
</cells>
<axis side="east">
<clue value="5" index="A"/>
<clue value="5" index="B"/>
<clue value="5" index="C"/>
<clue value="5" index="D"/>
<clue value="5" index="E"/>
<clue value="5" index="F"/>
<clue value="5" index="G"/>
<clue value="5" index="H"/>
<clue value="5" index="I"/>
<clue value="5" index="J"/>
</axis>
<axis side="south">
<clue value="5" index="1"/>
<clue value="5" index="2"/>
<clue value="5" index="3"/>
<clue value="5" index="4"/>
<clue value="5" index="5"/>
<clue value="5" index="6"/>
<clue value="5" index="7"/>
<clue value="5" index="8"/>
<clue value="5" index="9"/>
<clue value="5" index="10"/>
</axis>
</board>
</puzzle>
</Legup>
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<Legup>
<puzzle name="Battleship">
<board size="10">
<cells>
<cell value="3" x="2" y="3"/>
<cell value="3" x="2" y="2"/>
<cell value="3" x="2" y="1"/>
<cell value="3" x="1" y="2"/>
</cells>
<axis side="east">
<clue value="5" index="A"/>
<clue value="5" index="B"/>
<clue value="5" index="C"/>
<clue value="5" index="D"/>
<clue value="5" index="E"/>
<clue value="5" index="F"/>
<clue value="5" index="G"/>
<clue value="5" index="H"/>
<clue value="5" index="I"/>
<clue value="5" index="J"/>
</axis>
<axis side="south">
<clue value="5" index="1"/>
<clue value="5" index="2"/>
<clue value="5" index="3"/>
<clue value="5" index="4"/>
<clue value="5" index="5"/>
<clue value="5" index="6"/>
<clue value="5" index="7"/>
<clue value="5" index="8"/>
<clue value="5" index="9"/>
<clue value="5" index="10"/>
</axis>
</board>
</puzzle>
</Legup>
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<Legup>
<puzzle name="Battleship">
<board size="10">
<cells>
<cell value="3" x="2" y="3"/>
<cell value="3" x="2" y="2"/>
<cell value="3" x="2" y="1"/>
</cells>
<axis side="east">
<clue value="5" index="A"/>
<clue value="5" index="B"/>
<clue value="5" index="C"/>
<clue value="5" index="D"/>
<clue value="5" index="E"/>
<clue value="5" index="F"/>
<clue value="5" index="G"/>
<clue value="5" index="H"/>
<clue value="5" index="I"/>
<clue value="5" index="J"/>
</axis>
<axis side="south">
<clue value="5" index="1"/>
<clue value="5" index="2"/>
<clue value="5" index="3"/>
<clue value="5" index="4"/>
<clue value="5" index="5"/>
<clue value="5" index="6"/>
<clue value="5" index="7"/>
<clue value="5" index="8"/>
<clue value="5" index="9"/>
<clue value="5" index="10"/>
</axis>
</board>
</puzzle>
</Legup>

0 comments on commit f6487f5

Please sign in to comment.