Exemplo n.º 1
0
    public CartesianField(ImmutableVector <int> dimensions, Func <ImmutableVector <int>, T> factory)
    {
        Dimensions = dimensions;

        int volume;

        if (dimensions.Dimensionality == 0)
        {
            volume = 0;
        }
        else
        {
            volume = 1;
            foreach (int dimension in dimensions)
            {
                volume *= dimension;
            }
        }

        cells = new T[volume];
        foreach (var coordinate in Coordinates)
        {
            this[coordinate] = factory(coordinate);
        }
    }
Exemplo n.º 2
0
        public void ConstructImmutableFromMutable()
        {
            var immutable = new ImmutableVector <int>(new MutableVector <int>(1, 2, 3));

            Assert.AreEqual(new int[] { 1, 2, 3 }, immutable);
        }
Exemplo n.º 3
0
 private static void SetImageShiftRatioAction(ImageDirectory directory, ImmutableVector shiftVector)
 {
Exemplo n.º 4
0
        public static Maze Deserialize2D(string serialized)
        {
            try
            {
                var rawLines = serialized.Split('\n', '\r');
                var lines    = new List <string>(rawLines.Length);
                foreach (var line in from s in rawLines where s != "" select s)
                {
                    lines.Add(line);
                }

                bool reverseEndpoints = false;
                if (lines[lines.Count - 1] == BoxDrawing.reverseEndpoints)
                {
                    reverseEndpoints = true;
                    lines.RemoveAt(lines.Count - 1);
                }

                var maze = new Maze((lines[0].Length - 1) / 2, lines.Count - 1);

                // Walls
                for (int y = 0; y < maze.Dimensions[1]; ++y)
                {
                    var line = lines[y];
                    for (int x = 0; x < maze.Dimensions[0]; ++x)
                    {
                        var wallBits = DecodeCorner(line[2 * x]);
                        var walls    = maze[x, y];
                        if (x > 0)
                        {
                            walls[0] = (wallBits & 8) != 0;
                        }
                        if (y > 0)
                        {
                            walls[1] = (wallBits & 1) != 0;
                        }
                    }
                }

                // Endpoints
                int i         = 0;
                var endpoints = new ImmutableVector <int> [2];
                {
                    var line = lines[0];
                    for (int x = 0; x < maze.Dimensions[0]; ++x)
                    {
                        if (line[2 * x + 1] == ' ')
                        {
                            endpoints[i++] = new ImmutableVector <int>(x, -1);
                        }
                    }
                }
                for (int y = 0; y < maze.Dimensions[1]; ++y)
                {
                    var line = lines[y];
                    if ((DecodeCorner(line[0]) & 8) == 0)
                    {
                        endpoints[i++] = new ImmutableVector <int>(-1, y);
                    }
                    if ((DecodeCorner(line[2 * maze.Dimensions[0]]) & 8) == 0)
                    {
                        endpoints[i++] = new ImmutableVector <int>(maze.Dimensions[0], y);
                    }
                }
                {
                    var line = lines[maze.Dimensions[1]];
                    for (int x = 0; x < maze.Dimensions[0]; ++x)
                    {
                        if (line[2 * x + 1] == ' ')
                        {
                            endpoints[i++] = new ImmutableVector <int>(x, maze.Dimensions[1]);
                        }
                    }
                }

                switch (i)
                {
                case 1:
                    maze.Entrance = maze.Exit = endpoints[0];
                    break;

                case 2:
                    maze.Entrance = endpoints[reverseEndpoints ? 1 : 0];
                    maze.Exit     = endpoints[reverseEndpoints ? 0 : 1];
                    break;
                }

                return(maze);
            }
            catch (Exception e)
            {
                Debug.Log(e);
                return(null);
            }
        }
Exemplo n.º 5
0
        public static Maze Deserialize3D(string serialized)
        {
            try
            {
                var rawLines = serialized.Split('\n', '\r');
                var lines    = new List <string>(rawLines.Length);
                foreach (var line in from s in rawLines where s != "" select s)
                {
                    lines.Add(line);
                }

                bool reverseEndpoints = false;
                if (lines[lines.Count - 1] == BoxDrawing.reverseEndpoints)
                {
                    reverseEndpoints = true;
                    lines.RemoveAt(lines.Count - 1);
                }

                int layerWidth = 3;
                bool HasVerticalWall()
                {
                    for (int row = 0; row < lines.Count; ++row)
                    {
                        int corner = DecodeCorner(lines[row][layerWidth]);
                        if (corner != -1 && (corner & 10) != 0)
                        {
                            return(true);
                        }
                    }
                    return(false);
                }
                while (!HasVerticalWall())
                {
                    layerWidth += 2;
                }

                var maze = new Maze((layerWidth - 1) / 2, lines[0].Length / layerWidth, lines.Count - 1);

                // Walls
                for (int z = 0; z < maze.Dimensions[2]; ++z)
                {
                    for (int y = 0; y < maze.Dimensions[1]; ++y)
                    {
                        var line = lines[z];
                        for (int x = 0; x < maze.Dimensions[0]; ++x)
                        {
                            int ci = layerWidth * y + 2 * x;

                            var wallBits = DecodeCorner(line[ci]);
                            var walls    = maze[x, y, z];
                            if (x > 0)
                            {
                                walls[0] = (wallBits & 8) != 0;
                            }
                            if (z > 0)
                            {
                                walls[2] = (wallBits & 1) != 0;
                            }

                            if (y > 0)
                            {
                                walls[1] = (DecodeFloor(lines[z + 1][ci + 1]) & 1) == 0;
                            }
                            if (y < maze.Dimensions[1] - 1)
                            {
                                walls[4] = (DecodeFloor(line[ci + 1]) & 2) == 0;
                            }
                        }
                    }
                }

                // Endpoints
                int i         = 0;
                var endpoints = new ImmutableVector <int> [2];
                {
                    var line = lines[0];
                    for (int y = 0; y < maze.Dimensions[1]; ++y)
                    {
                        for (int x = 0; x < maze.Dimensions[0]; ++x)
                        {
                            if ((DecodeCorner(line[layerWidth * y + 2 * x]) & 1) == 0)
                            {
                                endpoints[i++] = new ImmutableVector <int>(x, y, -1);
                            }
                        }
                    }
                }
                for (int z = 0; z < maze.Dimensions[2]; ++z)
                {
                    var line = lines[z + 1];
                    {
                        for (int x = 0; x < maze.Dimensions[0]; ++x)
                        {
                            if ((DecodeFloor(line[2 * x + 1]) & 1) != 0)
                            {
                                endpoints[i++] = new ImmutableVector <int>(x, -1, z);
                            }
                        }
                    }
                    line = lines[z];
                    for (int y = 0; y < maze.Dimensions[1]; ++y)
                    {
                        int ci = layerWidth * y;
                        if ((DecodeCorner(line[ci]) & 8) == 0)
                        {
                            endpoints[i++] = new ImmutableVector <int>(-1, y, z);
                        }
                        if ((DecodeCorner(line[ci + layerWidth - 1]) & 8) == 0)
                        {
                            endpoints[i++] = new ImmutableVector <int>(maze.Dimensions[0], y, z);
                        }
                    }
                    {
                        int ci = layerWidth * (maze.Dimensions[1] - 1);
                        for (int x = 0; x < maze.Dimensions[0]; ++x)
                        {
                            if ((DecodeFloor(line[ci + 2 * x + 1]) & 2) != 0)
                            {
                                endpoints[i++] = new ImmutableVector <int>(x, maze.Dimensions[1], z);
                            }
                        }
                    }
                }
                {
                    var line = lines[maze.Dimensions[2]];
                    for (int y = 0; y < maze.Dimensions[1]; ++y)
                    {
                        for (int x = 0; x < maze.Dimensions[0]; ++x)
                        {
                            if ((DecodeCorner(line[layerWidth * y + 2 * x]) & 1) == 0)
                            {
                                endpoints[i++] = new ImmutableVector <int>(x, y, maze.Dimensions[2]);
                            }
                        }
                    }
                }

                switch (i)
                {
                case 1:
                    maze.Entrance = maze.Exit = endpoints[0];
                    break;

                case 2:
                    maze.Entrance = endpoints[reverseEndpoints ? 1 : 0];
                    maze.Exit     = endpoints[reverseEndpoints ? 0 : 1];
                    break;
                }

                return(maze);
            }
            catch (Exception e)
            {
                Debug.Log(e);
                return(null);
            }
        }
Exemplo n.º 6
0
 public Walls(Maze maze, ImmutableVector <int> coordinate)
 {
     this.maze       = maze;
     this.coordinate = coordinate;
     contained       = maze.cells.ContainsCoordinate(coordinate);
 }
Exemplo n.º 7
0
 public static IMaze Swizzle(this IMaze maze, ImmutableVector <int> swizzle) => new Maze.Swizzler(maze, swizzle);
Exemplo n.º 8
0
 /// <param name="swizzle">Swizzle is specified like a direction. Elements less than the
 /// dimensionality map the specified incoming dimension to the outgoing dimension at that
 /// position. Otherwise the dimension is flipped.</param>
 public Swizzler(IMaze maze, ImmutableVector <int> swizzle)
 {
     this.maze    = maze;
     this.swizzle = swizzle;
 }
Exemplo n.º 9
0
 public Walls(IMazeWalls backing, ImmutableVector <int> swizzle)
 {
     this.backing = backing;
     this.swizzle = swizzle;
 }