Commit 7b3a7e55 authored by Trisha Gee's avatar Trisha Gee
Browse files

[Ch6] GameHelper: Simplified the place method. Some tests fail because I moved...

[Ch6] GameHelper: Simplified the place method. Some tests fail because I moved the persistence out of it
parent 9397fc8d
Loading
Loading
Loading
Loading
+8 −13
Changes for src/main/java/ch6/GameHelper.java: 8 added lines, 13 removed lines.
Original line number Diff line number Diff line
@@ -41,15 +41,19 @@ public class GameHelper {
      } else{
        success = verticalStartupFits(location, startupSize);
      }
      for (int i = 0; i < startupCoords.length; i++) {
        startupCoords[i] = location =+ alignment.getIncrement();
      }

      if (success) {
        success = bruteForcePlace(startupSize, startupCoords, alignment.getIncrement(), location);
        success = canPlaceStartup(startupCoords);
      }
    }                                                   // 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);
    }
@@ -66,26 +70,17 @@ public class GameHelper {
    return letter + row;
  }

  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) {
      System.out.println("check it's available. k=" + k + ", startupSize:" + startupSize);
  boolean canPlaceStartup(int[] startupCoords) {
    for (int coord : startupCoords) {
      // check all potential positions
      if (grid[j] == 0) {
      if (grid[coord] == 0) {
        System.out.println("yes");
        startupCoords[k++] = j;                    // save location
        System.out.println("startupCoords = " + Arrays.toString(startupCoords));
      } else {
        System.out.println("no");
        return false;
      }
    }
    // this is awful, more than one iteration
    for (int index : startupCoords) {
      grid[index] = 1;                         // mark master grid position as ‘used'
    }

    return true;
  }

+8 −10
Changes for src/test/java/ch6/GameHelperTest.java: 8 added lines, 10 removed lines.
Original line number Diff line number Diff line
@@ -72,8 +72,8 @@ class GameHelperTest {
    GameHelper gameHelper = new GameHelper();

    // when
    int[] startupCoords = new int[3];
    boolean success = gameHelper.bruteForcePlace(3, startupCoords, 1, 0);
    int[] startupCoords = new int[]{0, 1, 2};
    boolean success = gameHelper.canPlaceStartup(startupCoords);

    // then
    assertTrue(success);
@@ -86,25 +86,23 @@ class GameHelperTest {
    GameHelper gameHelper = new GameHelper();

    // when
    int[] startupCoords = new int[3];
    boolean success = gameHelper.bruteForcePlace(3, startupCoords, 7, 0);
    boolean success = gameHelper.canPlaceStartup(new int[]{0, 7, 14});

    // 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"
    boolean setupSuccess = gameHelper.canPlaceStartup(new int[]{8, 9, 10}); // "B1"
    assertTrue(setupSuccess);
    System.out.println(gameHelper);

    // when
    int[] startupCoords = new int[3];
    boolean success = gameHelper.bruteForcePlace(3, startupCoords, 1, 9); // "C1" - horizontal
    boolean success = gameHelper.canPlaceStartup(new int[]{9, 10, 11}); // "C1" - horizontal
    System.out.println(gameHelper);

    // then
@@ -115,13 +113,13 @@ class GameHelperTest {
  void shouldNotBeAbleToPlaceVerticalIntoGridAtSameLocationAsExisting() {
    // given
    GameHelper gameHelper = new GameHelper();
    boolean setupSuccess = gameHelper.bruteForcePlace(3, new int[3], 1, 8); // "B1"
    boolean setupSuccess = gameHelper.canPlaceStartup(new int[]{8, 9, 10}); // "B1"
    assertTrue(setupSuccess);
    System.out.println(gameHelper);

    // when
    int[] startupCoords = new int[3];
    boolean success = gameHelper.bruteForcePlace(3, startupCoords, 7, 1); // "A1" - vertical
    int[] startupCoords = new int[]{1, 9, 16};
    boolean success = gameHelper.canPlaceStartup(startupCoords); // "A1" - vertical
    System.out.println(gameHelper);

    // then