Commit f6dc2493 authored by Trisha Gee's avatar Trisha Gee
Browse files

[Ch6] GameHelper: Simplified canPlace. Broke out updating the grid into its...

[Ch6] GameHelper: Simplified canPlace. Broke out updating the grid into its own method so I can actually test the thing. Also, method names with "and" in suck
parent 7b3a7e55
Loading
Loading
Loading
Loading
+39 −27
Changes for src/main/java/ch6/GameHelper.java: 39 added lines, 27 removed lines.
Original line number Diff line number Diff line
@@ -32,36 +32,55 @@ public class GameHelper {
    boolean success = false;                            // flag = found a good location ?

    startupCount++;                                     // nth Startup to place
    Alignment alignment = calculateIncrement();
    Alignment alignment = getAlignment();

    while (!success & attempts++ < MAX_ATTEMPTS) {      // main search loop  (32)
      int location = random.nextInt(GRID_SIZE);         // get random starting point
      if (alignment == HORIZONTAL) {
        success = horizontalStartupFits(location, startupSize);
      } else{
        success = verticalStartupFits(location, startupSize);
      }
      boolean validLocation = isValidLocation(location, startupSize, alignment);

      if (validLocation) {
        for (int i = 0; i < startupCoords.length; i++) {
        startupCoords[i] = location =+ alignment.getIncrement();
          startupCoords[i] = location;
          location += alignment.getIncrement();
        }
        System.out.println("trying: " + Arrays.toString(startupCoords));

      if (success) {
        success = canPlaceStartup(startupCoords);
        success = allPositionsAvailable(startupCoords);
      }
    }                                                   // end while
    System.out.println(this);

    savePositionToGrid(startupCoords);
    ArrayList<String> alphaCells = convertCoordsToAlphaFormat(startupCoords);
    System.out.println(alphaCells);
    System.out.println(this);
    return alphaCells;
  }

  private boolean isValidLocation(int location, int startupSize, Alignment alignment) {
    int finalLocation = location + (alignment.getIncrement() * startupSize);
    if (alignment == HORIZONTAL) {
      return horizontalStartupFits(location, finalLocation);
    } else {
      return verticalStartupFits(finalLocation);
    }
  }

  private ArrayList<String> convertCoordsToAlphaFormat(int[] startupCoords) {
    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);
    }
    System.out.println(alphaCells);
    System.out.println(this);
    return alphaCells;
  }

  void savePositionToGrid(int[] startupCoords) {
    for (int index : startupCoords) {                 // turn location into 'a0'-style startupCoords
      grid[index] = 1;                         // mark master grid position as ‘used'
    }
  }

  String getAlphaCoordsFromIndex(int index) {
    int row = calculateRowFromIndex(index);           // get row value
    int column = index % GRID_LENGTH;        // get numeric column value
@@ -70,31 +89,24 @@ public class GameHelper {
    return letter + row;
  }

  boolean canPlaceStartup(int[] startupCoords) {
    for (int coord : startupCoords) {
      // check all potential positions
      if (grid[coord] == 0) {
        System.out.println("yes");
        System.out.println("startupCoords = " + Arrays.toString(startupCoords));
      } else {
        System.out.println("no");
        return false;
  boolean allPositionsAvailable(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
      }
    }
    return true;
    return true;                        // if the method got this far, there were no clashes, yay!
  }

  boolean horizontalStartupFits(int location, int startupSize) {
    int finalLocation = location + (HORIZONTAL.getIncrement() * startupSize);
  boolean horizontalStartupFits(int location, int finalLocation) {
    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);
  boolean verticalStartupFits(int finalLocation) {
    return finalLocation < GRID_SIZE; // check end position doesn't go off the bottom of the grid
  }

  private Alignment calculateIncrement() {
  private Alignment getAlignment() {
    if (startupCount % 2 == 0) {                  // if EVEN Startup
      return HORIZONTAL;           // place horizontally
    } else {                                      // else ODD
+11 −12
Changes for src/test/java/ch6/GameHelperTest.java: 11 added lines, 12 removed lines.
Original line number Diff line number Diff line
@@ -2,6 +2,8 @@ package ch6;

import org.junit.jupiter.api.Test;

import static ch6.GameHelper.Alignment.HORIZONTAL;
import static ch6.GameHelper.Alignment.VERTICAL;
import static org.junit.jupiter.api.Assertions.*;

class GameHelperTest {
@@ -45,7 +47,7 @@ class GameHelperTest {
    GameHelper gameHelper = new GameHelper();

    // expect
    assertFalse(gameHelper.horizontalStartupFits(26, 3));
    assertFalse(gameHelper.horizontalStartupFits(26, 26 + (HORIZONTAL.getIncrement() * 3)));
  }

  @Test
@@ -54,7 +56,7 @@ class GameHelperTest {
    GameHelper gameHelper = new GameHelper();

    // expect
    assertTrue(gameHelper.verticalStartupFits(26, 3));
    assertTrue(gameHelper.verticalStartupFits(26 + (VERTICAL.getIncrement() * 3)));
  }

  @Test
@@ -63,7 +65,7 @@ class GameHelperTest {
    GameHelper gameHelper = new GameHelper();

    // expect
    assertFalse(gameHelper.verticalStartupFits(47, 3));
    assertFalse(gameHelper.verticalStartupFits(47 + (VERTICAL.getIncrement() * 3)));
  }

  @Test
@@ -73,7 +75,7 @@ class GameHelperTest {

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

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

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

    // then
    assertTrue(success);
@@ -96,13 +98,11 @@ class GameHelperTest {
  void shouldNotBeAbleToPlaceHorizontalIntoGridAtSameLocationAsExisting() {
    // given
    GameHelper gameHelper = new GameHelper();
    boolean setupSuccess = gameHelper.canPlaceStartup(new int[]{8, 9, 10}); // "B1"
    assertTrue(setupSuccess);
    gameHelper.savePositionToGrid(new int[]{8, 9, 10}); // "B1"
    System.out.println(gameHelper);

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

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

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

    // then