/**
   * Looks at the surrounding cells based on the current one to determine which ways have been tried
   * already and sets the appropriate boolean values in the current cell
   *
   * @param currentCell -- this is the cell in the maze the robot is currently in
   * @param map -- this is the array that stores the cell data of all explored cells in the maze
   */
  public static void analyzeSurroundings(CellData currentCell, CellData[][] map) {
    int x = currentCell.getX();
    int y = currentCell.getY();

    // look at the cell to the North
    try {
      CellData north = map[x][y + 1];
      if (north == null) {
      } else {
        currentCell.setTriedNorth(true);
      }
    } catch (IndexOutOfBoundsException e) {
      // exception thrown if at the edge of the maze
      currentCell.setTriedNorth(true);
    }

    // look at the cell to the East
    try {
      CellData east = map[x + 1][y];
      if (east == null) {
      } else {
        currentCell.setTriedEast(true);
      }
    } catch (IndexOutOfBoundsException e) {
      // exception thrown if at the edge of the maze
      currentCell.setTriedEast(true);
    }

    // look at the cell to the South
    try {
      CellData south = map[x][y - 1];
      if (south == null) {
      } else {
        currentCell.setTriedSouth(true);
      }
    } catch (IndexOutOfBoundsException e) {
      // exception thrown if at the edge of the maze
      currentCell.setTriedSouth(true);
    }

    // look at the cell to the West
    try {
      CellData west = map[x - 1][y];
      if (west == null) {
      } else {
        currentCell.setTriedWest(true);
      }
    } catch (IndexOutOfBoundsException e) {
      // exception thrown if at the edge of the maze
      currentCell.setTriedWest(true);
    }
  }
  public static void findClosestMove(
      SimRobot simRobot,
      int angle,
      LinkedList<CellData> moveHistory,
      LinkedList<CellData> notFullyExplored) {
    CellData destination = notFullyExplored.pop();
    CellData currentCell = moveHistory.pop();
    CellData nextMove = moveHistory.peek();

    // while the robot is still making its way back to the closest open move
    while ((currentCell.getX() != destination.getX())
        || (currentCell.getY() != destination.getY())) {
      if (currentCell.getX() - nextMove.getX() == 0) {
        // if the x values are the same then move either north or south
        if (currentCell.getY() - nextMove.getY() == 1) {
          moveSouth(simRobot, angle);
          angle = 180;
          currentCell = moveHistory.pop();
        } else if (currentCell.getY() - nextMove.getY() == -1) {
          moveNorth(simRobot, angle);
          angle = 0;
          currentCell = moveHistory.pop();
        }
      } else {
        if (currentCell.getX() - nextMove.getX() == 1) {
          moveWest(simRobot, angle);
          angle = 270;
          currentCell = moveHistory.pop();
        } else if (currentCell.getX() - nextMove.getX() == -1) {
          moveEast(simRobot, angle);
          angle = 90;
          currentCell = moveHistory.pop();
        }
      }
      nextMove = moveHistory.peek();
    }

    // orient robot north after arrived at desired cell
    if (angle == 90) {
      simRobot.left90();
    } else if (angle == 180) {
      simRobot.left90();
      simRobot.left90();
    } else if (angle == 270) {
      simRobot.right90();
    }
  }
  public static void goHome(SimRobot simRobot, int angle, LinkedList<CellData> moveHistory) {
    System.out.println("E.T. phone home...");
    CellData destination = moveHistory.getLast();
    CellData currentCell = moveHistory.pop();
    CellData nextMove = moveHistory.peek();

    while ((currentCell.getX() != destination.getX())
        || (currentCell.getY() != destination.getY())) {
      System.out.println(
          "The current cell is (" + currentCell.getX() + ", " + currentCell.getY() + ")");
      System.out.println(
          "The next cell should be (" + nextMove.getX() + ", " + nextMove.getY() + ")");
      if ((currentCell.getX() - nextMove.getX() == 0)
          && (currentCell.getY() - nextMove.getY() == 0)) {
        System.out.println("There was a duplicate");
        currentCell = moveHistory.pop();
      } else if (currentCell.getX() - nextMove.getX() == 0) {
        // if the x values are the same then move either north or south
        System.out.println("Move North or South");
        if (currentCell.getY() - nextMove.getY() == 1) {
          // then go south
          System.out.println("Go South");
          if (angle % 360 == 0) {
            simRobot.right90();
            simRobot.right90();
          } else if (angle % 360 == 90) {
            simRobot.right90();
          } else if (angle % 360 == 270) {
            simRobot.left90();
          }
          simRobot.forwardOneCell();
          // reset the angle to south
          angle = 180;
          currentCell = moveHistory.pop();
        } else {
          // then go North
          System.out.println("Go North");
          if (angle % 360 == 90) {
            simRobot.left90();
          } else if (angle % 360 == 180) {
            simRobot.left90();
            simRobot.left90();
          } else if (angle % 360 == 270) {
            simRobot.right90();
          }
          simRobot.forwardOneCell();
          // reset the angle to North
          angle = 0;
          currentCell = moveHistory.pop();
        }
      } else if (currentCell.getY() - nextMove.getY() == 0) {
        System.out.println("Move East or West");
        if (currentCell.getX() - nextMove.getX() == 1) {
          // go west
          System.out.println("Go West");
          if (angle % 360 == 0) {
            simRobot.left90();
          } else if (angle % 360 == 90) {
            simRobot.right90();
            simRobot.right90();
          } else if (angle % 360 == 180) {
            simRobot.right90();
          }
          simRobot.forwardOneCell();
          // reset the angle to North
          angle = 270;
          currentCell = moveHistory.pop();
        } else {
          // go East
          System.out.println("Go East");
          if (angle % 360 == 0) {
            simRobot.right90();
          } else if (angle % 360 == 180) {
            simRobot.left90();
          } else if (angle % 360 == 270) {
            simRobot.right90();
            simRobot.right90();
          }
          simRobot.forwardOneCell();
          // reset the angle to North
          angle = 90;
          currentCell = moveHistory.pop();
        }
      }
      nextMove = moveHistory.peek();
    }
    System.out.println("E.T. made it back successfully!");
  }