Guest User

Untitled

a guest
Jul 16th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.15 KB | None | 0 0
  1. /**
  2. * This file can be run as a Java Application because it has a main method.
  3. *
  4. * Search for TODOs to complete four method stubs that compile but do not yet work.
  5. *
  6. * This class contains the code to escape obstacle courses stored in the project
  7. * in files with names like left.txt and big.txt.
  8. */
  9. import java.util.Scanner;
  10. import java.io.File;
  11. import java.io.FileNotFoundException;
  12.  
  13. public class ObstacleCourse implements ObstacleCourseInterface {
  14.  
  15. public static void main(String[] args) {
  16. ObstacleCourse aCourse = new ObstacleCourse("right.txt");
  17. boolean exitFound = aCourse.findTheExit(aCourse.getStartRow(), aCourse
  18. .getStartColumn());
  19. if (exitFound)
  20. System.out.println("Success!");
  21. else
  22. System.out.println("Could not find an exit!");
  23. System.out.println(aCourse.toString());
  24. }
  25.  
  26. protected char[][] course;
  27. private int sRow;
  28. private int sCol;
  29. private int exitRow;
  30. private int exitCol;
  31.  
  32. public ObstacleCourse(String fileName) {
  33. exitRow = -1;
  34. exitCol = -1;
  35. initializeCourseWith(fileName);
  36. }
  37.  
  38. /**
  39. * Initializes the 2d char array course.
  40. */
  41. private void initializeCourseWith(String fileName) {
  42.  
  43. Scanner obstacleCourseFile = null;
  44. try {
  45. obstacleCourseFile = new Scanner(new File(fileName));
  46. } catch (FileNotFoundException e) {
  47. System.out.println("Could not find file '" + fileName + "'");
  48. }
  49.  
  50. int rows = obstacleCourseFile.nextInt();
  51. int columns = obstacleCourseFile.nextInt();
  52.  
  53. // Get to the next line to read in maze line by line
  54. String line = obstacleCourseFile.nextLine();
  55. course = new char[rows][columns];
  56. for (int r = 0; r < rows; r++) {
  57. line = obstacleCourseFile.nextLine(); // read to end of line, end
  58. for (int c = 0; c < columns; c++) { // of line discarded
  59. course[r][c] = line.charAt(c);
  60. if (line.charAt(c) == 'S') {
  61. sRow = r;
  62. sCol = c;
  63. // Added after class so you don't have to worry about
  64. // the presence of 'S' in the possible method.
  65. course[r][c] = ' ';
  66. }
  67. }
  68. }
  69. }
  70.  
  71. // TODO: You have to add and implement the other methods specified in the
  72. // interface (and any other methods needed by you).
  73.  
  74. /**
  75. * Return a textual view of this obstacle course
  76. */
  77. public String toString() {
  78. String result = "";
  79. for (int r = 0; r < course.length; r++) {
  80. for (int c = 0; c < course[0].length; c++) { // of line discarded
  81. result += course[r][c] + " ";
  82. }
  83. result += "\n";
  84. }
  85. return result;
  86. }
  87.  
  88. /*
  89. * Returns the start column for the start position. Determined by reading in
  90. * the input file and searching for the 'S'.
  91. */
  92. public int getStartColumn() {
  93. return sCol;
  94. }
  95.  
  96. /*
  97. * Returns the start row for this obstacle course. Determined by reading in
  98. * the input file and searching for the 'S'.
  99. */
  100. public int getStartRow() {
  101. return sRow;
  102. }
  103.  
  104. /*
  105. * Used to determine whether you can move into array[row][col]. It can only
  106. * move into that cell if it is not a wall and the position has not already
  107. * been tried. Returns true if you can move in, false if not.
  108. */
  109. public boolean possible(int row, int col) {
  110. // TODO Auto-generated method stub
  111. if(course[row][col]==' '){
  112. return true;
  113. }else{
  114. return false;
  115. }
  116. }
  117.  
  118. /*
  119. * THE RECURSIVE-BACKTRACKING method which exhaustively searches the maze for
  120. * an exit. Returns: TRUE to the caller if at the current level, it found an
  121. * exit OR false, if it can't go anywhere else from the current position
  122. */
  123. public boolean findTheExit(int row, int col) {
  124. // TODO Auto-generated method stub
  125. boolean escaped=false;
  126. if(possible(row, col)==true){
  127. course[row][col]='.';
  128. if(row==0 || row==course.length-1 || col==0 || col==course[0].length-1){
  129. escaped=true;
  130. }else{
  131. escaped=findTheExit(row+1, col);
  132. if(escaped==false){
  133. escaped=findTheExit(row, col+1);
  134. }
  135. if(escaped==false){
  136. escaped=findTheExit(row-1, col);
  137. }
  138. if(escaped==false){
  139. escaped=findTheExit(row, col-1);
  140. }
  141. }
  142. if(escaped==true){
  143. exitRow=row;
  144. exitCol=col;
  145. course[row][col]='O';
  146. }
  147. }
  148. return escaped;
  149. }
  150.  
  151. /*
  152. * Returns the column of the found exit. Return -1 before trying or and if no
  153. * exit was found after trying. There may be more than exit, but this method
  154. * must return the row where the exit was found that terminates findTheExit.
  155. */
  156. public int getExitColumn() {
  157. // TODO Auto-generated method stub
  158. int ans=-1;
  159. if(findTheExit(exitRow,exitCol)==true){
  160. ans=exitCol;
  161. }
  162. return ans;
  163. }
  164.  
  165. /*
  166. * Returns the row of the found exit. Return -1 before trying or and if no
  167. * exit was found after trying. There may be more than exit, but this method
  168. * must return the row where the exit was found that terminates findTheExit.
  169. */
  170. public int getExitRow() {
  171. // TODO Auto-generated method stub
  172. int ans=-1;
  173. if(findTheExit(exitRow,exitCol)==true){
  174. ans=exitRow;
  175. }
  176. return ans;
  177. }
  178.  
  179. }
Add Comment
Please, Sign In to add comment