Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.20 KB | None | 0 0
  1. package assignment09;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.File;
  5. import java.io.FileReader;
  6. import java.io.IOException;
  7. import java.util.ArrayList;
  8. import java.util.Arrays;
  9.  
  10. /**
  11. * Represents a PacMan Map in graph form from a formatted text file
  12. *
  13. * @author Benjamin Wadsworth and Adrian Sahbaz
  14. * @uid u0991657 and u0963488
  15. * @date 3/30/2017
  16. * @class CS 2420
  17. * @assignment 9 - PacMan
  18. * @pledge I pledge that the work done here was my own and that I have learned how to write this
  19. * program, such that I could throw it out and restart and finish it in a timely manner. I
  20. * am not turning in any work that I cannot understand, describe, or recreate. I further
  21. * acknowledge that I contributed substantially to all code handed in and vouch for it's
  22. * authenticity. Benjamin Wadsworth
  23. */
  24. public class Graph
  25. {
  26. private static final char X = (char) 0xFFFF;
  27. private int start;
  28. private int goal;
  29.  
  30. private ArrayList<Node> graph;
  31.  
  32. public Graph(File file) throws IOException
  33. {
  34. //Find rows/columns from first line
  35. int r = 0;
  36. int c = 0;
  37. BufferedReader br = new BufferedReader(new FileReader(file));
  38. if (br.ready())
  39. {
  40. String[] s = br.readLine().split(" ");
  41. if (s.length != 2)
  42. {
  43. throw new IOException("No row/column declaration!");
  44. }
  45.  
  46. r = Integer.parseInt(s[0]);
  47. c = Integer.parseInt(s[1]);
  48. }
  49. else
  50. {
  51. throw new IOException("No file data!");
  52. }
  53.  
  54. //Read each line, creating new nodes for all spaces and assign edges from previous
  55. int lines = 0;
  56. ArrayList<Node> temp = new ArrayList<>();
  57. char[] last = new char[c];
  58. Arrays.fill(last, X);
  59. char counter = 0;
  60. while (br.ready())
  61. {
  62. char[] chars = br.readLine().toCharArray();
  63. if (chars.length != c)
  64. {
  65. throw new IOException("Improper Column on line " + (lines + 1));
  66. }
  67.  
  68. //Check each index for content and assign nodes
  69. for (int i = 0; i < chars.length; i++)
  70. {
  71. switch (chars[i])
  72. {
  73. //Normal Node
  74. case ' ':
  75. {
  76. chars[i] = counter;
  77. Node next = new Node();
  78. //Associate with its left
  79. if (i != 0 && chars[i-1] != X)
  80. {
  81. Node left = temp.get(chars[i-1]);
  82. next.edges.add(left);
  83. left.edges.add(next);
  84. }
  85. //Associate with top
  86. if (last[i] != X)
  87. {
  88. Node top = temp.get(last[i]);
  89. next.edges.add(top);
  90. top.edges.add(next);
  91. }
  92. temp.add(new Node());
  93. counter ++;
  94. break;
  95. }
  96. //Wall, replace to end and ignore
  97. case 'X':
  98. {
  99. chars[i] = X;
  100. break;
  101. }
  102. //Normal node, plus assign start
  103. case 'S':
  104. {
  105. chars[i] = counter;
  106. Node next = new Node();
  107. //Associate with its left
  108. if (i != 0 && chars[i-1] != X)
  109. {
  110. Node left = temp.get(chars[i-1]);
  111. next.edges.add(left);
  112. left.edges.add(next);
  113. }
  114. //Associate with top
  115. if (last[i] != X)
  116. {
  117. Node top = temp.get(last[i]);
  118. next.edges.add(top);
  119. top.edges.add(next);
  120. }
  121. temp.add(new Node());
  122. start = counter;
  123. counter ++;
  124. break;
  125. }
  126. //Normal node, plus assign goal
  127. case 'G':
  128. {
  129. chars[i] = counter;
  130. Node next = new Node();
  131. //Associate with its left
  132. if (i != 0 && chars[i-1] != X)
  133. {
  134. Node left = temp.get(chars[i-1]);
  135. next.edges.add(left);
  136. left.edges.add(next);
  137. }
  138. //Associate with top
  139. if (last[i] != X)
  140. {
  141. Node top = temp.get(last[i]);
  142. next.edges.add(top);
  143. top.edges.add(next);
  144. }
  145. temp.add(new Node());
  146. goal = counter;
  147. counter ++;
  148. break;
  149. }
  150. //Not Recognized
  151. default:
  152. {
  153. throw new IOException("Incorrect Character at " + (lines + 1) + " " + (i + 1));
  154. }
  155. }
  156. }
  157. //Put the line just read into the last line variable
  158. last = chars;
  159. lines ++;
  160. }
  161. if (lines != r)
  162. {
  163. throw new IOException("Too Many Lines!");
  164. }
  165. graph = temp;
  166.  
  167. br.close();
  168. }
  169.  
  170. /**
  171. * Represents a node within the graph
  172. */
  173. public class Node
  174. {
  175. //ArrayList<Node> edges;
  176. ArrayList<Integer> edges;
  177. boolean visited;
  178.  
  179. Node ()
  180. {
  181. visited = false;
  182. edges = new ArrayList<>();
  183. }
  184. }
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement