Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * This file can be run as a Java Application because it has a main method.
- *
- * Search for TODOs to complete four method stubs that compile but do not yet work.
- *
- * This class contains the code to escape obstacle courses stored in the project
- * in files with names like left.txt and big.txt.
- */
- import java.util.Scanner;
- import java.io.File;
- import java.io.FileNotFoundException;
- public class ObstacleCourse implements ObstacleCourseInterface {
- public static void main(String[] args) {
- ObstacleCourse aCourse = new ObstacleCourse("right.txt");
- boolean exitFound = aCourse.findTheExit(aCourse.getStartRow(), aCourse
- .getStartColumn());
- if (exitFound)
- System.out.println("Success!");
- else
- System.out.println("Could not find an exit!");
- System.out.println(aCourse.toString());
- }
- protected char[][] course;
- private int sRow;
- private int sCol;
- private int exitRow;
- private int exitCol;
- public ObstacleCourse(String fileName) {
- exitRow = -1;
- exitCol = -1;
- initializeCourseWith(fileName);
- }
- /**
- * Initializes the 2d char array course.
- */
- private void initializeCourseWith(String fileName) {
- Scanner obstacleCourseFile = null;
- try {
- obstacleCourseFile = new Scanner(new File(fileName));
- } catch (FileNotFoundException e) {
- System.out.println("Could not find file '" + fileName + "'");
- }
- int rows = obstacleCourseFile.nextInt();
- int columns = obstacleCourseFile.nextInt();
- // Get to the next line to read in maze line by line
- String line = obstacleCourseFile.nextLine();
- course = new char[rows][columns];
- for (int r = 0; r < rows; r++) {
- line = obstacleCourseFile.nextLine(); // read to end of line, end
- for (int c = 0; c < columns; c++) { // of line discarded
- course[r][c] = line.charAt(c);
- if (line.charAt(c) == 'S') {
- sRow = r;
- sCol = c;
- // Added after class so you don't have to worry about
- // the presence of 'S' in the possible method.
- course[r][c] = ' ';
- }
- }
- }
- }
- // TODO: You have to add and implement the other methods specified in the
- // interface (and any other methods needed by you).
- /**
- * Return a textual view of this obstacle course
- */
- public String toString() {
- String result = "";
- for (int r = 0; r < course.length; r++) {
- for (int c = 0; c < course[0].length; c++) { // of line discarded
- result += course[r][c] + " ";
- }
- result += "\n";
- }
- return result;
- }
- /*
- * Returns the start column for the start position. Determined by reading in
- * the input file and searching for the 'S'.
- */
- public int getStartColumn() {
- return sCol;
- }
- /*
- * Returns the start row for this obstacle course. Determined by reading in
- * the input file and searching for the 'S'.
- */
- public int getStartRow() {
- return sRow;
- }
- /*
- * Used to determine whether you can move into array[row][col]. It can only
- * move into that cell if it is not a wall and the position has not already
- * been tried. Returns true if you can move in, false if not.
- */
- public boolean possible(int row, int col) {
- // TODO Auto-generated method stub
- if(course[row][col]==' '){
- return true;
- }else{
- return false;
- }
- }
- /*
- * THE RECURSIVE-BACKTRACKING method which exhaustively searches the maze for
- * an exit. Returns: TRUE to the caller if at the current level, it found an
- * exit OR false, if it can't go anywhere else from the current position
- */
- public boolean findTheExit(int row, int col) {
- // TODO Auto-generated method stub
- boolean escaped=false;
- if(possible(row, col)==true){
- course[row][col]='.';
- if(row==0 || row==course.length-1 || col==0 || col==course[0].length-1){
- escaped=true;
- }else{
- escaped=findTheExit(row+1, col);
- if(escaped==false){
- escaped=findTheExit(row, col+1);
- }
- if(escaped==false){
- escaped=findTheExit(row-1, col);
- }
- if(escaped==false){
- escaped=findTheExit(row, col-1);
- }
- }
- if(escaped==true){
- exitRow=row;
- exitCol=col;
- course[row][col]='O';
- }
- }
- return escaped;
- }
- /*
- * Returns the column of the found exit. Return -1 before trying or and if no
- * exit was found after trying. There may be more than exit, but this method
- * must return the row where the exit was found that terminates findTheExit.
- */
- public int getExitColumn() {
- // TODO Auto-generated method stub
- int ans=-1;
- if(findTheExit(exitRow,exitCol)==true){
- ans=exitCol;
- }
- return ans;
- }
- /*
- * Returns the row of the found exit. Return -1 before trying or and if no
- * exit was found after trying. There may be more than exit, but this method
- * must return the row where the exit was found that terminates findTheExit.
- */
- public int getExitRow() {
- // TODO Auto-generated method stub
- int ans=-1;
- if(findTheExit(exitRow,exitCol)==true){
- ans=exitRow;
- }
- return ans;
- }
- }
Add Comment
Please, Sign In to add comment