Commit 6ae49187 authored by Trisha Gee's avatar Trisha Gee
Browse files

[Ch6] GameHelper: partial refactor. Trying to simplify the logic that brute...

[Ch6] GameHelper: partial refactor. Trying to simplify the logic that brute forces finding a location for the startup.
parent aee7926b
Loading
Loading
Loading
Loading
+32 −19
Changes for src/main/java/ch6/GameHelper.java: 32 added lines, 19 removed lines.
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@ public class GameHelper {
  private static final String ALPHABET = "abcdefg";
  private static final int GRID_LENGTH = 7;
  private static final int GRID_SIZE = 49;
  private static final int MAX_ATTEMPTS = 200;

  private final int[] grid = new int[GRID_SIZE];
  private final Random random = new Random();
@@ -23,49 +24,61 @@ public class GameHelper {
  public ArrayList<String> placeStartup(int startupSize) {
    ArrayList<String> alphaCells = new ArrayList<String>();
    // holds index to grid (0 - 48)
    int[] coords = new int[startupSize];                  // current candidate coords
    int[] startupCoords = new int[startupSize];        // current candidate co-ordinates
    int attempts = 0;                                  // current attempts counter
    boolean success = false;                           // flag = found a good location ?
    int location = 0;                                  // current starting location

    startupCount++;                                        // nth Startup to place
    int incr = 1;                                      // set horizontal increment
    if ((startupCount % 2) == 1) {                         // if odd Startup (place vertically)
      incr = GRID_LENGTH;                               // set vertical increment
    }
    int increment = calculateIncrement();

    while (!success & attempts++ < 200) {             // main search loop  (32)
      location = random.nextInt(GRID_SIZE);     // get random starting point
      int i = 0;                                       // nth position in Startup to place
    while (!success & attempts++ < MAX_ATTEMPTS) {             // main search loop  (32)
      int location = random.nextInt(GRID_SIZE);     // get random starting point
      success = true;                                 // assume success
      int i = 0;
      while (success && i < startupSize) {                // look for adjacent unused spots
        if (grid[location] == 0) {                    // if not already used

          coords[i++] = location;                    // save location
          location += incr;                          // try ‘next' adjacent
          if (location >= GRID_SIZE) {                 // out of bounds - ‘bottom'
            success = false;                         // failure
          }
          if (location % GRID_LENGTH == 0) {  // out of bounds - right edge
            success = false;                         // failure
          success = startupFits(location, startupSize, increment);
          if (success) {
            startupCoords[i++] = location;                    // save location
            location += increment;                          // try ‘next' adjacent
          }
        } else {                                      // found already used location
          success = false;                          // failure
        }
      }
    }                                                   // end while
    System.out.println(this);

    for (int index : coords) {                 // turn location into 'a0'-style coords
      grid[index] = 1;                         // mark master grid pts. as ‘used'
    for (int index : startupCoords) {                 // turn location into 'a0'-style startupCoords
      grid[index] = 1;                         // mark master grid position as ‘used'
      int row = index / GRID_LENGTH;           // get row value
      int column = index % GRID_LENGTH;        // get numeric column value

      String letter = ALPHABET.substring(column, column + 1); // convert column index to letter
      alphaCells.add(letter + row);
    }
    System.out.println(alphaCells);
    System.out.println(this);
    return alphaCells;
  }

  private boolean startupFits(int location, int startupSize, int increment) {
    int finalLocation = location + (increment * startupSize);
    if (finalLocation > GRID_SIZE || finalLocation % GRID_LENGTH == 0) {
      System.out.println("location = " + location + ", startupSize = " + startupSize + ", increment = " + increment);
      return false;
    }
    return true;
  }

  private int calculateIncrement() {
    if (startupCount % 2 == 0) {                // if EVEN Startup
      return 1;                                 // place horizontally
    } else {                                    // else ODD
      return GRID_LENGTH;                       // set vertical increment
    }
  }

  @Override
  public String toString() {
    StringBuilder str = new StringBuilder();