///LoadMaze <summary> /// load saved maze from given path /// </summary> /// <param name="p">path</param> public void LoadMaze(string p) { byte[] mazes = File.ReadAllBytes(Path.GetFullPath(p)); extendedMaze MazeToSave = (extendedMaze)Deserialize(mazes); MazeDictionary.Add(MazeToSave.getName(), MazeToSave); m_Status = "Maze_Cell " + MazeToSave.getName() + " " + Convert.ToString(MazeToSave.m_CellSize); PrintEvent(); }
/// DisplayMaze<summary> /// Display The Maze by droew he on bord and set hem on the canvas /// </summary> /// <param name="maze">maze to display</param> public void DisplayMaze(extendedMaze maze) { int mazeCellSize = MazeCellsize[maze.getName()]; m_mazeBoard = new MazeBoard(maze._Maze.GetLength(0), maze._Maze.GetLength(1), mazeCellSize, maze); cnvs_main.Children.Add(m_mazeBoard); Canvas.SetLeft(m_mazeBoard, 0); Canvas.SetTop(m_mazeBoard, 0); cnvs_main.Focus(); m_mazeBoard.Focus(); }
///Delete <summary> /// delet maze from dictionay /// </summary> /// <param name="p">maze name to delete</param> public void Delete(string p) { MazeDictionary.Remove(p); extendedMaze toDel = new extendedMaze(); foreach (KeyValuePair <extendedMaze, Solution> item in SolutionDictionary) { if (item.Key.getName() == p) { toDel = item.Key; SolutionDictionary.Remove(toDel); break; } } }
///SaveFile<summary> /// seving given maze /// </summary> /// <param name="p">path</param> /// <param name="Name">save name</param> public void SaveFile(string p, string Name) { string[] linePart; string S; linePart = p.Split('\\'); S = linePart[linePart.Length - 1]; p = p.Remove(p.Length - S.Length, S.Length); if (MazeDictionary.ContainsKey(Name)) { extendedMaze MazeToSave = MazeDictionary[Name]; byte[] m_File = ObjetToByteArr(MazeToSave); File.WriteAllBytes(Path.GetFullPath(p) + S, m_File); } }
/// GetMaze<summary> /// maze geter form dictionary by name /// </summary> /// <param name="Name">name to get</param> /// <returns>maze</returns> public extendedMaze GetMaze(string Name) { extendedMaze Ans = new extendedMaze(); try { Ans = MazeDictionary[Name]; } catch (System.Exception ex) { m_Status = string.Format(ex.Message); PrintEvent(); } return(Ans); }
public void addMaze(extendedMaze Maze, string name) { Mutex_Add_Maze.WaitOne(); try { MazeDictionary.Add(name, Maze); } catch (System.Exception ex) { m_Status = ex.Message; PrintEvent(); Mutex_Add_Maze.ReleaseMutex(); Thread.CurrentThread.Abort(); } Mutex_Add_Maze.ReleaseMutex(); }
public void addSol(extendedMaze maze, Solution sol) { Mutex_Add_Sol.WaitOne(); try { SolutionDictionary.Add(maze, sol); } catch (System.Exception ex) { m_Status = ex.Message; PrintEvent(); Mutex_Add_Maze.ReleaseMutex(); Thread.CurrentThread.Abort(); } Mutex_Add_Sol.ReleaseMutex(); }
/// ThreadPoolGeneratMaze<summary> /// thrading to genrate maze by give algorithem /// </summary> /// <param name="param">'hight' 'width' 'name'</param> public void ThreadPoolGeneratMaze(object param) { string[] p = (string[])param; IStoppable NewMazeGenerator; if (Curr_Genrator == "Dfs")//// Change from Configure { NewMazeGenerator = new DfsMazeGenerator(); } else { NewMazeGenerator = new RandomMazeGenerator(); } Workers.Add(NewMazeGenerator); maze m_Maze = new maze(); m_Status = string.Format("Maze {0} started Genarate...", p[2]); PrintEvent(); m_Maze = (maze)(((MazeGenerator)NewMazeGenerator).generatMaze(Convert.ToInt32(p[0]), Convert.ToInt32(p[1]))); if (m_Maze != null) { extendedMaze ex_Maze = new extendedMaze(m_Maze); ex_Maze.setName(p[2]); if (p[3] != "0") { ex_Maze.SaveGuiParams(p[3]); } addMaze(ex_Maze, p[2]); m_Status = string.Format("Maze {0} is ready...", p[2]); PrintEvent(); } }
/// DisplayMaze<summary> /// send to outout given maze to display /// </summary> /// <param name="maze">maze name to display</param> public void DisplayMaze(extendedMaze maze) { string ans = Environment.NewLine + (string)maze.print(); Output(ans); }
///move <summary> ///check fo vialid move, from given arguments /// </summary> /// <param name="parameters">maze name (1), poit postion (x,y)(1,2) , up/down/left/right (3)</param> public void move(string[] parameters) { AState Agoal; string[] goal = null; extendedMaze maze = null; int x = 0; int y = 0; string m_command = ""; Exception Missing = new Exception("Missing or incorrect Argument Exception"); try { maze = MazeDictionary[parameters[0]]; x = Convert.ToInt32(parameters[1]); y = Convert.ToInt32(parameters[2]); m_command = parameters[3]; Agoal = maze.GetGoalState(); goal = Agoal.GetState().Split(','); // if (x == Convert.ToInt32(goal[0]) & y == Convert.ToInt32(goal[1])) // m_command = "win"; } catch (System.Exception ex) { m_Status = ex.Message; PrintEvent(); } if (x == Convert.ToInt32(goal[0]) & y == Convert.ToInt32(goal[1])) { NotifyPropertyChanged("win"); } switch (m_command) { case "up": if (y > 1) { if (x == Convert.ToInt32(goal[0]) & (y - 1) == Convert.ToInt32(goal[1])) { NotifyPropertyChanged("win"); NotifyPropertyChanged("left"); } else if (maze._Maze[x, y - 1] != 1) { NotifyPropertyChanged("left"); } } break; case "down": if (x == Convert.ToInt32(goal[0]) & (y + 1) == Convert.ToInt32(goal[1])) { NotifyPropertyChanged("win"); NotifyPropertyChanged("right"); } else if (y < maze._Maze.GetLength(1) & maze._Maze[x, y + 1] != 1) { NotifyPropertyChanged("right"); } break; case "left": if (x - 1 == Convert.ToInt32(goal[0]) & (y) == Convert.ToInt32(goal[1])) { NotifyPropertyChanged("win"); NotifyPropertyChanged("up"); } else if (x > 1 & maze._Maze[x - 1, y] != 1) { NotifyPropertyChanged("up"); } break; case "right": if (x + 1 == Convert.ToInt32(goal[0]) & (y) == Convert.ToInt32(goal[1])) { NotifyPropertyChanged("win"); NotifyPropertyChanged("down"); } else if (x < maze._Maze.GetLength(0) & maze._Maze[x + 1, y] != 1) { NotifyPropertyChanged("down"); } break; } }
///ThreadPoolSolveMaze <summary> /// thrding to solve maze /// </summary> /// <param name="p">name to solve</param> public void ThreadPoolSolveMaze(object p) { bool flag = false; bool exict = false; string Name = (string)p; extendedMaze m_Maze = new extendedMaze(); m_Status = string.Format("started Solving Maze {0}...", p); PrintEvent(); try { m_Maze = MazeDictionary[Name]; if (SolutionDictionary.ContainsKey(m_Maze)) { m_Status = string.Format("Solution For {0} Allready Exict In Dictionary", p); PrintEvent(); exict = true; } } catch (System.Exception ex) { m_Status = ex.Message; PrintEvent(); } if (!exict) { IStoppable Alg; if (Curr_Alg == "A*") //Setting Cunfigure { //***************** if (Curr_Heuristic == "MazeAirDistance") //3 Diffrent Algorithm { Alg = new Astar(new MazeAirDistance()); //Astar->MazeAirDistance } else //Astar->MazeManhattanDistance { Alg = new Astar(new MazeManhattanDistance()); //UCS } } else { Alg = new UCS(); } Workers.Add(Alg); try { foreach_Mutex.WaitOne(); foreach (KeyValuePair <extendedMaze, Solution> item in SolutionDictionary) { if (item.Key.getGrid() == m_Maze.getGrid()) // Ovaride equals to chack if { //a sol is Allready Exict m_Status = string.Format("Solution for {0} Allready Exict In {1}", m_Maze.getName(), item.Key.getName()); // if he does copy it PrintEvent(); Solution sol = SolutionDictionary[item.Key]; if (sol != null && sol.GetSolutionPath().Count > 0) { addSol(m_Maze, sol); } flag = true; break; } } foreach_Mutex.ReleaseMutex(); if (!flag) { ISearchable Searchable_Maze; if (!IsDiagonal) //Diagonal Setting { Searchable_Maze = new SearchableMaze(m_Maze, false); //**************** } else //False->Without { Searchable_Maze = new SearchableMaze(m_Maze, true); //True->With } Solution sol = (Solution)(((ASearchingAlgorithm)Alg).Solve(Searchable_Maze)); ArrayList list = sol.GetSolutionPath(); ArrayList states = new ArrayList(); foreach (AState item in list) { states.Add(item.GetState()); } m_Maze.m_Solution = m_Maze.printAfterSolution(states); m_Status = string.Format("{0} result Finished...", p); PrintEvent(); if (sol != null && sol.GetSolutionPath().Count > 0) { addSol(m_Maze, sol); } } } catch { m_Status = "Cant Find Maze"; PrintEvent(); } } }