/// <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> /// 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); }
/* * 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> /// 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); } }