Created
October 24, 2012 04:42
-
-
Save justcoding121/3943904 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this template, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package aiproject; | |
import java.awt.Point; | |
import java.io.BufferedReader; | |
import java.io.File; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.Scanner; | |
/** | |
* | |
* @author Jehonathan | |
*/ | |
public class Reader { | |
/** | |
* @param args the command line arguments | |
*/ | |
public String FNAME_input_connections; | |
public String FNAME_input_locations; | |
public Scanner conn; | |
public Scanner loc; | |
ArrayList Nodes = new ArrayList();//list of cities | |
HashMap mapper = new HashMap();//used to input file reading purpose | |
public Searcher g = new Searcher();//create the search object | |
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | |
public String start, destination, excluded; | |
public String connections = ""; | |
//Open Files | |
public void open() throws IOException { | |
conn = new Scanner(new File(FNAME_input_connections)); | |
loc = new Scanner(new File(FNAME_input_locations)); | |
g.opener(); | |
} | |
//Read input | |
public void read() { | |
do { | |
String name = loc.next().trim().replace("", ""); | |
if (!"END".equals(name)) { | |
Point p = new Point(loc.nextInt(), loc.nextInt()); | |
Node N = new Node(name, p); | |
Nodes.add(N); | |
g.addNode(N); | |
mapper.put(name, N); | |
} | |
} while (loc.hasNext()); | |
do { | |
String edgedetail = conn.nextLine().trim().replace("", ""); | |
if (!"END".equals(edgedetail)) { | |
String[] temp = edgedetail.split(" "); | |
int num_of_edges = Integer.parseInt(temp[1]); | |
for (int i = 2; i < num_of_edges + 2; i++) { | |
Node A = g.getNode(temp[0]); | |
Node B = g.getNode(temp[i]); | |
g.connectNode(A, B); | |
connections = connections + A.p.x + " " + A.p.y + " " + B.p.x + " " + B.p.y + ","; | |
} | |
} | |
} while (conn.hasNext()); | |
} | |
public void search() { | |
Node root = g.getNode(start); | |
Node Destination = g.getNode(destination); | |
//set the start node for the search | |
g.setStartNode(root); | |
//set the node to be excluded as visited so that it will be excluded from the search path | |
if (!"nothing".equals(excluded)) { | |
Node excl = g.getNode(excluded); | |
excl.visited = true; | |
g.setNode(excluded, excl); | |
} | |
//call the search function | |
g.search(Destination); | |
} | |
//close the input files | |
public void close() throws IOException { | |
loc.close(); | |
conn.close(); | |
g.close(); | |
} | |
ArrayList getCities() { | |
return Nodes; | |
} | |
void search1() { | |
Node root = g.getNode(start); | |
Node Destination = g.getNode(destination); | |
//set the start node for the search | |
g.setStartNode(root); | |
//set the node to be excluded as visited so that it will be excluded from the search path | |
if (!"nothing".equals(excluded)) { | |
Node excl = g.getNode(excluded); | |
excl.visited = true; | |
g.setNode(excluded, excl); | |
} | |
//call the search function | |
g.search1(Destination); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment