예제 #1
0
 private MazeShape(int width, int height)
 {
     this.maze = new Maze(width, height);
     maze.CreateMaze();
 }
예제 #2
0
        /// <summary>
        /// A string that encodes the maze parameters.
        /// This code can be used to construct an identical maze.
        /// </summary>
        public string Code(Maze maze)
        {
            long           nCode         = 0;
            MazeDimensions dimensionsObj = MazeDimensions.Instance(codeVersion);

            #region Encode the relevant parameters into a numeric code

            // Items are encoded in reverse order of decoding.
            // Some items must be encoded before others if decoding requires them.

            if (codeVersion == 0)
            {
                // Encode the start and end points.
                // Instead of the direct coordinates we will use the following information:
                // * travel direction
                // * distance from the border (instead of coordinate)
                // * other coordinate
                // The scaling factor is always MazeDimensions.MaxXSize, as it is greater than MazeDimensions.MaxYSize.

                int d1, d2, c1, c2;

                switch (maze.Direction)
                {
                case WallPosition.WP_E:
                    d1 = maze.StartSquare.XPos;
                    d2 = maze.XSize - 1 - maze.EndSquare.XPos;
                    c1 = maze.StartSquare.YPos;
                    c2 = maze.EndSquare.YPos;
                    break;

                case WallPosition.WP_W:
                    d1 = maze.EndSquare.XPos;
                    d2 = maze.XSize - 1 - maze.StartSquare.XPos;
                    c1 = maze.EndSquare.YPos;
                    c2 = maze.StartSquare.YPos;
                    break;

                case WallPosition.WP_S:
                    d1 = maze.StartSquare.YPos;
                    d2 = maze.YSize - 1 - maze.EndSquare.YPos;
                    c1 = maze.StartSquare.XPos;
                    c2 = maze.EndSquare.XPos;
                    break;

                case WallPosition.WP_N:
                    d1 = maze.EndSquare.YPos;
                    d2 = maze.YSize - 1 - maze.StartSquare.YPos;
                    c1 = maze.EndSquare.XPos;
                    c2 = maze.StartSquare.XPos;
                    break;

                default:
                    d1 = d2 = c1 = c2 = -1;
                    break;
                }

                nCode *= (dimensionsObj.MaxBorderDistance + 1);
                nCode += d1;

                nCode *= (dimensionsObj.MaxBorderDistance + 1);
                nCode += d2;

                nCode *= (dimensionsObj.MaxXSize + 1);
                nCode += c1;

                nCode *= (dimensionsObj.MaxXSize + 1);
                nCode += c2;

                nCode *= (int)WallPosition.WP_NUM;
                nCode += (int)maze.Direction;
            }

            // Encode maze dimension.

            nCode *= (dimensionsObj.MaxYSize - dimensionsObj.MinSize + 1);
            nCode += (maze.YSize - dimensionsObj.MinSize);

            nCode *= (dimensionsObj.MaxXSize - dimensionsObj.MinSize + 1);
            nCode += (maze.XSize - dimensionsObj.MinSize);

            // Encode initial seed.

            nCode *= SeedLimit;
            nCode += maze.Seed;

            #endregion

            // v0: The resulting nCode is less than 26^12.  See SWA.Ariadne.Model.Tests unit tests.
            // v1: The resulting nCode is less than 36^6.  See SWA.Ariadne.Model.Tests unit tests.

            #region Convert the numeric code into a character code (base 26)

            StringBuilder result = new StringBuilder(7);

            for (int p = CodeLength; p-- > 0;)
            {
                int digit = (int)(nCode % CodeDigitRange);
                nCode /= CodeDigitRange;
                char c = CodeDigits[digit];
                result.Insert(0, c);
            }

            switch (codeVersion)
            {
            case 0:
                result.Insert(8, this.Separator);
                result.Insert(4, this.Separator);
                break;

            case 1:
                result.Insert(3, this.Separator);
                break;

            case 2:
                result.Insert(3, this.Separator);
                break;
            }

            #endregion

            return(result.ToString());
        }