Loading src/main/java/ch6/GameHelper.java +10 −11 Changes for src/main/java/ch6/GameHelper.java: 10 added lines, 11 removed lines. Original line number Diff line number Diff line Loading @@ -32,23 +32,22 @@ public class GameHelper { boolean success = false; // flag = found a good location ? startupCount++; // nth Startup to place Alignment alignment = getAlignment(); Alignment alignment = getAlignment(); // alternates between vertical and horizontal while (!success & attempts++ < MAX_ATTEMPTS) { // main search loop (32) int location = random.nextInt(GRID_SIZE); // get random starting point boolean validLocation = isValidLocation(location, startupSize, alignment); boolean validLocation = isValidLocation(location, startupSize, alignment); // check the startup would fit on the grid if (validLocation) { for (int i = 0; i < startupCoords.length; i++) { if (validLocation) { // if it fits.... for (int i = 0; i < startupCoords.length; i++) { // create an array of proposed co-ordinates for the startup startupCoords[i] = location; location += alignment.getIncrement(); location += alignment.getIncrement(); // calculate the next location } System.out.println("trying: " + Arrays.toString(startupCoords)); success = allPositionsAvailable(startupCoords); } success = coordsAvailable(startupCoords); } // end loop } // end while System.out.println(this); savePositionToGrid(startupCoords); ArrayList<String> alphaCells = convertCoordsToAlphaFormat(startupCoords); Loading Loading @@ -76,8 +75,8 @@ public class GameHelper { } void savePositionToGrid(int[] startupCoords) { for (int index : startupCoords) { // turn location into 'a0'-style startupCoords grid[index] = 1; // mark master grid position as ‘used' for (int index : startupCoords) { grid[index] = 1; // mark master grid position as 'used' } } Loading @@ -89,7 +88,7 @@ public class GameHelper { return letter + row; } boolean allPositionsAvailable(int[] startupCoords) { boolean coordsAvailable(int[] startupCoords) { for (int coord : startupCoords) { // check all potential positions if (grid[coord] != 0) { // this co-ordinate already has an entry in the grid return false; // NO success Loading src/test/java/ch6/GameHelperTest.java +4 −4 Changes for src/test/java/ch6/GameHelperTest.java: 4 added lines, 4 removed lines. Original line number Diff line number Diff line Loading @@ -75,7 +75,7 @@ class GameHelperTest { // when int[] startupCoords = new int[]{0, 1, 2}; boolean success = gameHelper.allPositionsAvailable(startupCoords); boolean success = gameHelper.coordsAvailable(startupCoords); // then assertTrue(success); Loading @@ -88,7 +88,7 @@ class GameHelperTest { GameHelper gameHelper = new GameHelper(); // when boolean success = gameHelper.allPositionsAvailable(new int[]{0, 7, 14}); boolean success = gameHelper.coordsAvailable(new int[]{0, 7, 14}); // then assertTrue(success); Loading @@ -102,7 +102,7 @@ class GameHelperTest { System.out.println(gameHelper); // when boolean success = gameHelper.allPositionsAvailable(new int[]{9, 10, 11}); // "C1" - horizontal boolean success = gameHelper.coordsAvailable(new int[]{9, 10, 11}); // "C1" - horizontal System.out.println(gameHelper); // then Loading @@ -118,7 +118,7 @@ class GameHelperTest { // when int[] startupCoords = new int[]{1, 9, 16}; boolean success = gameHelper.allPositionsAvailable(startupCoords); // "A1" - vertical boolean success = gameHelper.coordsAvailable(startupCoords); // "A1" - vertical System.out.println(gameHelper); // then Loading Loading
src/main/java/ch6/GameHelper.java +10 −11 Changes for src/main/java/ch6/GameHelper.java: 10 added lines, 11 removed lines. Original line number Diff line number Diff line Loading @@ -32,23 +32,22 @@ public class GameHelper { boolean success = false; // flag = found a good location ? startupCount++; // nth Startup to place Alignment alignment = getAlignment(); Alignment alignment = getAlignment(); // alternates between vertical and horizontal while (!success & attempts++ < MAX_ATTEMPTS) { // main search loop (32) int location = random.nextInt(GRID_SIZE); // get random starting point boolean validLocation = isValidLocation(location, startupSize, alignment); boolean validLocation = isValidLocation(location, startupSize, alignment); // check the startup would fit on the grid if (validLocation) { for (int i = 0; i < startupCoords.length; i++) { if (validLocation) { // if it fits.... for (int i = 0; i < startupCoords.length; i++) { // create an array of proposed co-ordinates for the startup startupCoords[i] = location; location += alignment.getIncrement(); location += alignment.getIncrement(); // calculate the next location } System.out.println("trying: " + Arrays.toString(startupCoords)); success = allPositionsAvailable(startupCoords); } success = coordsAvailable(startupCoords); } // end loop } // end while System.out.println(this); savePositionToGrid(startupCoords); ArrayList<String> alphaCells = convertCoordsToAlphaFormat(startupCoords); Loading Loading @@ -76,8 +75,8 @@ public class GameHelper { } void savePositionToGrid(int[] startupCoords) { for (int index : startupCoords) { // turn location into 'a0'-style startupCoords grid[index] = 1; // mark master grid position as ‘used' for (int index : startupCoords) { grid[index] = 1; // mark master grid position as 'used' } } Loading @@ -89,7 +88,7 @@ public class GameHelper { return letter + row; } boolean allPositionsAvailable(int[] startupCoords) { boolean coordsAvailable(int[] startupCoords) { for (int coord : startupCoords) { // check all potential positions if (grid[coord] != 0) { // this co-ordinate already has an entry in the grid return false; // NO success Loading
src/test/java/ch6/GameHelperTest.java +4 −4 Changes for src/test/java/ch6/GameHelperTest.java: 4 added lines, 4 removed lines. Original line number Diff line number Diff line Loading @@ -75,7 +75,7 @@ class GameHelperTest { // when int[] startupCoords = new int[]{0, 1, 2}; boolean success = gameHelper.allPositionsAvailable(startupCoords); boolean success = gameHelper.coordsAvailable(startupCoords); // then assertTrue(success); Loading @@ -88,7 +88,7 @@ class GameHelperTest { GameHelper gameHelper = new GameHelper(); // when boolean success = gameHelper.allPositionsAvailable(new int[]{0, 7, 14}); boolean success = gameHelper.coordsAvailable(new int[]{0, 7, 14}); // then assertTrue(success); Loading @@ -102,7 +102,7 @@ class GameHelperTest { System.out.println(gameHelper); // when boolean success = gameHelper.allPositionsAvailable(new int[]{9, 10, 11}); // "C1" - horizontal boolean success = gameHelper.coordsAvailable(new int[]{9, 10, 11}); // "C1" - horizontal System.out.println(gameHelper); // then Loading @@ -118,7 +118,7 @@ class GameHelperTest { // when int[] startupCoords = new int[]{1, 9, 16}; boolean success = gameHelper.allPositionsAvailable(startupCoords); // "A1" - vertical boolean success = gameHelper.coordsAvailable(startupCoords); // "A1" - vertical System.out.println(gameHelper); // then Loading