Commit 8cdff2fe authored by Trisha Gee's avatar Trisha Gee
Browse files

[Ch6] First stab at simplifying the GameHelper. Adding the scanner; addressing...

[Ch6] First stab at simplifying the GameHelper. Adding the scanner; addressing IntelliJ IDEA warnings; differentiating between constants, finals, and mutable fields; using java.util.Random(); removing commented out code.
parent 96d73cd9
Loading
Loading
Loading
Loading
+31 −47
Changes for src/main/java/ch6/GameHelper.java: 31 added lines, 47 removed lines.
Original line number Diff line number Diff line
package ch6;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

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 String alphabet = "abcdefg";
  private int gridLength = 7;
  private int gridSize = 49;
  private int[] grid = new int[gridSize];
  private int comCount = 0;
  private final int[] grid = new int[GRID_SIZE];
  private final Random random = new Random();

  private int startupCount = 0;

  public String getUserInput(String prompt) {
    String inputLine = null;
    System.out.print(prompt + "  ");
    try {
      BufferedReader is = new BufferedReader(
              new InputStreamReader(System.in));
      inputLine = is.readLine();
      if (inputLine.length() == 0) {
        return null;
      }
    } catch (IOException e) {
      System.out.println("IOException: " + e);
    }
    return inputLine.toLowerCase();
    System.out.print(prompt + ": ");
    Scanner scanner = new Scanner(System.in);
    return scanner.nextLine().toLowerCase();
  }

  public ArrayList<String> placeStartup(int comSize) {
  public ArrayList<String> placeStartup(int startupSize) {
    ArrayList<String> alphaCells = new ArrayList<String>();
    // holds ‘f6' type coords
    String temp = null;                                // temporary String for concat
    int[] coords = new int[comSize];                  // current candidate coords
    int[] coords = new int[startupSize];                  // current candidate coords
    int attempts = 0;                                  // current attempts counter
    boolean success = false;                           // flag = found a good location ?
    int location = 0;                                  // current starting location

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

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

          coords[x++] = location;                    // save location
          coords[i++] = location;                    // save location
          location += incr;                          // try ‘next' adjacent
          if (location >= gridSize) {                 // out of bounds - ‘bottom'
          if (location >= GRID_SIZE) {                 // out of bounds - ‘bottom'
            success = false;                         // failure
          }
          if (x > 0 && (location % gridLength == 0)) {  // out of bounds - right edge
          if (location % GRID_LENGTH == 0) {  // out of bounds - right edge
            success = false;                         // failure
          }
        } else {                                      // found already used location
          // System.out.print(" used " + location);
          success = false;                          // failure
        }
      }
    }                                                   // end while

    int x = 0;                                          // turn location into alpha coords
    int i = 0;                                          // turn location into alpha coords
    int row = 0;
    int column = 0;
    // System.out.println("\n");
    while (x < comSize) {
      grid[coords[x]] = 1;                              // mark master grid pts. as ‘used'
      row = (int) (coords[x] / gridLength);             // get row value
      column = coords[x] % gridLength;                  // get numeric column value
      temp = String.valueOf(alphabet.charAt(column));   // convert to alpha
    String temp = null;                                // temporary String for concat
    while (i < startupSize) {
      grid[coords[i]] = 1;                              // mark master grid pts. as ‘used'
      row = (int) (coords[i] / GRID_LENGTH);             // get row value
      column = coords[i] % GRID_LENGTH;                  // get numeric column value
      temp = String.valueOf(ALPHABET.charAt(column));   // convert to alpha

      alphaCells.add(temp.concat(Integer.toString(row)));
      x++;
      // System.out.print("  coord "+x+" = " + alphaCells.get(x-1));
      i++;
    }

    // System.out.println("\n");

    return alphaCells;
  }
}
 No newline at end of file