Пример #1
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);
        }
Пример #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)
        {
            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();
                }
            }
            printMaze(winMaze, winMaze.PosZ);
            return(true);
        }
Пример #3
0
        public void generate3dMaze()
        {
            string mazeName = currentMazeName;

            if (string.IsNullOrEmpty(LevelData))
            {
                ErrorOutput = ("Please insert 'Levels' value");
                MessageBox.Show(ErrorOutput, "Error");
                return;
            }
            if (string.IsNullOrEmpty(ColumnData))
            {
                ErrorOutput = ("Please insert 'Column' value");
                MessageBox.Show(ErrorOutput, "Error");
                return;
            }
            if (string.IsNullOrEmpty(RowData))
            {
                ErrorOutput = ("Please insert 'Rows' value");
                MessageBox.Show(ErrorOutput, "Error");
                return;
            }
            if (string.IsNullOrEmpty(CellSizeData))
            {
                ErrorOutput = ("Please insert 'Size' value");
                MessageBox.Show(ErrorOutput, "Error");
                return;
            }
            if ((Int32.Parse(LevelData) < 1) || (Int32.Parse(ColumnData) < 6) || (Int32.Parse(RowData) < 6) || (Int32.Parse(CellSizeData) < 10))
            {
                ErrorOutput = ("Incorrect maze input, Please insert:\n\nLevels - Above 1\nColumns - Above 6\nRows - Above 6");
                MessageBox.Show(ErrorOutput, "Error");
                return;
            }

            if (((Int32.Parse(CellSizeData) * Int32.Parse(ColumnData)) > 380) || ((Int32.Parse(CellSizeData) * Int32.Parse(RowData)) > 350))
            {
                ErrorOutput = ("Size of the maze is too large:\n\nPlease enter new dimetions\nor change the cell size");
                MessageBox.Show(ErrorOutput, "Error");
                return;
            }
            int[] mazeDimentions = { Int32.Parse(LevelData), Int32.Parse(ColumnData), Int32.Parse(RowData) };
            int   cellSize       = Int32.Parse(CellSizeData);

            var resetEvent = new ManualResetEvent(false);

            ThreadPool.QueueUserWorkItem(new WaitCallback((state) =>
            {
                threadGenerateMaze(mazeDimentions, mazeName, cellSize);
                resetEvent.Set();
            }));
            resetEvent.WaitOne();

            WinMaze winMaze = getWinMaze(mazeName);

            printMaze(winMaze, winMaze.PosZ);
        }
Пример #4
0
        /// <summary>
        /// Displays the current state of the maze in view
        /// </summary>
        /// <param name="winMaze">WinMaze object</param>
        /// <param name="floor">Current floor to displays</param>
        private void printMaze(WinMaze winMaze, int floor)
        {
            CurrentMazeCanvas   = new Canvas();
            SecondaryMazeCanvas = new Canvas();

            if ((winMaze.PosZ == 0) && (winMaze.PosX == (winMaze.getMaze().MyColumns - 1)) &&
                (winMaze.PosY == (winMaze.getMaze().MyRows - 1)) && !mFinished)
            {
                mFinished = true;
                FinishWindow finishWindow = new FinishWindow();
                finishWindow.Show();
            }

            staticMaze = winMaze;
            if (solExists)
            {
                solExists = false;
                winMaze   = originalWinMaze;
                winMaze.clearSolution();
            }
            else
            {
                originalWinMaze = winMaze;
            }

            MazeBoard mazeBoard = new MazeBoard(winMaze, winMaze.PosZ, winMaze.CellSize, 0);

            CurrentMazeCanvas.Children.Add(mazeBoard);
            Canvas.SetLeft(mazeBoard, 30);
            Canvas.SetTop(mazeBoard, 10);

            if ((winMaze.PosZ + 1) < winMaze.getMaze().MyHeight) // Upper level display
            {
                MazeBoard upMazeBoard = new MazeBoard(winMaze, floor, 10, 1);
                SecondaryMazeCanvas.Children.Add(upMazeBoard);
                Canvas.SetLeft(upMazeBoard, 0);
                Canvas.SetTop(upMazeBoard, 30);
            }

            if ((winMaze.PosZ - 1) >= 0)
            {
                MazeBoard downMazeBoard = new MazeBoard(winMaze, floor, 10, -1); // Lower level display
                SecondaryMazeCanvas.Children.Add(downMazeBoard);
                Canvas.SetLeft(downMazeBoard, 0);
                Canvas.SetTop(downMazeBoard, 200);
            }
            isMazeExists = true;
            staticMaze   = winMaze;
        }
Пример #5
0
        /// <summary>
        /// Move up in the maze command
        /// </summary>
        public void moveUp()
        {
            WinMaze maze = m_winMazesDictionary["maze"];

            if ((maze.PosZ + 1) < maze.getMaze().MyHeight)
            {
                int isBrockAbove = maze.getMaze().getMazeByFloor(maze.PosZ + 1).getCell(maze.PosX, maze.PosY).BlockOrEmpty;
                if (isBrockAbove == 0)
                {
                    maze.PosZ += 1;
                    m_winMazesDictionary[maze.getName()] = maze;
                    m_currentWinMaze = maze;
                    printMaze(maze, maze.PosZ);
                }
            }
        }
Пример #6
0
        /// <summary>
        /// Move down in the maze command
        /// </summary>
        public void moveDown()
        {
            WinMaze maze = m_winMazesDictionary["maze"];

            if (maze.PosZ - 1 >= 0)
            {
                int isBrockBelow = maze.getMaze().getMazeByFloor(maze.PosZ - 1).getCell(maze.PosX, maze.PosY).BlockOrEmpty;
                if (isBrockBelow == 0)
                {
                    maze.PosZ -= 1;
                    m_winMazesDictionary[maze.getName()] = maze;
                    m_currentWinMaze = maze;
                    printMaze(maze, maze.PosZ);
                }
            }
        }
Пример #7
0
        /// <summary>
        /// Move forward in the maze command
        /// </summary>
        public void moveForward()
        {
            WinMaze maze = m_winMazesDictionary["maze"];

            int[] currentCellWalls = maze.getMaze().getMazeByFloor(maze.PosZ).getCell(maze.PosX, maze.PosY).getWallsAroundCell();
            if (maze.PosY + 1 < maze.getMaze().MyRows)
            {
                if (currentCellWalls[3] == 0)
                {
                    maze.PosY += 1;
                    m_winMazesDictionary[maze.getName()] = maze;
                    m_currentWinMaze = maze;
                    printMaze(maze, maze.PosZ);
                }
            }
        }
Пример #8
0
        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);
            originalWinMaze  = winMaze;
            isSolutionExists = false;
        }
Пример #9
0
        /// <summary>
        /// Move back in the maze command
        /// </summary>
        public void moveBack()
        {
            WinMaze maze = m_winMazesDictionary["maze"];

            int[] currentCellWalls = maze.getMaze().getMazeByFloor(maze.PosZ).getCell(maze.PosX, maze.PosY).getWallsAroundCell();
            if (maze.PosY - 1 >= 0)
            {
                int[] currentCellWallsBack = maze.getMaze().getMazeByFloor(maze.PosZ).getCell(maze.PosX, maze.PosY - 1).getWallsAroundCell();
                if ((currentCellWalls[2] == 0) || (currentCellWallsBack[3] == 0))
                {
                    maze.PosY -= 1;
                    m_winMazesDictionary[maze.getName()] = maze;
                    m_currentWinMaze = maze;
                    printMaze(maze, maze.PosZ);
                }
            }
        }
Пример #10
0
        /// <summary>
        /// Displays the solution in View
        /// </summary>
        public void showSolution()
        {
            string   mazeTitle = currentMazeName;
            Solution solution  = displaySolution(mazeTitle);

            if (null == solution)
            {
                ErrorOutput = ("ERROR: The solution does not exist for the maze named: " + mazeTitle + ".");
                MessageBox.Show(ErrorOutput, "Error");
            }
            else // soultion exists
            {
                List <string[]> tempList = solution.getSolutionCoordinates();
                MessageBox.Show(solution.GetSolutionPathByString());
                staticMaze.setSolutionCoordinates(tempList);
                m_currentWinMaze.setSolutionCoordinates(tempList);
            }
            WinMaze winMaze = getWinMaze("maze");

            printMaze(winMaze, winMaze.PosZ);
        }
Пример #11
0
 /// <summary>
 /// Hides the solution in View
 /// </summary>
 public void hideSolution()
 {
     staticMaze = originalWinMaze;
     solExists  = true;
     printMaze(staticMaze, staticMaze.PosZ);
 }