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
  • dho3/cosc220_2022_t2_assessment_two
1 result
Select Git revision
Loading items
Show changes
Commits on Source (2)
  • Daniel Ho's avatar
    Add tests for assignment errors · e72875bd
    Daniel Ho authored
    Add tests for correct detection of complete and incomplete
    boxes, and to ensure IllegalStateException is thrown when attempting
    to draw a horizontal or vertical line over lines that were already drawn.
    e72875bd
  • Daniel Ho's avatar
    Fix assignment errors · 61900cd5
    Daniel Ho authored
    Fix #1.
    Fix correct detection of complete and incomplete boxes on grid.
    Fix throwing of IllegalStateException when attempting to draw
    horizontal or vertical lines over lines that were previously drawn.
    61900cd5
......@@ -111,8 +111,7 @@ 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).
return true;
return (getHorizontal(x, y) && getHorizontal(x , y + 1) && getVertical(x, y) && getVertical(x+1, y));
}
/** Tries to claim a box for a player. If the box is complete, sets the ownership and returns true. */
......@@ -139,7 +138,10 @@ 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.
// throw exception if attempting to draw over a line that was previously drawn
if (getHorizontal(x, y)) {
throw new IllegalStateException("horizontal line already drawn");
}
this.horizontals[x][y] = true;
......@@ -170,7 +172,10 @@ 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.
// throw exception if attempting to draw over a line that was previously drawn
if (getVertical(x, y)) {
throw new IllegalStateException("vertical line already drawn");
}
this.verticals[x][y] = true;
// Try to claim the north or south boxes
......
......@@ -27,5 +27,150 @@ public class DotsAndBoxesGridTest {
assertTrue(true);
}
// FIXME: You need to write tests for the two known bugs in the code.
/**
* Tests correct detection of completed boxes
*/
@Test
public void boxCompleteDetectsCompletedBoxes() {
// test top left corner box of grid
DotsAndBoxesGrid case1 = new DotsAndBoxesGrid(5, 5, 2);
case1.drawHorizontal(0, 0, 0);
case1.drawVertical(0, 0, 1);
case1.drawHorizontal(0, 1, 0);
case1.drawVertical(1, 0, 1);
assertTrue(case1.boxComplete(0,0));
// test bottom right corner box of grid
DotsAndBoxesGrid case2 = new DotsAndBoxesGrid(5, 5, 2);
case2.drawHorizontal(3, 3, 0);
case2.drawVertical(3, 3, 1);
case2.drawHorizontal(3, 4, 0);
case2.drawVertical(4, 3, 1);
assertTrue(case2.boxComplete(3,3));
// test grid with 1 box
DotsAndBoxesGrid case3 = new DotsAndBoxesGrid(2, 2, 2);
case3.drawHorizontal(0, 0, 0);
case3.drawVertical(0, 0, 1);
case3.drawHorizontal(0, 1, 0);
case3.drawVertical(1, 0, 1);
assertTrue(case3.boxComplete(0,0));
// test a box around the center of grid
DotsAndBoxesGrid case4 = new DotsAndBoxesGrid(5, 5, 2);
case4.drawHorizontal(2, 2, 0);
case4.drawVertical(2, 2, 1);
case4.drawHorizontal(2, 3, 0);
case4.drawVertical(3, 2, 1);
assertTrue(case4.boxComplete(2,2));
}
/**
* Tests correct detection of incomplete boxes
*/
@Test
public void boxCompleteDetectsIncompleteBoxes() {
// test top left corner box of grid missing top horizontal line
DotsAndBoxesGrid case1 = new DotsAndBoxesGrid(5, 5, 2);
case1.drawVertical(0, 0, 1);
case1.drawHorizontal(0, 1, 0);
case1.drawVertical(1, 0, 1);
assertFalse(case1.boxComplete(0,0));
// test top left corner box of grid missing bottom horizontal line
DotsAndBoxesGrid case2 = new DotsAndBoxesGrid(5, 5, 2);
case2.drawHorizontal(0, 0, 0);
case2.drawVertical(0, 0, 1);
case2.drawVertical(1, 0, 1);
assertFalse(case2.boxComplete(0,0));
// test top left corner box of grid missing left vertical line
DotsAndBoxesGrid case3 = new DotsAndBoxesGrid(5, 5, 2);
case3.drawHorizontal(0, 0, 0);
case3.drawHorizontal(0, 1, 0);
case3.drawVertical(1, 0, 1);
assertFalse(case3.boxComplete(0,0));
// test top left corner box of grid missing right vertical line
DotsAndBoxesGrid case4 = new DotsAndBoxesGrid(5, 5, 2);
case4.drawHorizontal(0, 0, 0);
case4.drawVertical(0, 0, 1);
case4.drawHorizontal(0, 1, 0);
assertFalse(case4.boxComplete(0,0));
// test bottom right corner box of grid missing top horizontal line
DotsAndBoxesGrid case5 = new DotsAndBoxesGrid(5, 5, 2);
case5.drawVertical(3, 3, 1);
case5.drawHorizontal(3, 4, 0);
case5.drawVertical(4, 3, 1);
assertFalse(case5.boxComplete(3,3));
// test grid with 1 box missing top horizontal line
DotsAndBoxesGrid case6 = new DotsAndBoxesGrid(2, 2, 2);
case6.drawVertical(0, 0, 1);
case6.drawHorizontal(0, 1, 0);
case6.drawVertical(1, 0, 1);
assertFalse(case6.boxComplete(0,0));
// test a box around the center of grid missing top horizontal line
DotsAndBoxesGrid case7 = new DotsAndBoxesGrid(5, 5, 2);
case7.drawVertical(2, 2, 1);
case7.drawHorizontal(2, 3, 0);
case7.drawVertical(3, 2, 1);
assertFalse(case7.boxComplete(2,2));
}
/**
* Tests that IllegalStateException is thrown when drawHorizontal() or drawVertical() is called
* on a line that was already drawn.
*/
@Test
public void drawMethodsDetectRedrawnLines() {
DotsAndBoxesGrid case1 = new DotsAndBoxesGrid(5, 5, 2);
// tests drawing on previously-drawn horizontal lines
case1.drawHorizontal(0, 0, 0);
assertThrows(IllegalStateException.class, () -> {
case1.drawHorizontal(0,0,1);
});
case1.drawHorizontal(2, 2, 0);
assertThrows(IllegalStateException.class, () -> {
case1.drawHorizontal(2,2,1);
});
case1.drawHorizontal(3, 4, 0);
assertThrows(IllegalStateException.class, () -> {
case1.drawHorizontal(3,4,1);
});
// tests drawing on previously-drawn vertical lines
case1.drawVertical(0, 0, 0);
assertThrows(IllegalStateException.class, () -> {
case1.drawVertical(0,0,1);
});
case1.drawVertical(2, 2, 0);
assertThrows(IllegalStateException.class, () -> {
case1.drawVertical(2,2,1);
});
case1.drawVertical(4, 3, 0);
assertThrows(IllegalStateException.class, () -> {
case1.drawVertical(4,3,1);
});
}
}