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

[Ch6] GameHelper: Tidy up. Formatting comments, removing toString, removing enum.

parent f71a6a6f
Loading
Loading
Loading
Loading
+14 −41
Changes for src/main/java/ch6/GameHelper.java: 14 added lines, 41 removed lines.
Original line number Diff line number Diff line
@@ -5,15 +5,15 @@ import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

import static ch6.GameHelper.Alignment.HORIZONTAL;
import static ch6.GameHelper.Alignment.VERTICAL;

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;

  static final int HORIZONTAL_INCREMENT = 1;          // A better way to represent these two things would be an enum
  static final int VERTICAL_INCREMENT = GRID_LENGTH;  // (enumeration), which we cover in Appendix B

  private final int[] grid = new int[GRID_SIZE];
  private final Random random = new Random();

@@ -32,32 +32,31 @@ public class GameHelper {
    boolean success = false;                            // flag = found a good location ?

    startupCount++;                                     // nth Startup to place
    Alignment alignment = getAlignment();               // alternates between vertical and horizontal
    int increment = getIncrement();                     // alternates between vertical and horizontal alignment

    while (!success & attempts++ < MAX_ATTEMPTS) {      // main search loop  (32)
      int location = random.nextInt(GRID_SIZE);         // get random starting point

      for (int i = 0; i < startupCoords.length; i++) {  // create an array of proposed co-ordinates for the startup
        startupCoords[i] = location;                    // put current location in the array
        location += alignment.getIncrement();           // calculate the next location
        location += increment;                          // calculate the next location
      }
      System.out.println("Trying: " + Arrays.toString(startupCoords));

      if (startupFits(startupCoords, alignment)) {      // check the startup would fit on the grid
      if (startupFits(startupCoords, increment)) {      // check the startup would fit on the grid
        success = coordsAvailable(startupCoords);       // if it fits, check those locations aren't already taken
      }                                                 // end loop
    }                                                   // end while

    savePositionToGrid(startupCoords);                  // these coords passed everything, so update the grid with this startup
    savePositionToGrid(startupCoords);                  // these coords passed all checks, so update the grid with this startup's position
    ArrayList<String> alphaCells = convertCoordsToAlphaFormat(startupCoords);
    System.out.println(alphaCells);
    System.out.println(this);
    return alphaCells;
  }

  boolean startupFits(int[] startupCoords, Alignment alignment) {
  boolean startupFits(int[] startupCoords, int increment) {
    int finalLocation = startupCoords[startupCoords.length - 1];
    if (alignment == HORIZONTAL) {
    if (increment == HORIZONTAL_INCREMENT) {
      return calculateRowFromIndex(startupCoords[0]) == calculateRowFromIndex(finalLocation); // check end is on same row as start
    } else {
      return finalLocation < GRID_SIZE;                 // check end position doesn't go off the bottom of the grid
@@ -96,41 +95,15 @@ public class GameHelper {
    return letter + row;
  }

  private Alignment getAlignment() {
    if (startupCount % 2 == 0) {                  // if EVEN Startup
      return HORIZONTAL;           // place horizontally
    } else {                                      // else ODD
      return VERTICAL;             // set vertical increment
    }
  }

  private int calculateRowFromIndex(int index) {
    return index / GRID_LENGTH;
  }

  @Override
  public String toString() {
    StringBuilder str = new StringBuilder();
    for (int i = 0; i < grid.length; i++) {
      if (i % 7 == 0) {
        str.append("\n");
      }
      str.append(grid[i]).append(", ");
    }
    return str.toString();
  }

  enum Alignment {
    HORIZONTAL(1), VERTICAL(GRID_LENGTH);

    private final int increment;

    Alignment(int increment) {
      this.increment = increment;
    }

    public int getIncrement() {
      return increment;
  private int getIncrement() {
    if (startupCount % 2 == 0) {                        // if EVEN Startup
      return HORIZONTAL_INCREMENT;                      // place horizontally
    } else {                                            // else ODD
      return VERTICAL_INCREMENT;                        // set vertical increment
    }
  }
}
+3 −5
Changes for src/test/java/ch6/GameHelperTest.java: 3 added lines, 5 removed lines.
Original line number Diff line number Diff line
@@ -2,8 +2,6 @@ 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 {
@@ -47,7 +45,7 @@ class GameHelperTest {
    GameHelper gameHelper = new GameHelper();

    // expect
    assertFalse(gameHelper.startupFits(new int[]{26, 27, 28}, HORIZONTAL));
    assertFalse(gameHelper.startupFits(new int[]{26, 27, 28}, GameHelper.HORIZONTAL_INCREMENT));
  }

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

    // expect
    assertTrue(gameHelper.startupFits(new int[]{26, 33, 40}, VERTICAL));
    assertTrue(gameHelper.startupFits(new int[]{26, 33, 40}, GameHelper.VERTICAL_INCREMENT));
  }

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

    // expect
    assertFalse(gameHelper.startupFits(new int[]{47, 54, 61}, VERTICAL));
    assertFalse(gameHelper.startupFits(new int[]{47, 54, 61}, GameHelper.VERTICAL_INCREMENT));
  }

  @Test