Skip to content
Snippets Groups Projects
Commit 25be4ec4 authored by ssmit235's avatar ssmit235
Browse files

Throw exception when line already exists.

parent be1aea3d
No related branches found
No related tags found
No related merge requests found
......@@ -132,7 +132,7 @@ public class DotsAndBoxesGrid {
* @param y
* @return true if it completes a box
*/
public boolean drawHorizontal(int x, int y, int player) {
public boolean drawHorizontal(int x, int y, int player) throws LineAlreadyDrawnException {
if (x >= width - 1 || x < 0) {
throw new IndexOutOfBoundsException(String.format("x was %d, which is out of range. Range is 0 to %d", x, width - 1));
}
......@@ -140,7 +140,11 @@ public class DotsAndBoxesGrid {
throw new IndexOutOfBoundsException(String.format("y was %d, which is out of range. Range is 0 to %d", y, height));
}
// FIXME: You need to throw an exception if the line was already drawn.
if (getHorizontal(x, y)) {
throw new LineAlreadyDrawnException();
}
this.horizontals[x][y] = true;
......@@ -163,7 +167,7 @@ public class DotsAndBoxesGrid {
* @param y
* @return true if it completes a box
*/
public boolean drawVertical(int x, int y, int player) {
public boolean drawVertical(int x, int y, int player) throws LineAlreadyDrawnException {
if (x >= width || x < 0) {
throw new IndexOutOfBoundsException(String.format("x was %d, which is out of range. Range is 0 to %d", x, width));
}
......@@ -171,7 +175,9 @@ public class DotsAndBoxesGrid {
throw new IndexOutOfBoundsException(String.format("y was %d, which is out of range. Range is 0 to %d", y, height - 1));
}
// You need to throw an exception if the line was already drawn.
if (getVertical(x, y)) {
throw new LineAlreadyDrawnException();
}
this.verticals[x][y] = true;
// Try to claim the north or south boxes
......
......@@ -77,7 +77,7 @@ public class DotsAndBoxesUI {
line.setOnMouseClicked((evt) -> {try {
grid.drawHorizontal(x, y, grid.getPlayer());
} catch (IllegalStateException ex) {
} catch (IllegalStateException | LineAlreadyDrawnException ex) {
// do nothing
// This is a little artificial, as normally we'd implement this with a check that the line isn't
// already "drawn" and then not calling the function. But for the exercise, we wanted students
......@@ -110,7 +110,7 @@ public class DotsAndBoxesUI {
line.setOnMouseClicked((evt) -> {try {
grid.drawVertical(x, y, grid.getPlayer());
} catch (IllegalStateException ex) {
} catch (IllegalStateException | LineAlreadyDrawnException ex) {
// do nothing
}});
......
......@@ -25,14 +25,14 @@ public class DotsAndBoxesGridTest {
// FIXME: You need to write tests for the two known bugs in the code.
@Test
public void testExisitingHorizontalLineThrowsException() {
public void testExisitingHorizontalLineThrowsException() throws LineAlreadyDrawnException {
DotsAndBoxesGrid dbg = new DotsAndBoxesGrid(4, 3, 2);
dbg.drawHorizontal(0, 0, 0);
assertThrows(LineAlreadyDrawnException.class, () -> {dbg.drawHorizontal(0, 0, 0);});
}
@Test
public void testExisitingVerticalLineThrowsException() {
public void testExisitingVerticalLineThrowsException() throws LineAlreadyDrawnException {
DotsAndBoxesGrid dbg = new DotsAndBoxesGrid(4, 3, 2);
dbg.drawVertical(0, 0, 0);
assertThrows(LineAlreadyDrawnException.class, () -> {dbg.drawVertical(0, 0, 0);});
......@@ -48,7 +48,7 @@ public class DotsAndBoxesGridTest {
@Test
public void testBoxComplete() {
public void testBoxComplete() throws LineAlreadyDrawnException {
DotsAndBoxesGrid dbg = new DotsAndBoxesGrid(4, 3, 2);
assertEquals(dbg.boxComplete(0,0),false);
dbg.drawHorizontal(0,0,0);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment