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

Target

Select target project
  • nranabha/dotsandboxes
1 result
Select Git revision
Show changes
Commits on Source (2)
......@@ -39,7 +39,7 @@ task fatJar(type: Jar) {
manifest {
attributes 'Main-Class': 'dotsandboxes.Main'
}
baseName = 'executable-jar'
archiveBaseName = 'executable-jar'
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
from { configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) } }
with jar
......
......@@ -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 horizontals[x][y] && horizontals[x][y + 1] && verticals[x][y] && verticals[x + 1][y];
}
/** Tries to claim a box for a player. If the box is complete, sets the ownership and returns true. */
......@@ -139,8 +138,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 (this.horizontals[x][y]){
throw new IllegalStateException("Horizontal line is already drawn here");
}
this.horizontals[x][y] = true;
// Try to claim the north or south boxes
......@@ -170,7 +170,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 (this.verticals[x][y]){
throw new IllegalStateException("Vertical line is already drawn here");
}
this.verticals[x][y] = true;
// Try to claim the north or south boxes
......
......@@ -4,6 +4,8 @@ import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assumptions.*;
import java.beans.Transient;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
......@@ -15,17 +17,47 @@ public class DotsAndBoxesGridTest {
private static final Logger logger = LogManager.getLogger(DotsAndBoxesGridTest.class);
/*
* Tests are functions that have an @Test annotation before them.
* The typical format of a test is that it contains some code that does something, and then one
* or more assertions to check that a condition holds.
*
* This is a dummy test just to show that the test suite itself runs
* Code to test that boxComplete returns true for boxes that are complete
*/
@Test
public void testTestSuiteRuns() {
logger.info("Dummy test to show the test suite runs");
assertTrue(true);
public void boxCompleteDetectsCompletedBoxes(){
DotsAndBoxesGrid tGrid = new DotsAndBoxesGrid(5, 5, 2);
tGrid.drawHorizontal(0, 0, 0);
tGrid.drawHorizontal(0, 1, 0);
tGrid.drawVertical(0, 0, 0);
tGrid.drawVertical(1, 0, 0);
assertTrue(tGrid.boxComplete(0, 0));
}
// FIXME: You need to write tests for the two known bugs in the code.
/*
* Code to test that boxComplete returns false for boxes that are not complete
*/
@Test
public void boxCompleteDetectsIncompleteBoxes(){
DotsAndBoxesGrid tBoxesGrid = new DotsAndBoxesGrid(5,5 , 2);
assertFalse(tBoxesGrid.boxComplete(0, 0));
tBoxesGrid.drawHorizontal(0, 0, 0);
assertFalse(tBoxesGrid.boxComplete(0, 0));
tBoxesGrid.drawHorizontal(0, 1, 0);
assertFalse(tBoxesGrid.boxComplete(0, 0));
tBoxesGrid.drawVertical(0, 0, 0);
assertFalse(tBoxesGrid.boxComplete(0, 0));
}
/*
* Code to test if a line is drawn on top of an existing line is detected
*/
@Test
public void drawMethodsDetectRedrawnLines(){
DotsAndBoxesGrid tGrid = new DotsAndBoxesGrid(5, 5, 2);
tGrid.drawHorizontal(0, 0, 0);
assertThrows(IllegalStateException.class, () -> {
tGrid.drawHorizontal(0, 0, 0);
});
tGrid.drawVertical(0, 0, 0);
assertThrows(IllegalStateException.class, () -> {
tGrid.drawVertical(0, 0, 0);
});
}
}