Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision
  • 1-fix-assignment-errors
  • main
  • testsfail
3 results

Target

Select target project
  • cmoore44/assessment2
1 result
Select Git revision
  • 1-fix-assignment-errors
  • main
  • testsfail
3 results
Show changes

Commits on Source 2

File added
......@@ -5,6 +5,8 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.function.Consumer;
import javax.management.RuntimeErrorException;
/**
* The state of a dots and boxes grid.
*
......@@ -111,7 +113,10 @@ public class DotsAndBoxesGrid {
}
// A box is complete if the north and south horizontals and the east and west verticals have all been drawn.
// FIXME: You'll need to fix this code (after writing a test first).
if (!getHorizontal(x,y) || !getVertical(x, y) ||
!getHorizontal(x, y + 1) || !getVertical(x + 1, y)) {
return false;
}
return true;
}
......@@ -139,7 +144,9 @@ 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 RuntimeException(String.format("This horizontal line has already been drawn"));
}
this.horizontals[x][y] = true;
......@@ -170,7 +177,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 RuntimeException(String.format("This vertical line has already been drawn"));
}
this.verticals[x][y] = true;
// Try to claim the north or south boxes
......
......@@ -27,5 +27,67 @@ public class DotsAndBoxesGridTest {
assertTrue(true);
}
// FIXME: You need to write tests for the two known bugs in the code.
@Test
public void testBoxCompleteFalse() {
//Creating a box that should be incorrect and checking if it returns false
DotsAndBoxesGrid boxCompleteFalse = new DotsAndBoxesGrid(5, 5, 2);
boxCompleteFalse.drawHorizontal(0, 0, 0);
boxCompleteFalse.drawVertical(0, 0, 1);
boxCompleteFalse.drawHorizontal(0, 1, 0);
boxCompleteFalse.drawVertical(1, 1, 1);
assertFalse(boxCompleteFalse.boxComplete(0, 0));
}
@Test
public void testBoxCompleteTrue() {
//Creating a box that should be correct and checking if it returns true
DotsAndBoxesGrid boxCompleteTrue = new DotsAndBoxesGrid(5, 5, 2);
boxCompleteTrue.drawHorizontal(0, 0, 0);
boxCompleteTrue.drawVertical(0, 0, 1);
boxCompleteTrue.drawHorizontal(0, 1, 0);
boxCompleteTrue.drawVertical(1, 0, 1);
assertTrue(boxCompleteTrue.boxComplete(0, 0));
}
@Test
public void testDrawHorizontalDrawn() {
//Draws a horizontal line, then draws another horizontal line in the same
//place to check if an exception is appropriately thrown
DotsAndBoxesGrid horizontalDrawn = new DotsAndBoxesGrid(5, 5, 1);
horizontalDrawn.drawHorizontal(0, 0, 1);
assertThrows(RuntimeException.class, () -> {
horizontalDrawn.drawHorizontal(0, 0, 1);
});
}
@Test
public void testDrawHorizontalNotDrawn() {
//Draws a horizontal line to check if no exception is thrown
DotsAndBoxesGrid horizontalNotDrawn = new DotsAndBoxesGrid(5, 5, 1);
assertDoesNotThrow(() -> {
horizontalNotDrawn.drawHorizontal(0, 0, 1);
});
}
@Test
public void testDrawVerticalDrawn() {
//Draws a vertical line, then draws another vertical line in the same
//place to check if an exception is appropriately thrown
DotsAndBoxesGrid verticalDrawn = new DotsAndBoxesGrid(5, 5, 1);
verticalDrawn.drawVertical(0, 0, 1);
assertThrows(RuntimeException.class, () -> {
verticalDrawn.drawVertical(0, 0, 1);
});
}
@Test
public void testDrawVerticalNotDrawn() {
//Draws a vertical line to check if no exception is thrown
DotsAndBoxesGrid verticalNotDrawn = new DotsAndBoxesGrid(5, 5, 1);
assertDoesNotThrow(() -> {
verticalNotDrawn.drawVertical(0, 0, 1);
});
}
}