private static void testCompression() { MyMaze3dGenerator smg = new MyMaze3dGenerator(); Maze maze = smg.generate(9, 6, 5); byte[] byets = ((Maze3d)maze).toByteArray(); using (FileStream fileOutStream = new FileStream("1.maze", FileMode.Create)) { using (MyCompressorStream outStream = new MyCompressorStream(fileOutStream, MyCompressorStream.Compress)) { outStream.Write(byets, 0, byets.Length); outStream.Flush(); } } byte[] mazeBytes; using (FileStream fileInStream = new FileStream("1.maze", FileMode.Open)) { using (MyCompressorStream inStream = new MyCompressorStream(fileInStream, MyCompressorStream.Decompress)) { mazeBytes = new byte[byets.Length]; inStream.Read(mazeBytes, 0, mazeBytes.Length); } } Maze3d loadedMaze = new Maze3d(mazeBytes); Console.WriteLine(loadedMaze.equals(maze)); Console.WriteLine("*******************************"); Console.WriteLine("Press any key to continue"); Console.WriteLine(); Console.ReadKey(); }
private static void testCompressor() { int[] size3D = { 3, 6, 6 }; // (z,y,x) IMazeGenerator mazeGenerator3d = new MyMaze3dGenerator(); Maze3d maze = (Maze3d)mazeGenerator3d.generate(size3D); maze.print(); Console.ReadKey(); byte[] decomp = maze.toByteArray(); for (int i = 0; i < decomp.Length; i++) { Console.Write(decomp[i] + " "); } ICompressor compressor = new MyMaze3DCompressor(); Console.WriteLine("\ncompression : "); byte[] comp = compressor.compress(decomp); for (int i = 0; i < comp.Length; i++) { Console.Write(comp[i] + " "); } decomp = compressor.decompress(comp); Console.WriteLine("\ndecompression : "); for (int i = 0; i < decomp.Length; i++) { Console.Write(decomp[i] + " "); } }
/// <summary> /// Generates a 3d maze and adds it to the dictionary. /// </summary> /// <param name="size">array[3] : [0]columns [1] rows [2] levels - as maze dimensions </param> /// <param name="mazeName">the maze name - maze ID</param> private void threadGenerateMaze(int[] size, string mazeName) { IMazeGenerator mazeGenerator3d = new MyMaze3dGenerator(); Maze3d maze = (Maze3d)mazeGenerator3d.generate(size); addMaze(mazeName, maze); m_controller.Output("maze " + mazeName + " is ready!\n"); }
/// <summary> /// generate the maze3d in new thread /// </summary> /// <param name="maze_name">name of the maze</param> /// <param name="x">dim x</param> /// <param name="y">dim y</param> /// <param name="z">dim z</param> private void GenerateMaze3dInNewThread(string maze_name, int x, int y, int z) { MyMaze3dGenerator maze = new MyMaze3dGenerator(); Maze new_maze = maze.generate(x, y, z); IfSolutionExist(maze_name); // if the solution exist with identical name - remove it. IfNameMazeExist(maze_name); // if the name maze exit - remove it. m_DicOfMazes.Add(maze_name, new_maze); // add new maze to dictionary of mazes }
public void TestRunAlotOfTimes() { var mg = new MyMaze3dGenerator(); for (int i = 0; i < 100; i++) { mg.generate(2, 2, 2); } }
public void genarateMaze(string mazeName, int x, int y, int z) { new Thread(() => { MyMaze3dGenerator mg = new MyMaze3dGenerator(); Maze3d maze = (Maze3d)mg.generate(x, y, z); AddMaze(mazeName, maze); controller.output("Maze " + mazeName + " genarated!"); }).Start(); }
/// <summary> /// generate maze and calls print for both BFS and DFS searching algorithms /// </summary> private static void testSearchAlgorithms() { int[] size3D = { 4, 6, 6 }; // (z,y,x) IMazeGenerator mazeGenerator3d = new MyMaze3dGenerator(); ISearchable maze = new SearchableMaze3D(mazeGenerator3d.generate(size3D)); print("BFS", new BFS(), maze); Console.WriteLine("\nPlease enter any key to continue\n"); Console.ReadKey(); print("DFS", new DFS(), maze); }
public void TestBadArguments() { try { var mg = new MyMaze3dGenerator(); Maze3d maze = (Maze3d)mg.generate(2, -10, 2); Assert.Fail("Test Bad Arguments Faild"); } catch (OverflowException) { } }
private static void TestMyCompressorStream() { IMazeGenerator mg = new MyMaze3dGenerator(); Maze maze = mg.generate(10, 14, 5); Console.WriteLine("Test - My Stream Compressor"); string filePath = @"compressedFile.txt"; using (FileStream fileOutStream = new FileStream(filePath, FileMode.Create)) { using (MyCompressorStream c = new MyCompressorStream(fileOutStream, 1)) { byte[] byteArray = ((Maze3d)maze).toByteArray(); int length = byteArray.Length; Console.WriteLine(); for (int i = 0; i < byteArray.Length; i++) { Console.Write(byteArray[i]); } Console.WriteLine(); Console.WriteLine(); c.Write(byteArray, 0, length); } } Console.WriteLine(); byte[] mazeBytes; using (FileStream fileInStream = new FileStream(filePath, FileMode.Open)) { using (MyCompressorStream inStream = new MyCompressorStream(fileInStream, 2)) { mazeBytes = new byte[(((Maze3d)maze).toByteArray()).Length]; int length = mazeBytes.Length; inStream.Read(mazeBytes, 0, length); Console.WriteLine(); Console.WriteLine("*** Data Bytes after reading from the file ***"); for (int i = 0; i < mazeBytes.Length; i++) { Console.Write(mazeBytes[i]); } Console.WriteLine(); } } Console.WriteLine(); Console.WriteLine("*******Before writing to the file*******"); maze.print(); Console.WriteLine("*******After reading from the file*******"); Maze3d mazeAfterReading = new Maze3d(mazeBytes); mazeAfterReading.print(); }
private static void testMeze3dGenerator(MyMaze3dGenerator mg) { ArrayList s = new ArrayList(); s.Add(20); s.Add(20); s.Add(4); Console.WriteLine(mg.MeasureAlgorithmTime(s)); AMaze maze = mg.generate(s); Position start = maze.getStartPosition(); start.print(); maze.getGoalPosition().print(); maze.Print(); byte[] dar = (maze as Maze3d).toByteArray(); Maze3d hara = new Maze3d(dar); ICompressor car = new MyMaze3DCompressor(); byte[] hara1 = car.compress(dar); byte[] hara2 = car.decompress(hara1); Maze3d hara3 = new Maze3d(hara2); hara3.Print(); /*using (FileStream fileOutStream = new FileStream("1.maze", FileMode.Create)) * { * using (Stream outStream = new MyCompressorStream(fileOutStream, 1)) * { * outStream.Write((maze as Maze3d).toByteArray(),0,1); * outStream.Flush(); * } * } * byte[] mazeBytes; * using (FileStream fileInStream = new FileStream("1.maze", FileMode.Open)) * { * using (Stream inStream = new MyCompressorStream(fileInStream,1)) * { * mazeBytes = new byte[(maze as Maze3d).toByteArray().Count()]; * input.read(b); * } * } * Maze3d loadedMaze = new Maze3d(mazeBytes); * System.out.println(loaded.equals(maze));*/ Console.ReadKey(); }
public void TestIsASolution() { //arrange var mg = new MyMaze3dGenerator(); Maze3d maze = (Maze3d)mg.generate(7, 7, 4); BFS bfs = new BFS(); SearchableMaze3d sMaze = new SearchableMaze3d(maze); Solution sol = bfs.Solve(sMaze); //act bfs.Solve(sMaze); //assert Assert.IsNotNull(sol); }
/// <summary> /// Generates 3D maze in a new thread in the thread pool /// </summary> /// <param name="mazeName"></param> /// <param name="width"></param> /// <param name="length"></param> /// <param name="layers"></param> private void Generate3dMazeInThreadPool(string mazeName, int width, int length, int layers) { ThreadPool.SetMaxThreads(m_numberOfThreadsInThreadPool, m_numberOfThreadsInThreadPool); ThreadPool.QueueUserWorkItem ( new WaitCallback((state) => { MyMaze3dGenerator mazeGenerator = new MyMaze3dGenerator(); //** setStoppableObjects(mazeGenerator); Maze3d maze = (Maze3d)mazeGenerator.generate(length, width, layers); AddToGeneratedMazesDictionary(mazeName, maze); ModelChanged("maze " + mazeName + " is ready"); }) ); }
/// <summary> /// run the generate 3d maze in thread pool /// </summary> /// <param name="maze_name">name of maze</param> /// <param name="x">dim x</param> /// <param name="y">dim y</param> /// <param name="z">dim z</param> private void RunInThreadPoolOfGenerate(string name, int x, int y, int z) { ThreadPool.QueueUserWorkItem ( new WaitCallback((state) => { MyMaze3dGenerator maze = new MyMaze3dGenerator(); Maze new_maze = maze.generate(x, y, z); if (new_maze != null) { m_DicOfMazes.Add(name, new_maze); // add new maze to dictionary of mazes ModelChanged("MazeIsReady", name); } }) ); }
private void threadGenerateMaze(int[] mazeDimentions, string mazeName, int cellSize) { IMazeGenerator maze3dGenerator = new MyMaze3dGenerator(); m_stoppingList.Add(maze3dGenerator); Maze3d currentMaze = (Maze3d)maze3dGenerator.generate(mazeDimentions); WinMaze winMaze = new WinMaze(currentMaze, mazeName, cellSize); m_winMazesDictionary[mazeName] = winMaze; m_currentWinMaze = winMaze; m_stoppingList.Remove(maze3dGenerator); m_solutionsDictionary.Remove("maze"); add3dMaze("maze", currentMaze); isSolutionExists = false; saveMazeDictionary(); }
public void GenerateMaze(string mazename, int x, int y, int z) { if ((m_controller as MyController).mazes.ContainsKey(mazename)) { (m_controller as MyController).M_view.Output("maze name already exists"); return; } ArrayList list = new ArrayList(); list.Add(x); list.Add(y); list.Add(z); MyMaze3dGenerator mg = new MyMaze3dGenerator(); AMaze maze = mg.generate(list); (m_controller as MyController).mazes.Add(mazename, maze); (m_controller as MyController).M_view.Output("maze " + mazename + " is ready"); }
private static void TestCheck() { IMazeGenerator mg = new MyMaze3dGenerator(); Maze maze = mg.generate(8, 8, 5); MyMaze3DCompressor comp = new MyMaze3DCompressor(); byte[] byteArray = ((Maze3d)maze).toByteArray(); Console.WriteLine("Before"); for (int i = 0; i < byteArray.Length; i++) { Console.Write(byteArray[i]); } Console.WriteLine(); Console.WriteLine("After"); byte[] byteArrayAfter = comp.compress(byteArray); byte[] byteArrayAfterDec = comp.decompress(byteArrayAfter); for (int i = 0; i < byteArrayAfterDec.Length; i++) { Console.Write(byteArrayAfterDec[i]); } Console.WriteLine(); }
private static void testSearchAlgorithms() { ASearchingAlgorithm BFS = new BreadthFirstSearch(); ASearchingAlgorithm DFS = new DepthFirstSearch(); AMazeGenerator g = new MyMaze3dGenerator(); Maze maze = g.generate(5, 5, 4); ISearchable search = new SearchableMaze3d(maze); Console.WriteLine("****************Part 2 - Tests***************"); Console.WriteLine("The Start State: {0}", maze.getStartPosition()); Console.WriteLine("The Goal State: {0}", maze.getGoalPosition()); Console.WriteLine(); //BFS maze.print(); Console.WriteLine("***************The Solve by BFS:***************"); Solution s_bfs = BFS.Solve(search); s_bfs.printSolution(); Console.WriteLine(); //DFS // maze.print(); Console.WriteLine("press any key to continue to DFS..."); Console.ReadLine(); Console.WriteLine("***************The Solve by DFS:***************"); Solution s_dfs = DFS.Solve(search); s_dfs.printSolution(); Console.WriteLine(); Console.WriteLine("****************Results***************"); Console.WriteLine("The Numbers of Nodes in BFS solution: {0} Nodes", s_bfs.numOfNodesInSolution()); Console.WriteLine("The Numbers of Nodes in DFS solution: {0} Nodes", s_dfs.numOfNodesInSolution()); Console.WriteLine("The Numbers of Generated Nodes in BFS: {0} Nodes", BFS.GetNumberOfGeneratedNodes()); Console.WriteLine("The Numbers of Generated Nodes in DFS: {0} Nodes", DFS.GetNumberOfGeneratedNodes()); Console.WriteLine("Time that take to find solution in BFS: {0} Milliseconds", BFS.GetSolvingTimeMiliseconds()); Console.WriteLine("Time that take to find solution in DFS: {0} Milliseconds", DFS.GetSolvingTimeMiliseconds()); }
private static void testMyCompressorStream() { Console.WriteLine("******* testMyCompressorStream *******\n"); int[] size3D = { 3, 6, 7 }; // (z,y,x) IMazeGenerator mazeGenerator3d = new MyMaze3dGenerator(); Maze3d maze = (Maze3d)mazeGenerator3d.generate(size3D); // save the maze to a file – compressed using (FileStream fileOutStream = new FileStream(@"D:\1.maze.txt", FileMode.Create)) { using (Stream outStream = new MyCompressorStream(fileOutStream)) { outStream.Write(maze.toByteArray(), 0, maze.toByteArray().Length); outStream.Flush(); } } byte[] mazeBytes; using (FileStream fileInStream = new FileStream(@"D:\1.maze.txt", FileMode.Open)) { using (Stream inStream = new MyCompressorStream(fileInStream)) { mazeBytes = new byte[maze.toByteArray().Length]; inStream.Read(mazeBytes, 0, mazeBytes.Length); } } Maze3d loadedMaze = new Maze3d(mazeBytes); Console.WriteLine("The original maze : "); maze.print(); maze.getStartPosition().print(); maze.getGoalPosition().print(); Console.WriteLine("The decompress maze : "); loadedMaze.print(); loadedMaze.getStartPosition().print(); loadedMaze.getGoalPosition().print(); Console.WriteLine(loadedMaze.Equals(maze)); }
private static void testSearchAlgorithms() { ArrayList s = new ArrayList(); s.Add(5); s.Add(5); s.Add(4); MyMaze3dGenerator mg = new MyMaze3dGenerator(); AMaze maze = mg.generate(s); maze.Print(); Console.WriteLine("DFS solution path:"); SearchableMaze3d sm = new SearchableMaze3d((maze as Maze3d)); DepthFirstSearch ds = new DepthFirstSearch(); Solution dssol = ds.Solve(sm); dssol.PrintSolution(); Console.WriteLine("-------------------------------------------------------"); Console.WriteLine("for BFS solution press any key"); Console.ReadKey(); Console.WriteLine("BFS solution path:"); BreadthFirstSearch bs = new BreadthFirstSearch(); Solution bssol = bs.Solve(sm); bssol.PrintSolution(); Console.WriteLine("-------------------------------------------------------"); Console.WriteLine("for further information about DFS solution press any key"); Console.ReadKey(); Console.WriteLine("num of nodes developed in DFS: " + ds.getNumberOfNodes()); Console.WriteLine("time it took to find solution(ms) in DFS: " + ds.GetSolvingTime()); Console.WriteLine(); Console.WriteLine("for further information about BFS solution press any key"); Console.ReadKey(); Console.WriteLine(); Console.WriteLine("num of nodes developed in BFS: " + bs.getNumberOfNodes()); Console.WriteLine("time it took to find solution(ms) in BFS: " + bs.GetSolvingTime()); }
/// <summary> /// Part 2 /// testing the search algorithms /// </summary> private static void testSearchAlgorithms() { MyMaze3dGenerator smg = new MyMaze3dGenerator(); Maze maze3d = smg.generate(20, 20, 10); ISearchable maze = new SearchableMaze3d((Maze3d)maze3d); AState startState = maze.GetStartState(); Console.WriteLine("**************************************"); Console.WriteLine("*******TESTING SEARCH ALGORITHMS******"); Console.WriteLine("**************************************"); Console.WriteLine("maze to solve:"); Console.WriteLine(); maze3d.printMapKeys(); maze3d.print(); Console.WriteLine(); Console.WriteLine("Press any key to continue"); Console.ReadKey(); Console.WriteLine(); Console.WriteLine("TEST 1: testing Breadth First Search algorithm"); Console.WriteLine("**************************************"); Console.WriteLine(); ASearchingAlgorithm bfs = new BreadthFirstSearch(); Solution solution = bfs.Solve(maze); if (solution.IsSolutionExists()) { Console.WriteLine("Solution found:"); Console.WriteLine("***************"); Console.WriteLine(); Console.WriteLine("Path:"); foreach (AState state in solution.GetSolutionPath()) { state.PrintState(); } } else { Console.WriteLine("No Solution found!"); } Console.WriteLine(""); Console.WriteLine(string.Format("Moves to goals state: {0}", solution.GetSolutionSteps())); Console.WriteLine(string.Format("Nodes generated: {0}", bfs.GetNumberOfGeneratedNodes())); Console.WriteLine(string.Format("Solving time (miliseconds): {0}", bfs.GetSolvingTimeMiliseconds())); Console.WriteLine("Press any key to continue"); Console.WriteLine(); Console.ReadKey(); Console.WriteLine("TEST 2: testing Depth First Search algorithm"); Console.WriteLine("**************************************"); Console.WriteLine(); ASearchingAlgorithm dfs = new DepthFirstSearch(); Solution solution2 = dfs.Solve(maze); if (solution2.IsSolutionExists()) { Console.WriteLine("Solution found:"); Console.WriteLine("***************"); Console.WriteLine(); Console.WriteLine("Path:"); foreach (AState state in solution2.GetSolutionPath()) { state.PrintState(); } } else { Console.WriteLine("No Solution found!"); } Console.WriteLine(""); Console.WriteLine(string.Format("Moves to goals state: {0}", solution2.GetSolutionSteps())); Console.WriteLine(string.Format("Nodes generated: {0}", dfs.GetNumberOfGeneratedNodes())); Console.WriteLine(string.Format("Solving time (miliseconds): {0}", dfs.GetSolvingTimeMiliseconds())); Console.ReadLine(); }