@@ -26,7 +26,7 @@ public class ServerStrategySolveSearchProblem implements IServerStrategy{
*/
@ Override
public void serverStrategy (InputStream inFromClient , OutputStream outToClient ) {
System . out . println ( "Server Strategy Solve maze!" );
try {
//Creating the I/O streams
ObjectInputStream fromClient = new ObjectInputStream (inFromClient );
@@ -40,30 +40,33 @@ public void serverStrategy(InputStream inFromClient, OutputStream outToClient) {
//The Location of the file
String tempDirectoryPath = System .getProperty ("java.io.tmpdir" );
//The name of the file is a combination of the number of rows, number of columns, start Position,end Position, and index (we will add it later)
String filename =maze .getNumOfRows ()+"_" +maze .getNumOfColumns ()+"_" +maze .getStartPosition ().GetRowIndex ()+"_" +maze .getStartPosition ().GetColumnIndex ()+
"_" +maze .getGoalPosition ().GetRowIndex ()+"_" +maze .getGoalPosition ().GetColumnIndex ();
String filePath =tempDirectoryPath +filename ;
int counter =0 ;
//The counter of the files
int index =0 ;
//This function compresses the maze into a string (the maze itself... without the start/goal positions or the size of the maze)
String mazeString =maze .convertMazeToString ();
//Creating the file
File f ;
//the file
File file ;
boolean flag =true ;//If The solution exists
while (flag )
{
counter ++;
f = new File (filePath +"_" +counter +".txt" );
//Creating the file (with the new name - including the counter)
index ++;
file = new File (filePath +"_" +index +".txt" );
System .out .println (filePath +"_" +counter +".txt" );
//If there is such a file
if (f .exists () && !f .isDirectory ())
if (file .exists () && !file .isDirectory ())
{
//If this file is the solution for this maze
if (this .checkIfFileExist (f ,mazeString ))
if (this .checkIfFileExist (file ,mazeString ))
{
//Get the solution from the file and return it
solution = this .SolutionExists (f );
solution = this .SolutionExists (file );
break ;
}
}
@@ -79,10 +82,19 @@ public void serverStrategy(InputStream inFromClient, OutputStream outToClient) {
if (!flag )
{
//Solve the maze and put it's solution in the file
solution =solutionDoesNotExists (maze ,filePath +"_" +counter +".txt" );
solution =solutionDoesNotExists (maze ,filePath +"_" +index +".txt" );
}
//Getting the arrayList of the solution (without the neighbors - The neighbors list of every state is null. avoids stackOverFlow)
ArrayList <AState >list =solution .getSolutionPath ();
ArrayList <AState > mazeList =new ArrayList <>();
for (int i =0 ;i <list .size ();i ++)
{
mazeList .add (new MazeState ((MazeState )list .get (i )));
}
//Creating the modified solution
solution =new Solution (mazeList );
//Sending the solution of the maze via the output stream
toClient .writeObject (solution );
@@ -93,13 +105,19 @@ public void serverStrategy(InputStream inFromClient, OutputStream outToClient) {
e .printStackTrace ();
}
}
/**
* This function will extract the solution of the given maze from the right file and return it
* @param file - The file in which the solution is located
* @return - The solution
*/
private Solution SolutionExists (File file )
{
System .out .println ("Exist" );
//The list that will become the solution
ArrayList <AState > list =new ArrayList <>();
String str ="" ;
//Reading the solution from the file
try {
Scanner sc = new Scanner (file );
sc .nextLine ();
@@ -109,9 +127,12 @@ private Solution SolutionExists(File file)
} catch (FileNotFoundException e ) {
e .printStackTrace ();
}
int index =-1 ;
String row ="" ;
String col ="" ;
//Extracting the solution from the
while (index !=str .length ()-1 )
{
index =str .indexOf ("," );
@@ -125,10 +146,15 @@ private Solution SolutionExists(File file)
str =str .substring (index +1 );
}
//Creating and returning th solution
return new Solution (list );
}
/**
* This function will recieve a string that represents a number and will return the number in int
* @param str - The given string
* @return - The number that the string represents
*/
private int ConvertFromStringToInt (String str )
{
int num =0 ;
@@ -138,9 +164,17 @@ private int ConvertFromStringToInt(String str)
}
return num ;
}
/**
* This function will check if a current file is has the solution of the current maze
* @param file - The given file
* @param maze - The given maze represented in string
* @return - True if the file is the file that holds the solution to the maze
*/
private boolean checkIfFileExist (File file ,String maze )
{
String str ="" ;
//Read the first line (has the representation of the maze in string)
try {
Scanner sc = new Scanner (file );
str =sc .nextLine ();
@@ -149,12 +183,20 @@ private boolean checkIfFileExist(File file,String maze)
} catch (FileNotFoundException e ) {
e .printStackTrace ();
}
//Compare between the strings
return str .equals (maze );
}
/**
* This function will create a file with the solution of the current maze
* @param maze - The maze
* @param filePath - The path of the file to be created
* @return - The solution of the maze
*/
private Solution solutionDoesNotExists (Maze maze ,String filePath )
{
System . out . println ( "Dose not exist" );
//Creating the tools to solve the maze
BestFirstSearch bestFirstSearch = new BestFirstSearch ();
SearchableMaze searchableMaze = new SearchableMaze (maze );
@@ -171,14 +213,15 @@ private Solution solutionDoesNotExists(Maze maze,String filePath)
str2 =list .get (i ).toString ();
str +=str2 .substring (1 ,str2 .length ()-1 )+"," ;
}
System .out .println ("The solution " +str );
//Putting the solution in the file
PrintWriter writer = null ;
try {
writer = new PrintWriter (filePath , "UTF-8" );
//The representation of the maze in string - we will it inorder to compare between two mazes
writer .println (maze .convertMazeToString ());
writer .println (str );
writer .close ();