Loading src/main/java/ch6/GameHelper.java +10 −16 Changes for src/main/java/ch6/GameHelper.java: 10 added lines, 16 removed lines. Original line number Diff line number Diff line Loading @@ -26,7 +26,6 @@ public class GameHelper { } public ArrayList<String> placeStartup(int startupSize) { ArrayList<String> alphaCells = new ArrayList<String>(); // holds index to grid (0 - 48) int[] startupCoords = new int[startupSize]; // current candidate co-ordinates int attempts = 0; // current attempts counter Loading @@ -41,7 +40,6 @@ public class GameHelper { success = horizontalStartupFits(location, startupSize); } else{ success = verticalStartupFits(location, startupSize); } if (success) { Loading @@ -50,8 +48,8 @@ public class GameHelper { } // end while System.out.println(this); ArrayList<String> alphaCells = new ArrayList<String>(); for (int index : startupCoords) { // turn location into 'a0'-style startupCoords grid[index] = 1; // mark master grid position as ‘used' String alphaCoords = getAlphaCoordsFromIndex(index); alphaCells.add(alphaCoords); } Loading @@ -65,11 +63,10 @@ public class GameHelper { int column = index % GRID_LENGTH; // get numeric column value String letter = ALPHABET.substring(column, column + 1); // convert column index to letter String alphaCoords = letter + row; return alphaCoords; return letter + row; } private boolean bruteForcePlace(int startupSize, int[] startupCoords, int increment, int location) { boolean bruteForcePlace(int startupSize, int[] startupCoords, int increment, int location) { System.out.println("startupSize = " + startupSize + ", startupCoords = " + Arrays.toString(startupCoords) + ", increment = " + increment + ", location = " + location); int k = 0; for (int j = location; k < startupSize; j += increment) { Loading @@ -84,25 +81,22 @@ public class GameHelper { return false; } } // this is awful, more than one iteration for (int index : startupCoords) { grid[index] = 1; // mark master grid position as ‘used' } return true; } boolean horizontalStartupFits(int location, int startupSize) { int finalLocation = location + (HORIZONTAL.getIncrement() * startupSize); if (calculateRowFromIndex(location) != calculateRowFromIndex(finalLocation)) { // end position is on a different row to start position System.out.println("location = " + location + ", startupSize = " + startupSize); return false; } return true; return calculateRowFromIndex(location) == calculateRowFromIndex(finalLocation); // check end is on same row as start } boolean verticalStartupFits(int location, int startupSize) { int finalLocation = location + (VERTICAL.getIncrement() * startupSize); if (finalLocation > GRID_SIZE) { // end position is off the bottom System.out.println("location = " + location + ", startupSize = " + startupSize); return false; } return true; return finalLocation < GRID_SIZE; // check end position doesn't go off the bottom of the grid } private Alignment calculateIncrement() { Loading src/test/java/ch6/GameHelperTest.java +62 −0 Changes for src/test/java/ch6/GameHelperTest.java: 62 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -66,6 +66,68 @@ class GameHelperTest { assertFalse(gameHelper.verticalStartupFits(47, 3)); } @Test void shouldBeAbleToPlaceHorizontalIntoEmptyGridStartingFromValidLocation() { // given GameHelper gameHelper = new GameHelper(); // when int[] startupCoords = new int[3]; boolean success = gameHelper.bruteForcePlace(3, startupCoords, 1, 0); // then assertTrue(success); assertArrayEquals(new int[]{0, 1, 2}, startupCoords); } @Test void shouldBeAbleToPlaceVerticalIntoEmptyGridStartingFromValidLocation() { // given GameHelper gameHelper = new GameHelper(); // when int[] startupCoords = new int[3]; boolean success = gameHelper.bruteForcePlace(3, startupCoords, 7, 0); // then assertTrue(success); assertArrayEquals(new int[]{0, 7, 14}, startupCoords); } @Test void shouldNotBeAbleToPlaceHorizontalIntoGridAtSameLocationAsExisting() { // given GameHelper gameHelper = new GameHelper(); boolean setupSuccess = gameHelper.bruteForcePlace(3, new int[3], 1, 8); // "B1" assertTrue(setupSuccess); System.out.println(gameHelper); // when int[] startupCoords = new int[3]; boolean success = gameHelper.bruteForcePlace(3, startupCoords, 1, 9); // "C1" - horizontal System.out.println(gameHelper); // then assertFalse(success); } @Test void shouldNotBeAbleToPlaceVerticalIntoGridAtSameLocationAsExisting() { // given GameHelper gameHelper = new GameHelper(); boolean setupSuccess = gameHelper.bruteForcePlace(3, new int[3], 1, 8); // "B1" assertTrue(setupSuccess); System.out.println(gameHelper); // when int[] startupCoords = new int[3]; boolean success = gameHelper.bruteForcePlace(3, startupCoords, 7, 1); // "A1" - vertical System.out.println(gameHelper); // then assertFalse(success); } @Test void shouldTurnIndexIntoAlphaCoords() { GameHelper gameHelper = new GameHelper(); Loading Loading
src/main/java/ch6/GameHelper.java +10 −16 Changes for src/main/java/ch6/GameHelper.java: 10 added lines, 16 removed lines. Original line number Diff line number Diff line Loading @@ -26,7 +26,6 @@ public class GameHelper { } public ArrayList<String> placeStartup(int startupSize) { ArrayList<String> alphaCells = new ArrayList<String>(); // holds index to grid (0 - 48) int[] startupCoords = new int[startupSize]; // current candidate co-ordinates int attempts = 0; // current attempts counter Loading @@ -41,7 +40,6 @@ public class GameHelper { success = horizontalStartupFits(location, startupSize); } else{ success = verticalStartupFits(location, startupSize); } if (success) { Loading @@ -50,8 +48,8 @@ public class GameHelper { } // end while System.out.println(this); ArrayList<String> alphaCells = new ArrayList<String>(); for (int index : startupCoords) { // turn location into 'a0'-style startupCoords grid[index] = 1; // mark master grid position as ‘used' String alphaCoords = getAlphaCoordsFromIndex(index); alphaCells.add(alphaCoords); } Loading @@ -65,11 +63,10 @@ public class GameHelper { int column = index % GRID_LENGTH; // get numeric column value String letter = ALPHABET.substring(column, column + 1); // convert column index to letter String alphaCoords = letter + row; return alphaCoords; return letter + row; } private boolean bruteForcePlace(int startupSize, int[] startupCoords, int increment, int location) { boolean bruteForcePlace(int startupSize, int[] startupCoords, int increment, int location) { System.out.println("startupSize = " + startupSize + ", startupCoords = " + Arrays.toString(startupCoords) + ", increment = " + increment + ", location = " + location); int k = 0; for (int j = location; k < startupSize; j += increment) { Loading @@ -84,25 +81,22 @@ public class GameHelper { return false; } } // this is awful, more than one iteration for (int index : startupCoords) { grid[index] = 1; // mark master grid position as ‘used' } return true; } boolean horizontalStartupFits(int location, int startupSize) { int finalLocation = location + (HORIZONTAL.getIncrement() * startupSize); if (calculateRowFromIndex(location) != calculateRowFromIndex(finalLocation)) { // end position is on a different row to start position System.out.println("location = " + location + ", startupSize = " + startupSize); return false; } return true; return calculateRowFromIndex(location) == calculateRowFromIndex(finalLocation); // check end is on same row as start } boolean verticalStartupFits(int location, int startupSize) { int finalLocation = location + (VERTICAL.getIncrement() * startupSize); if (finalLocation > GRID_SIZE) { // end position is off the bottom System.out.println("location = " + location + ", startupSize = " + startupSize); return false; } return true; return finalLocation < GRID_SIZE; // check end position doesn't go off the bottom of the grid } private Alignment calculateIncrement() { Loading
src/test/java/ch6/GameHelperTest.java +62 −0 Changes for src/test/java/ch6/GameHelperTest.java: 62 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -66,6 +66,68 @@ class GameHelperTest { assertFalse(gameHelper.verticalStartupFits(47, 3)); } @Test void shouldBeAbleToPlaceHorizontalIntoEmptyGridStartingFromValidLocation() { // given GameHelper gameHelper = new GameHelper(); // when int[] startupCoords = new int[3]; boolean success = gameHelper.bruteForcePlace(3, startupCoords, 1, 0); // then assertTrue(success); assertArrayEquals(new int[]{0, 1, 2}, startupCoords); } @Test void shouldBeAbleToPlaceVerticalIntoEmptyGridStartingFromValidLocation() { // given GameHelper gameHelper = new GameHelper(); // when int[] startupCoords = new int[3]; boolean success = gameHelper.bruteForcePlace(3, startupCoords, 7, 0); // then assertTrue(success); assertArrayEquals(new int[]{0, 7, 14}, startupCoords); } @Test void shouldNotBeAbleToPlaceHorizontalIntoGridAtSameLocationAsExisting() { // given GameHelper gameHelper = new GameHelper(); boolean setupSuccess = gameHelper.bruteForcePlace(3, new int[3], 1, 8); // "B1" assertTrue(setupSuccess); System.out.println(gameHelper); // when int[] startupCoords = new int[3]; boolean success = gameHelper.bruteForcePlace(3, startupCoords, 1, 9); // "C1" - horizontal System.out.println(gameHelper); // then assertFalse(success); } @Test void shouldNotBeAbleToPlaceVerticalIntoGridAtSameLocationAsExisting() { // given GameHelper gameHelper = new GameHelper(); boolean setupSuccess = gameHelper.bruteForcePlace(3, new int[3], 1, 8); // "B1" assertTrue(setupSuccess); System.out.println(gameHelper); // when int[] startupCoords = new int[3]; boolean success = gameHelper.bruteForcePlace(3, startupCoords, 7, 1); // "A1" - vertical System.out.println(gameHelper); // then assertFalse(success); } @Test void shouldTurnIndexIntoAlphaCoords() { GameHelper gameHelper = new GameHelper(); Loading