Example #1
0
        public List <History> FindPathThroughMap(RTSGameMap map)
        {
            //Find the quickest way through the map?
            //The start and end point is fixed in this instance
            var startLocation = map.StartLocation;
            var endLocation   = map.EndLocation;

            List <History> whereHaveIBeen = new List <History>();
            Coords         whereWillIBe   = new Coords();
            Coords         whereAmI       = new Coords();
            bool           foundEnd       = false;

            whereAmI = startLocation;
            int stepNumber = 0;

            do
            {
                whereWillIBe = whereAmI;

                bool dontMoveRight = false;
                bool dontMoveLeft  = false;
                bool dontMoveDown  = false;
                bool dontMoveUp    = false;
                var  nextStep      = whereHaveIBeen.Where(f => f.Location.x == whereAmI.x && f.Location.y == whereAmI.y).ToList();
                if (nextStep != null)
                {
                    foreach (History next in nextStep)
                    {
                        next.Valid = false;
                        if ((next.NewLocation.x - next.Location.x) > 0)
                        {
                            dontMoveRight = true;
                        }
                        if ((next.NewLocation.x - next.Location.x) < 0)
                        {
                            dontMoveLeft = true;
                        }
                        if ((next.NewLocation.y - next.Location.y) > 0)
                        {
                            dontMoveDown = true;
                        }
                        if ((next.NewLocation.x - next.Location.x) < 0)
                        {
                            dontMoveUp = true;
                        }
                    }
                }

                if (map.GetLocationType((whereAmI.x + 1), whereAmI.y) == RTSGameMap.RTSGameMapOccupied.False && dontMoveRight == false)
                {
                    whereWillIBe.x++;
                }
                else if (map.GetLocationType(whereAmI.x, (whereAmI.y + 1)) == RTSGameMap.RTSGameMapOccupied.False && dontMoveDown == false)
                {
                    whereWillIBe.y++;
                }
                else if (map.GetLocationType((whereAmI.x - 1), whereAmI.y) == RTSGameMap.RTSGameMapOccupied.False && dontMoveLeft == false)
                {
                    whereWillIBe.x--;
                }
                else if (map.GetLocationType(whereAmI.x, (whereAmI.y - 1)) == RTSGameMap.RTSGameMapOccupied.False && dontMoveUp == false)
                {
                    whereWillIBe.y--;
                }
                map.SetLocationType(whereAmI.x, whereAmI.y, RTSGameMap.RTSGameMapOccupied.False);
                map.SetLocationType(whereWillIBe.x, whereWillIBe.y, RTSGameMap.RTSGameMapOccupied.True);

                if (whereWillIBe.Equals(startLocation))
                {
                    break;
                }

                whereHaveIBeen.Add(new History
                {
                    StepNumber  = stepNumber,
                    Location    = whereAmI,
                    NewLocation = whereWillIBe,
                    Valid       = true
                });
                Debug.WriteLine(String.Format("{0} - {1} : {2} - {3}", whereAmI.x, whereAmI.y, whereWillIBe.x, whereWillIBe.y));
                whereAmI = whereWillIBe;

                if (whereAmI.Equals(endLocation))
                {
                    foundEnd = true;
                }
            } while (foundEnd != true);
            return(whereHaveIBeen.Where(f => f.Valid == true).ToList());
        }
Example #2
0
        public RTSGameMap CreateMapTiles(int squareSize)
        {
            StringBuilder sb  = new StringBuilder();
            var           map = new RTSGameMap(squareSize);

            //Line 1 - Set first row to be a barrier
            //for (int i = 0; i < squareSize; i++)
            //{
            //    map.SetLocationType(i, 0, RTSGameMap.RTSGameMapOccupied.True);
            //}

            ////Set last row to be a barrier
            //for (int i = 0; i < squareSize; i++)
            //{
            //    map.SetLocationType(i, (squareSize - 1), RTSGameMap.RTSGameMapOccupied.True);
            //}

            ////Set first column to be a barrier (Row + barrier row)
            //for (int i = 2; i < squareSize; i++)
            //{
            //    map.SetLocationType(0, i, RTSGameMap.RTSGameMapOccupied.True);
            //}


            ////Set last column to be a barrier (row - 1 - barrier row)
            //for (int i = 0; i < (squareSize - 2); i++)
            //{
            //    map.SetLocationType((squareSize - 1), i, RTSGameMap.RTSGameMapOccupied.True);
            //}


            //Now do some randomisation to set the map to be occupied
            Random rnd = new Random();

            for (int i = 0; i < (squareSize - 1); i++)
            {
                for (int j = 0; j < 10; j++)
                {
                    int col = rnd.Next(1, squareSize + 1);
                    if (col < squareSize)
                    {
                        map.SetLocationType(i, col, RTSGameMap.RTSGameMapOccupied.True);
                    }
                }
            }

            //map.SetLocationType(0, 1, RTSGameMap.RTSGameMapOccupied.True);
            //map.SetLocationType(1, 1, RTSGameMap.RTSGameMapOccupied.True);
            //map.SetLocationType(2, 1, RTSGameMap.RTSGameMapOccupied.True);
            //map.SetLocationType(3, 1, RTSGameMap.RTSGameMapOccupied.True);
            //map.SetLocationType(4, 1, RTSGameMap.RTSGameMapOccupied.True);

            //map.SetLocationType(4, 2, RTSGameMap.RTSGameMapOccupied.True);
            //map.SetLocationType(4, 3, RTSGameMap.RTSGameMapOccupied.True);
            //map.SetLocationType(4, 4, RTSGameMap.RTSGameMapOccupied.True);
            //map.SetLocationType(4, 5, RTSGameMap.RTSGameMapOccupied.True);

            //map.SetLocationType(6, 0, RTSGameMap.RTSGameMapOccupied.True);
            //map.SetLocationType(6, 1, RTSGameMap.RTSGameMapOccupied.True);
            //map.SetLocationType(6, 2, RTSGameMap.RTSGameMapOccupied.True);
            //map.SetLocationType(6, 3, RTSGameMap.RTSGameMapOccupied.True);
            //map.SetLocationType(6, 4, RTSGameMap.RTSGameMapOccupied.True);
            //map.SetLocationType(6, 4, RTSGameMap.RTSGameMapOccupied.True);

            //for (int i = 0; i < squareSize-1; i++)
            //{
            //    map.SetLocationType(i, 6, RTSGameMap.RTSGameMapOccupied.True);
            //}
            //for (int i = 2; i < squareSize; i++)
            //{
            //    map.SetLocationType(i, 8, RTSGameMap.RTSGameMapOccupied.True);
            //}

            //for (int i = 0; i < squareSize -1; i++)
            //{
            //    map.SetLocationType(i, 10, RTSGameMap.RTSGameMapOccupied.True);
            //}

            return(map);
        }
Example #3
0
        public Stack <Coords> FindPathThroughMap(RTSGameMap visitedMap)
        {
            StringBuilder sb = new StringBuilder();

            int[,] map = new int[visitedMap.MapSize, visitedMap.MapSize];


            for (int i = 0; i < visitedMap.MapSize; i++)
            {
                for (int j = 0; j < visitedMap.MapSize; j++)
                {
                    map[i, j] = 1000000;
                }
            }

            map[0, 0] = 0;

            int           dist = 1;//Distance between squares
            List <Coords> next = new List <Coords>();

            next.Add(new Coords {
                x = 0, y = 0
            });                                    //Start Location
            while (next.Count != 0)
            {
                //Need to get each of its neighbours
                Coords ff         = next.FirstOrDefault();
                int    val        = map[ff.x, ff.y];
                var    neighbours = visitedMap.GetNeighbours(ff.x, ff.y);
                visitedMap.SetLocationType(ff.x, ff.y, RTSGameMap.RTSGameMapOccupied.True);
                next.Remove(ff);

                foreach (Coords coords in neighbours)
                {
                    if (map[coords.x, coords.y] > val + dist)
                    {
                        map[coords.x, coords.y] = val + dist;
                    }
                    if (!next.Where(f => f.x == coords.x && f.y == coords.y).Any())
                    {
                        next.Add(coords);
                    }
                }
            }

            //Start from the end point and traverse backwards
            //Last in first out stack
            Stack <Coords> st      = new Stack <Coords>();
            Stack <Coords> validSt = new Stack <Coords>();

            for (int y = 0; y < visitedMap.MapSize; y++)
            {
                for (int x = 0; x < visitedMap.MapSize; x++)
                {
                    st.Push(new Coords {
                        x = x, y = y
                    });
                }
            }

            //Get the first value from the stack and then remove
            int    squareVal       = -1;
            int    lastSquareVal   = -1;
            bool   foundPath       = false;
            Coords reverseLocation = visitedMap.EndLocation;

            lastSquareVal = map[reverseLocation.x, reverseLocation.y];
            validSt.Push(visitedMap.EndLocation);
            while (foundPath == false)
            {
                var adjacent = visitedMap.GetAdjacent(reverseLocation.x, reverseLocation.y);
                foreach (Coords coords in adjacent)
                {
                    squareVal = map[coords.x, coords.y];
                    if ((lastSquareVal - squareVal) == dist)
                    {
                        lastSquareVal = squareVal;
                        validSt.Push(coords);
                        reverseLocation = coords;
                        break;
                    }
                }
                if (reverseLocation.Equals(visitedMap.StartLocation))
                {
                    foundPath = true;
                }
            }

            sb.Clear();
            for (int x = 0; x < visitedMap.MapSize; x++)
            {
                for (int y = 0; y < visitedMap.MapSize; y++)
                {
                    sb.Append(":");
                    sb.Append(map[y, x]);
                }
                Debug.WriteLine(sb.ToString());
                sb.Clear();
            }


            foreach (Coords coords in validSt)
            {
                Debug.WriteLine(String.Format("{0} - {1}", coords.x, coords.y));
            }

            return(validSt);
        }