Commit 15f13f1a authored by Trisha Gee's avatar Trisha Gee
Browse files

[Ch6] GameHelper: Adding comments

parent f6dc2493
Loading
Loading
Loading
Loading
+10 −11
Changes for src/main/java/ch6/GameHelper.java: 10 added lines, 11 removed lines.
Original line number Diff line number Diff line
@@ -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);
@@ -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'
    }
  }

@@ -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
+4 −4
Changes for src/test/java/ch6/GameHelperTest.java: 4 added lines, 4 removed lines.
Original line number Diff line number Diff line
@@ -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);
@@ -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);
@@ -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
@@ -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