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

[Ch6] GameHelper: Comments and some code organisation

parent bcaecf54
Loading
Loading
Loading
Loading
+18 −18
Changes for src/main/java/ch6/GameHelper.java: 18 added lines, 18 removed lines.
Original line number Diff line number Diff line
@@ -38,17 +38,17 @@ public class GameHelper {
      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;
        startupCoords[i] = location;                    // put current location in the array
        location += alignment.getIncrement();           // calculate the next location
      }
      System.out.println("Trying: " + Arrays.toString(startupCoords));

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

    savePositionToGrid(startupCoords);
    savePositionToGrid(startupCoords);                  // these coords passed everything, so update the grid with this startup
    ArrayList<String> alphaCells = convertCoordsToAlphaFormat(startupCoords);
    System.out.println(alphaCells);
    System.out.println(this);
@@ -64,12 +64,14 @@ public class GameHelper {
    }
  }

  private ArrayList<String> convertCoordsToAlphaFormat(int[] startupCoords) {
    ArrayList<String> alphaCells = new ArrayList<String>();
    for (int index : startupCoords) {                 // for each grid coordinate
      alphaCells.add(getAlphaCoordsFromIndex(index)); // turn it into an "a0" style and add to a list
  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
        System.out.println("position: " + coord + " already taken.");
        return false;                   // NO success
      }
    return alphaCells;
    }
    return true;                        // if the method got this far, there were no clashes, yay!
  }

  void savePositionToGrid(int[] startupCoords) {
@@ -78,6 +80,14 @@ public class GameHelper {
    }
  }

  private ArrayList<String> convertCoordsToAlphaFormat(int[] startupCoords) {
    ArrayList<String> alphaCells = new ArrayList<String>();
    for (int index : startupCoords) {                 // for each grid coordinate
      alphaCells.add(getAlphaCoordsFromIndex(index)); // turn it into an "a0" style and add to a list
    }
    return alphaCells;
  }

  String getAlphaCoordsFromIndex(int index) {
    int row = calculateRowFromIndex(index);           // get row value
    int column = index % GRID_LENGTH;        // get numeric column value
@@ -86,16 +96,6 @@ public class GameHelper {
    return letter + row;
  }

  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
        System.out.println("position: " + coord + " already taken.");
        return false;                   // NO success
      }
    }
    return true;                        // if the method got this far, there were no clashes, yay!
  }

  private Alignment getAlignment() {
    if (startupCount % 2 == 0) {                  // if EVEN Startup
      return HORIZONTAL;           // place horizontally