/// <summary> /// solving a private maze /// if the solution for this maze already exists it returns it /// </summary> /// <param name="name"></param> /// <param name="algorithm"></param> /// <returns></returns> public string SolveMaze(string name, int algorithm) { Maze m = privateMazeDict[name]; MazeAdapter <Position> maze = new MazeAdapter <Position>(m); Searcher <Position> searcher; Solution <Position> sol; if (!privateSolDict.Keys.Contains(name)) { if (algorithm == 0) { searcher = new BFS <Position>(); CostComparator <Position> compare = new CostComparator <Position>(); sol = searcher.Search(maze, compare); } else { searcher = new DFS <Position>(); sol = searcher.Search(maze); } privateSolDict.Add(name, sol); } else { sol = privateSolDict[name]; } string stringSolution = maze.ToSolution(sol); int numberOfNodesevaluated = sol.EvaluatedNodes; stringSolution += " "; stringSolution += numberOfNodesevaluated; return(stringSolution); }
/// <summary> /// Return the format string of JSON to the object. /// </summary> /// <returns>String of JSON.</returns> public string ToJson() { string strSolution = MazeAdapter.ToString(this.solution); NestedAdapterSolution nested = new NestedAdapterSolution(name, strSolution, solution.EvaluatedNodes); return(JsonConvert.SerializeObject(nested)); }
/// <summary> /// Solves the maze. /// </summary> /// <param name="name">The name.</param> /// <param name="typeAlgo">The type algo.</param> /// <returns></returns> public SolveInfo SolveMaze(string name, string typeAlgo) { Maze maze = singleplayerMazeList[name]; MazeAdapter mazeSearchable = new MazeAdapter(maze); // Isearchable var temp = new CompareStates <Position>(); Solution <Position> solution = null; int nodesEvaluated = 0; if (solutionsList.ContainsKey(maze)) { return(solutionsList[maze]); } // find solution of the maze: switch (typeAlgo) { case "0": BFS <Position> bfs = new BFS <Position>(temp); solution = bfs.Search(mazeSearchable); nodesEvaluated = bfs.GetNumberOfNodesEvaluated(); break; case "1": DFS <Position> dfs = new DFS <Position>(temp); solution = dfs.Search(mazeSearchable); nodesEvaluated = dfs.GetNumberOfNodesEvaluated(); break; } SolveInfo solveInfo = new SolveInfo(nodesEvaluated, solution); // add the solution to the solutions dictionary solutionsList.Add(maze, solveInfo); return(solveInfo); }
/// <summary> /// Executes the specified arguments. /// </summary> /// <param name="args">The arguments.</param> /// <param name="client">The client.</param> /// <returns></returns> public string Execute(string[] args, TcpClient client, string closeConnection, string keepOpen) { string name = args[0]; int algorithm = int.Parse(args[1]); int getNumberEvaluated = 0; string solution = null; NetworkStream stream = client.GetStream(); StreamReader reader = new StreamReader(stream); StreamWriter writer = new StreamWriter(stream); Solution <Position> s = null; if (algorithm == 0) { s = model.GetBFSSolution(name); solution = MazeAdapter.PrintSolution(s); getNumberEvaluated = s.GetNumberEvaluated(); } else { s = model.GetDFSSolution(name); solution = MazeAdapter.PrintSolution(s); getNumberEvaluated = s.GetNumberEvaluated(); } NestedSolve solve = new NestedSolve(name, solution, getNumberEvaluated); writer.WriteLine(JsonConvert.SerializeObject(solve)); writer.Flush(); return(closeConnection); }
/// <summary> /// Solves the maze DFS. /// </summary> /// <param name="name">The name.</param> /// <returns>Solution</returns> public Solution <Position> solveMazeDFS(string name) { modelData.mutexDfs.WaitOne(); Solution <Position> solution = null; // Check if the maze exist. if (modelData.Mazes.ContainsKey(name)) { ISearchable <Position> mazeObjectAdapter = new MazeAdapter(modelData.Mazes[name]); ISearcher <Position> DFS = new Dfs <Position>(); // Check if the solution exist. if (modelData.DfsSolutions.ContainsKey(name)) { solution = modelData.DfsSolutions[name]; } else { // Calculate the solution. solution = DFS.Search(mazeObjectAdapter); modelData.DfsSolutions.Add(name, solution); } } State <Position> .StatePool.Clear(); modelData.mutexDfs.ReleaseMutex(); return(solution); }
/* * The Execute method. * The method executes a specific command, * according to its implementation, using * relevant parameters, and the sender's details * (Client). */ public override void Execute(Player client, string parameters) { const int BFS_SYMBOL = 0; const int DFS_SYMBOL = 1; // parameters expected format: <gamename> <searchalgorithm> string[] args = parameters.Split(' '); string name = args[0]; Game game = model.GetGame(name); if (game == null) { Answer(client, "Error: No such game."); client.Connection.Close(); return; } int algorithm; if (!int.TryParse(args[1], out algorithm) || (algorithm != BFS_SYMBOL && algorithm != DFS_SYMBOL)) { Answer(client, "Error: Expected algorithm parameter is 0 or 1"); return; } Maze maze = game.Maze; Searcher <Position> searcher; if (algorithm == BFS_SYMBOL) { searcher = new BFS <Position>(); } else { searcher = new DFS <Position>(); } // Use a mazeAdpater since it implements ISearchable var mazeAdapter = new MazeAdapter(game.Maze); Solution <Position> solution = searcher.Search(mazeAdapter); var solutionToJasonBuilder = new JasonSolutionBuilder(solution); solutionToJasonBuilder["Name"] = maze.Name; solutionToJasonBuilder["NodesEvaluated"] = searcher.GetNumberOfNodesEvaluated(); // Get the solution as json. string response = solutionToJasonBuilder.ToJason().ToString(); // Send the response to the client. this.Answer(client, response); // Delete the game, and close the connection. this.model.DeleteGame(name); client.Connected = false; client.Connection.Close(); }
/// <summary> /// Gets the BFS solution. /// </summary> /// <param name="name">The name.</param> /// <returns></returns> Solution <Position> IModel.GetBFSSolution(string name) { bfsMutex.WaitOne(); if (solutionsBFS.ContainsKey(name)) { return(solutionsBFS[name]); } ISearchable <Position> mazeObjectAdapter = new MazeAdapter(mazes[name]); Solution <Position> solution = BFS.Search(mazeObjectAdapter); State <Position> .StatePool.ClearStatePool(); bfsMutex.ReleaseMutex(); return(solution); }
/// <summary> /// Gets the DFS solution. /// </summary> /// <param name="name">The name.</param> /// <returns></returns> Solution <Position> IModel.GetDFSSolution(string name) { dfsMutex.WaitOne(); if (solutionsDFS.ContainsKey(name)) { return(solutionsDFS[name]); } ISearchable <Position> mazeObjectAdapter = new MazeAdapter(mazes[name]); Solution <Position> solution = DFS.Search(mazeObjectAdapter); Console.WriteLine("DFS solution: "); State <Position> .StatePool.ClearStatePool(); dfsMutex.ReleaseMutex(); return(solution); }
/// <summary> /// Solves the maze. /// </summary> /// <param name="name">The name.</param> /// <param name="alg">The alg.</param> /// <returns>MazeSolution.</returns> public MazeSolution SolveMaze(string name, int alg) { Solution <Position> sol; int nodesEvaluated; if (this.mazeSolutionCache.ContainsKey(name)) { return(this.mazeSolutionCache[name]); } if (this.mazeCache.ContainsKey(name)) { Maze m = this.mazeCache[name]; if (alg == 0) { BFS <Position> bfs = new BFS <Position>(); ISearchable <Position> newMaze = new MazeAdapter(m); sol = bfs.Search(newMaze); nodesEvaluated = bfs.GetNumOfNodesEvaluated(); } else { DFS <Position> dfs = new DFS <Position>(); ISearchable <Position> newMaze = new MazeAdapter(m); sol = dfs.Search(newMaze); nodesEvaluated = dfs.GetNumOfNodesEvaluated(); } State <Position> .StatePool.ClearPool(); MazeSolution ms = new MazeSolution(sol, name, nodesEvaluated); ms.SolutionPath(); this.mazeSolutionCache.Add(name, ms); return(ms); } else { return(null); } }
public Solution SolveMaze(string name, int algorithm) { Solution solution = controller.GetSolution(name); if (!(solution == null)) { return(solution); } Maze maze = controller.GetMaze(name); //Searcher<Position> searcher; Searcher <string> searcher = null; if (algorithm == 0) { //BFS<Position> bfs = new BFS<Position>(); BFS <string> bfs = new BFS <string>(); bfs.set("BFS"); searcher = bfs; } else if (algorithm == 1) { //DFS } //MazeAdapter<Position> mazeAdapter = new MazeAdapter<Position>(maze); MazeAdapter <string> mazeAdapter = new MazeAdapter <string>(maze); //solution = searcher.search(mazeAdapter); solution = searcher.search(mazeAdapter); controller.AddSolution(name, solution); return(solution); }
/// <summary> /// Solves the maze BFS. /// </summary> /// <param name="name">The name.</param> /// <returns>Solution</returns> public Solution <Position> solveMazeBFS(string name) { Solution <Position> solution = null; // Check if the maze exist. if (modelData.Mazes.ContainsKey(name)) { ISearchable <Position> mazeObjectAdapter = new MazeAdapter(modelData.Mazes[name]); ISearcher <Position> BFS = new Bfs <Position>(); // Check if the solution exist. if (modelData.BfsSolutions.ContainsKey(name)) { solution = modelData.BfsSolutions[name]; } else { // Calculate the solution. solution = BFS.Search(mazeObjectAdapter); modelData.BfsSolutions.Add(name, solution); } } return(solution); }