Ejemplo n.º 1
0
        private bool TryBuild(GridGraph g, Pair v)
        {
            List <char> built = new List <char>();

            foreach (char c in g.BuiltDirections[v.Item1, v.Item2])
            {
                built.Add(c);
            }
            List <char> movable = new List <char>();

            foreach (char c in g.MovableDirections[v.Item1, v.Item2])
            {
                movable.Add(c);
            }
            char initial = g.InitialBuiltDirection[v.Item1, v.Item2];

            // Special case for cell right below exit
            if (v.Equals(new Pair(g.Width / 2, 0)) && movable.Count == 1 && movable[0] == 'D')
            {
                return(false);
            }
            List <char> good = new List <char>();

            foreach (char c in g.PotentialDirections(v))
            {
                good.Add(c);
            }
            if (initial == 'U' || initial == 'D')
            {
                if (good.Contains('U'))
                {
                    good.Remove('U');
                }
                if (good.Contains('D'))
                {
                    good.Remove('D');
                }
            }
            else if (initial == 'L' || initial == 'R')
            {
                if (good.Contains('L'))
                {
                    good.Remove('L');
                }
                if (good.Contains('R'))
                {
                    good.Remove('R');
                }
            }
            foreach (char c in built)
            {
                if (good.Contains(c))
                {
                    good.Remove(c);
                }
            }
            List <int> probabilities = new List <int>();

            foreach (char c in good)
            {
                switch (c)
                {
                case 'U':
                    probabilities.Add(3);
                    break;

                case 'L':
                    probabilities.Add(2);
                    break;

                case 'R':
                    probabilities.Add(2);
                    break;

                case 'D':
                    probabilities.Add(1);
                    break;
                }
            }
            while (good.Count > 0)
            {
                PairList ranges = MakeRanges(probabilities);
                int      choice = rand.ChooseFrom(ranges);
                char     dir    = good[choice]; // Ha!
                // TODO change 1 to some general function for n
                int max_length = g.LongestPath(v, dir, 1);
                if (dir == 'D')
                {
                    //TODO more calculations into max length
                    max_length = Math.Min(max_length, 4);
                }
                else
                {
                    max_length = Math.Min(max_length, 6);
                }
                int length = rand.Generate(1, max_length);
                g.BuildPath(v, dir, length);
                return(true);
                //TODO we want to try all directions, not just the one we first select
            }
            return(false);
            //TODO we need to try again with different lengths and directions if it fails here
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Constructor which copies information from another GridGraph.
        /// </summary>
        /// <param name="other">The other GridGraph instance</param>
        public GridGraph(GridGraph other)
        {
            Width  = other.Width;
            Height = other.Height;
            Start  = Tuple.Create(Width / 2, Height - 1);
            InitialBuiltDirection = new char[Width, Height];
            MovableDirections     = new Directions[Width, Height];
            BuiltDirections       = new Directions[Width, Height];
            vertices       = new PairList();
            Distance       = new List <Tuple <Pair, int> >();
            adj            = new PairList[Width, Height];
            rev            = new PairList[Width, Height];
            is_path        = new bool[Width, Height];
            is_unused_path = new bool[Width, Height];
            is_wall        = new bool[Width, Height];
            is_unused_wall = new bool[Width, Height];
            for (int i = 0; i < Width; i++)
            {
                for (int j = 0; j < Height; j++)
                {
                    adj[i, j]               = new PairList();
                    rev[i, j]               = new PairList();
                    BuiltDirections[i, j]   = new List <char>();
                    MovableDirections[i, j] = new List <char>();

                    InitialBuiltDirection[i, j] = other.InitialBuiltDirection[i, j];
                    is_path[i, j]        = other.is_path[i, j];
                    is_unused_path[i, j] = other.is_unused_path[i, j];
                    is_wall[i, j]        = other.is_wall[i, j];
                    is_unused_wall[i, j] = other.is_unused_wall[i, j];

                    foreach (char c in other.BuiltDirections[i, j])
                    {
                        BuiltDirections[i, j].Add(c);
                    }
                    foreach (char c in other.MovableDirections[i, j])
                    {
                        MovableDirections[i, j].Add(c);
                    }
                    foreach (Pair p in other.adj[i, j])
                    {
                        adj[i, j].Add(new Tuple <int, int>(p.Item1, p.Item2));
                    }
                    foreach (Pair p in other.rev[i, j])
                    {
                        rev[i, j].Add(new Tuple <int, int>(p.Item1, p.Item2));
                    }
                }
            }
            foreach (Pair o in other.vertices)
            {
                Pair v = new Pair(o.Item1, o.Item2);
                vertices.Add(v);
            }
            foreach (Tuple <Pair, int> o in other.Distance)
            {
                Pair p = new Pair(o.Item1.Item1, o.Item1.Item2);
                Tuple <Pair, int> d = new Tuple <Pair, int>(p, o.Item2);
                Distance.Add(d);
            }
        }