Esempio n. 1
0
        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();
        }
Esempio n. 2
0
        /// <summary>
        /// Save Maze - Creates a compression of the maze and
        /// saves the file on the disk with a given file path
        /// </summary>
        /// <param name="mazeName">Maze name</param>
        /// <param name="path">File Name</param>
        /// <returns>True if successful saving the maze</returns>
        public bool saveMaze(string mazeName, string filePath)
        {
            //MessageBox.Show("save: " + mazeName);
            WinMaze winMaze     = m_currentWinMaze;
            Maze3d  currentMaze = winMaze.getMaze();

            add3dMaze(mazeName, currentMaze);
            if (isSolutionExists)
            {
                m_solutionsDictionary[mazeName] = mCurrentSolution;
            }

            if (currentMaze == null)
            {
                return(false);
            }
            using (FileStream fileOutStream = new FileStream(filePath, FileMode.Create))
            {
                using (Stream outStream = new MyCompressorStream(fileOutStream))
                {
                    outStream.Write(currentMaze.toByteArray(), 0, currentMaze.toByteArray().Length);
                    outStream.Flush();
                }
            }
            return(true);
        }
Esempio n. 3
0
        /// <summary>
        /// Load Maze - Loads a maze to the memory with a given file path
        /// from the user.
        /// </summary>
        /// <param name="path">Directory path</param>
        /// <param name="mazeName">Name of the maze</param>
        public void loadMaze(string path, string mazeName)
        {
            int numberOfBytes;

            byte[]      compressionArray = new byte[100];
            List <byte> compressedList   = new List <byte>();

            using (FileStream fileInStream = new FileStream(path, FileMode.Open))
            {
                using (Stream compressor = new MyCompressorStream(fileInStream))
                {
                    while ((numberOfBytes = compressor.Read(compressionArray, 0, 100)) != 0)
                    {
                        for (int i = 0; i < numberOfBytes; i++)
                        {
                            compressedList.Add(compressionArray[i]);
                        }
                    }
                }
            }
            Maze3d loadedMaze = new Maze3d(compressedList.ToArray());

            add3dMaze(mazeName, loadedMaze);
            WinMaze winMaze = new WinMaze(loadedMaze, "maze", 50);

            //if (!winMaze.isSolutionExists())
            //    isSolutionExists = false;
            m_currentWinMaze = winMaze;
            m_winMazesDictionary.Remove("maze");
            m_winMazesDictionary["maze"] = winMaze;
            m_instructions.Clear();
            m_instructions.Add("display");
            m_instructions.Add("maze");
        }
Esempio n. 4
0
        /// <summary>
        /// load the maze from the disk
        /// </summary>
        /// <param name="filePath">path of the file</param>
        /// <param name="maze_name">name of the maze</param>
        public void LoadMazeFromDisk(string filePath, string maze_name)
        {
            string fullPath = filePath + maze_name;

            byte[]      byteFromFile     = new byte[100];
            List <byte> byteMazeFromFile = new List <byte>();

            using (Stream stream = new FileStream(fullPath, FileMode.Open))
            {
                using (Stream file_s = new MyCompressorStream(stream, 2))
                {
                    MyCompressorStream file_Str = file_s as MyCompressorStream;
                    int read_f = 0;
                    while ((read_f = file_Str.Read(byteFromFile, 0, 100)) != 0)
                    {
                        if (read_f < 100)
                        {
                            addToList(byteMazeFromFile, byteFromFile, read_f);
                        }
                        else
                        {
                            byteMazeFromFile.AddRange(byteFromFile);
                        }
                        byteFromFile = new byte[100];
                    }
                    byte[] b = byteMazeFromFile.ToArray();
                    m_DicOfMazes[maze_name] = new Maze3d(b);
                    file_Str.Flush();
                }
            }
        }
Esempio n. 5
0
        public void Load(string path, string mazename)
        {
            if ((m_controller as MyController).mazes.ContainsKey(mazename))
            {
                (m_controller as MyController).M_view.Output("maze name already exists");
                return;
            }
            if (!File.Exists(path))
            {
                (m_controller as MyController).M_view.Output("File does not exsits");
                return;
            }
            byte[] todecompress = File.ReadAllBytes(path);
            byte[] buffer       = new byte[todecompress[0] * todecompress[1] * todecompress[2] + 3];

            using (FileStream fileInStream = new FileStream(path, FileMode.Open))
            {
                using (Stream inStream = new MyCompressorStream(fileInStream, 2))
                {
                    byte[] mazeBytes = new byte[inStream.Read(buffer, 0, 0)];
                }
            }
            AMaze maze = new Maze3d(buffer);

            (m_controller as MyController).mazes.Add(mazename, maze);
        }
Esempio n. 6
0
        /// <summary>
        /// Load Maze - Loads a maze to the memory with a given file path
        /// from the user.
        /// </summary>
        /// <param name="path">Directory path</param>
        /// <param name="mazeName">Name of the maze</param>
        public void loadMaze(string path, string mazeName)
        {
            mazeName        = mazeName.Substring(0, mazeName.Length - 5);
            currentMazeName = mazeName;
            int numberOfBytes;

            byte[]      compressionArray = new byte[100];
            List <byte> compressedList   = new List <byte>();

            using (FileStream fileInStream = new FileStream(path, FileMode.Open))
            {
                using (Stream compressor = new MyCompressorStream(fileInStream))
                {
                    while ((numberOfBytes = compressor.Read(compressionArray, 0, 100)) != 0)
                    {
                        for (int i = 0; i < numberOfBytes; i++)
                        {
                            compressedList.Add(compressionArray[i]);
                        }
                    }
                }
            }
            Maze3d loadedMaze = new Maze3d(compressedList.ToArray());

            add3dMaze(mazeName, loadedMaze);
            WinMaze winMaze = new WinMaze(loadedMaze, "maze", 50);

            m_currentWinMaze = winMaze;
            m_winMazesDictionary.Remove("maze");
            m_winMazesDictionary["maze"] = winMaze;
            printMaze(winMaze, winMaze.PosZ);
        }
Esempio n. 7
0
        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();
        }
Esempio n. 8
0
        /// <summary>
        /// save the maze to disk
        /// </summary>
        /// <param name="maze_name">name of the file</param>
        /// <param name="filePath">path of the file</param>
        public void SaveMazeToDisk(string maze_name, string filePath)
        {
            byte[] mazeToByte;
            string fullPath = filePath + maze_name;

            using (Stream stream = new FileStream(fullPath, FileMode.Create))
            {
                using (Stream file_s = new MyCompressorStream(stream, 1))
                {
                    MyCompressorStream file_Str = file_s as MyCompressorStream;
                    mazeToByte = ((Maze3d)(m_DicOfMazes[maze_name])).toByteArray();
                    file_Str.Write(mazeToByte, 0, mazeToByte.Length);
                    file_Str.Flush();
                }
            }
        }
Esempio n. 9
0
        /// <summary>
        /// Compress the maze 'mazeName' and save it into a file with a given path
        /// </summary>
        /// <param name="mazeName">the name of the maze to save</param>
        /// <param name="path">file path</param>
        /// <returns>true - for success, false for failure</returns>
        public bool saveMaze(string mazeName, string path)
        {
            Maze3d maze = retrieveMaze(mazeName);

            if (null == maze)
            {
                return(false);
            }
            using (FileStream fileOutStream = new FileStream(path, FileMode.Create))
            {
                using (Stream outStream = new MyCompressorStream(fileOutStream))
                {
                    outStream.Write(maze.toByteArray(), 0, maze.toByteArray().Length);
                    outStream.Flush();
                }
            }
            return(true);
        }
Esempio n. 10
0
 public void SaveToDisk(string mazeName, string path)
 {
     if (IsMazeExists(mazeName))
     {
         using (Stream compressed = new FileStream(@path, FileMode.Create))
         {
             using (Stream mycompressor = new MyCompressorStream(compressed, MyCompressorStream.Compress))
             {
                 byte[] mazebytes = m_mazes[mazeName].toByteArray();
                 mycompressor.Write(mazebytes, 0, mazebytes.Length);
             }
         }
     }
     else
     {
         throw new Exception("maze name " + mazeName + " doesn't exist!");
     }
 }
Esempio n. 11
0
 public void LoadFromDisk(string mazeName, string path)
 {
     using (Stream compressed = new FileStream(path, FileMode.Open))
     {
         using (Stream mydecompressor = new MyCompressorStream(compressed, MyCompressorStream.Decompress))
         {
             byte[] size = new byte[3];
             mydecompressor.Read(size, 0, size.Length);
             byte[] mazebytes = new byte[3 + (size[0] * size[1] * size[2])];
             for (int i = 0; i < 3; mazebytes[i] = size[i], i++)
             {
                 ;
             }
             mydecompressor.Read(mazebytes, 3, mazebytes.Length - 3);
             Maze3d newMaze = new Maze3d(mazebytes);
             m_mazes.Add(mazeName, newMaze);
         }
     }
 }
Esempio n. 12
0
 private static void DecompressFile(string compressedFilePath, string decompressedFilePath)
 {
     // decompress bart_compressed.txt => bart_decompressed.txt
     using (FileStream compressesFileStream = new FileStream(compressedFilePath, FileMode.Open))
     {
         using (FileStream decompressedFileStream = new FileStream(decompressedFilePath, FileMode.Create))
         {
             using (MyCompressorStream myCompressorStream = new MyCompressorStream(compressesFileStream))
             {
                 byte[] data = new byte[100];
                 int    r    = 0;
                 while ((r = myCompressorStream.Read(data, 0, 100)) != 0)
                 {
                     decompressedFileStream.Write(data, 0, data.Length);
                 }
             }
         }
     }
 }
Esempio n. 13
0
 private static void CompressFile(string originalFilePath, string compressedFilePath)
 {
     //Compress bart.txt => bart_compressed.txt
     using (FileStream originalFileStream = new FileStream(originalFilePath, FileMode.Open))
     {
         using (FileStream compressedFileStream = new FileStream(compressedFilePath, FileMode.Create))
         {
             using (MyCompressorStream myCompressorStream = new MyCompressorStream(compressedFileStream))
             {
                 //Read original bart
                 byte[] buffer = new byte[50];
                 int    r      = 0;
                 while ((r = originalFileStream.Read(buffer, 0, buffer.Length)) != 0)
                 {
                     myCompressorStream.Write(buffer, 0, r);
                 }
             }
         }
     }
 }
Esempio n. 14
0
        /// <summary>
        /// Loads a compressed maze from the given path ,decompress it and adds it to the dictionary
        /// also - if theres a previous existing solution for a maze with the same name - delete it
        /// </summary>
        /// <param name="path">path of the file to load</param>
        /// <param name="mazeName">maze name</param>
        public void loadMaze(string path, string mazeName)
        {
            int numOfBytes;

            byte[]      arrayCmp  = new byte[100];
            List <byte> totalComp = new List <byte>();

            using (FileStream fileInStream = new FileStream(path, FileMode.Open))
            {
                using (Stream compressor = new MyCompressorStream(fileInStream))
                {
                    while ((numOfBytes = compressor.Read(arrayCmp, 0, 100)) != 0)
                    {
                        for (int i = 0; i < numOfBytes; i++)
                        {
                            totalComp.Add(arrayCmp[i]);
                        }
                    }
                }
            }
            addMaze(mazeName, new Maze3d(totalComp.ToArray()));
        }
Esempio n. 15
0
 /// <summary>
 /// Loads maze from given path and saves it in app memory by given name
 /// </summary>
 /// <param name="path">path</param>
 /// <param name="mazeName">maze name</param>
 public void LoadMaze(string path, string mazeName)
 {
     if (!m_savedMazes.ContainsKey(path))
     {
         ModelChanged("path doesnt exit");
     }
     else //file path exist
     {
         Maze3d savedMaze = m_savedMazes[path];
         byte[] mazeBytes = new byte[savedMaze.toByteArray().Length];
         using (FileStream fileInStream = new FileStream(path, FileMode.Open))
         {
             using (Stream inStream = new MyCompressorStream(fileInStream, ATP2016Library.Compression.CompressionMode.Decompress))
             {
                 inStream.Read(mazeBytes, 0, mazeBytes.Length);
             }
         }
         Maze3d loadedMaze = new Maze3d(mazeBytes);
         AddToGeneratedMazesDictionary(mazeName, loadedMaze);
         ModelChanged("maze " + mazeName + " was loaded successfully");
     }
 }
Esempio n. 16
0
        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));
        }
Esempio n. 17
0
        /// <summary>
        /// Saves maze by name to a spesific path
        /// </summary>
        /// <param name="mazeName">maze name</param>
        /// <param name="path">path</param>
        public void SaveMaze(string mazeName, string path)
        {
            if (m_generatedMazes.ContainsKey(mazeName))
            {
                Maze3d maze = m_generatedMazes[mazeName];
                AddToSavedMazes(path, maze);//add the maze to the saved mazes Dictionary

                using (FileStream fileOutStream = new FileStream(path, FileMode.Create))
                {
                    using (Stream outStream = new MyCompressorStream(fileOutStream, ATP2016Library.Compression.CompressionMode.Compress))
                    {
                        outStream.Write(maze.toByteArray(), 0, maze.toByteArray().Length);
                        outStream.Flush();
                    }
                }

                ModelChanged("maze " + mazeName + " was saved in " + path);
            }
            else
            {
                ModelChanged("maze doesnt exist");
            }
        }