Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package assignment09;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.Arrays;
- /**
- * Represents a PacMan Map in graph form from a formatted text file
- *
- * @author Benjamin Wadsworth and Adrian Sahbaz
- * @uid u0991657 and u0963488
- * @date 3/30/2017
- * @class CS 2420
- * @assignment 9 - PacMan
- * @pledge I pledge that the work done here was my own and that I have learned how to write this
- * program, such that I could throw it out and restart and finish it in a timely manner. I
- * am not turning in any work that I cannot understand, describe, or recreate. I further
- * acknowledge that I contributed substantially to all code handed in and vouch for it's
- * authenticity. Benjamin Wadsworth
- */
- public class Graph
- {
- private static final char X = (char) 0xFFFF;
- private int start;
- private int goal;
- private ArrayList<Node> graph;
- public Graph(File file) throws IOException
- {
- //Find rows/columns from first line
- int r = 0;
- int c = 0;
- BufferedReader br = new BufferedReader(new FileReader(file));
- if (br.ready())
- {
- String[] s = br.readLine().split(" ");
- if (s.length != 2)
- {
- throw new IOException("No row/column declaration!");
- }
- r = Integer.parseInt(s[0]);
- c = Integer.parseInt(s[1]);
- }
- else
- {
- throw new IOException("No file data!");
- }
- //Read each line, creating new nodes for all spaces and assign edges from previous
- int lines = 0;
- ArrayList<Node> temp = new ArrayList<>();
- char[] last = new char[c];
- Arrays.fill(last, X);
- char counter = 0;
- while (br.ready())
- {
- char[] chars = br.readLine().toCharArray();
- if (chars.length != c)
- {
- throw new IOException("Improper Column on line " + (lines + 1));
- }
- //Check each index for content and assign nodes
- for (int i = 0; i < chars.length; i++)
- {
- switch (chars[i])
- {
- //Normal Node
- case ' ':
- {
- chars[i] = counter;
- Node next = new Node();
- //Associate with its left
- if (i != 0 && chars[i-1] != X)
- {
- Node left = temp.get(chars[i-1]);
- next.edges.add(left);
- left.edges.add(next);
- }
- //Associate with top
- if (last[i] != X)
- {
- Node top = temp.get(last[i]);
- next.edges.add(top);
- top.edges.add(next);
- }
- temp.add(new Node());
- counter ++;
- break;
- }
- //Wall, replace to end and ignore
- case 'X':
- {
- chars[i] = X;
- break;
- }
- //Normal node, plus assign start
- case 'S':
- {
- chars[i] = counter;
- Node next = new Node();
- //Associate with its left
- if (i != 0 && chars[i-1] != X)
- {
- Node left = temp.get(chars[i-1]);
- next.edges.add(left);
- left.edges.add(next);
- }
- //Associate with top
- if (last[i] != X)
- {
- Node top = temp.get(last[i]);
- next.edges.add(top);
- top.edges.add(next);
- }
- temp.add(new Node());
- start = counter;
- counter ++;
- break;
- }
- //Normal node, plus assign goal
- case 'G':
- {
- chars[i] = counter;
- Node next = new Node();
- //Associate with its left
- if (i != 0 && chars[i-1] != X)
- {
- Node left = temp.get(chars[i-1]);
- next.edges.add(left);
- left.edges.add(next);
- }
- //Associate with top
- if (last[i] != X)
- {
- Node top = temp.get(last[i]);
- next.edges.add(top);
- top.edges.add(next);
- }
- temp.add(new Node());
- goal = counter;
- counter ++;
- break;
- }
- //Not Recognized
- default:
- {
- throw new IOException("Incorrect Character at " + (lines + 1) + " " + (i + 1));
- }
- }
- }
- //Put the line just read into the last line variable
- last = chars;
- lines ++;
- }
- if (lines != r)
- {
- throw new IOException("Too Many Lines!");
- }
- graph = temp;
- br.close();
- }
- /**
- * Represents a node within the graph
- */
- public class Node
- {
- //ArrayList<Node> edges;
- ArrayList<Integer> edges;
- boolean visited;
- Node ()
- {
- visited = false;
- edges = new ArrayList<>();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement