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

[Ch6] Formatting comments for Read Bake Code

parent fbd9bce9
Loading
Loading
Loading
Loading
+34 −35
Changes for src/main/java/ch6/GameHelper.java: 34 added lines, 35 removed lines.
Original line number Diff line number Diff line
package ch6;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
import java.util.*;

public class GameHelper {
  private static final String ALPHABET = "abcdefg";
@@ -11,8 +8,8 @@ public class GameHelper {
  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
  static final int HORIZONTAL_INCREMENT = 1;          // A better way to represent these two
  static final int VERTICAL_INCREMENT = GRID_LENGTH;  // things is an enum (see Appendix B)

  private final int[] grid = new int[GRID_SIZE];
  private final Random random = new Random();
@@ -23,7 +20,7 @@ public class GameHelper {
    System.out.print(prompt + ": ");
    Scanner scanner = new Scanner(System.in);
    return scanner.nextLine().toLowerCase();
  }
  } //end getUserInput

  public ArrayList<String> placeStartup(int startupSize) {
    // holds index to grid (0 - 48)
@@ -32,79 +29,81 @@ public class GameHelper {
    boolean success = false;                            // flag = found a good location?

    startupCount++;                                     // nth Startup to place
    int increment = getIncrement();                     // alternates between vertical and horizontal alignment
    int increment = getIncrement();                     // alternate vert & horiz alignment

    while (!success & attempts++ < MAX_ATTEMPTS) {      // main search loop  (32)
    while (!success & attempts++ < MAX_ATTEMPTS) {      // main search loop
      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
      for (int i = 0; i < startupCoords.length; i++) {  // create array of proposed coords
        startupCoords[i] = location;                    // put current location in array
        location += increment;                          // calculate the next location
      }
      System.out.println("Trying: " + Arrays.toString(startupCoords));

      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
      if (startupFits(startupCoords, increment)) {      // startup fits on the grid?
        success = coordsAvailable(startupCoords);       // ...and locations aren't taken?
      }                                                 // end loop
    }                                                   // end while

    savePositionToGrid(startupCoords);                  // these coords passed all checks, so update the grid with this startup's position
    savePositionToGrid(startupCoords);                  // coords passed checks, save
    ArrayList<String> alphaCells = convertCoordsToAlphaFormat(startupCoords);
    System.out.println(alphaCells);
    System.out.println("Placed at: "+ alphaCells);
    return alphaCells;
  }
  } //end placeStartup

  boolean startupFits(int[] startupCoords, int increment) {
    int finalLocation = startupCoords[startupCoords.length - 1];
    if (increment == HORIZONTAL_INCREMENT) {
      return calculateRowFromIndex(startupCoords[0]) == calculateRowFromIndex(finalLocation); // check end is on same row as start
      // check end is on same row as start
      return calcRowFromIndex(startupCoords[0]) == calcRowFromIndex(finalLocation);
    } else {
      return finalLocation < GRID_SIZE;                 // check end position doesn't go off the bottom of the grid
    }
      return finalLocation < GRID_SIZE;                 // check end isn't off the bottom
    }
  } //end startupFits

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

  void savePositionToGrid(int[] startupCoords) {
    for (int index : startupCoords) {
      grid[index] = 1;                                  // mark master grid position as 'used'
    }
      grid[index] = 1;                                  // mark grid position as 'used'
    }
  } //end savePositionToGrid

  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 alphaCoords = getAlphaCoordsFromIndex(index); // turn it into an "a0" style
      alphaCells.add(alphaCoords);                      // add to a list
    }
    return alphaCells;                                  // return the "a0"-style coords
  } // end convertCoordsToAlphaFormat

  String getAlphaCoordsFromIndex(int index) {
    int row = calculateRowFromIndex(index);             // get row value
    int row = calcRowFromIndex(index);                  // get row value
    int column = index % GRID_LENGTH;                   // get numeric column value

    String letter = ALPHABET.substring(column, column + 1); // convert column index to letter
    String letter = ALPHABET.substring(column, column + 1); // convert to letter
    return letter + row;
  }
  } // end getAlphaCoordsFromIndex

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

  private int getIncrement() {
    if (startupCount % 2 == 0) {                        // if EVEN Startup
      return HORIZONTAL_INCREMENT;                      // place horizontally
    } else {                                            // else ODD
      return VERTICAL_INCREMENT;                        // set vertical increment
    }
  }
      return VERTICAL_INCREMENT;                        // place vertically
    }
  } //end getIncrement
} //end class