Jump to content
  • Advertisement
Sign in to follow this  
PCosmin89

[Question] Simple pathfinding, obstacles question

This topic is 3815 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I'm having a bit of difficulty in choosing the right road...
[Source]
        static char[,] maze = new char[20, 30];
        static int i, j;
        static int pxpos, pypos, gxpos, gypos;
        static int k = 1;
        static int type;

        static void FillMaze()
        {
            for (i = 0; i < 20; i++)
            {
                maze[i, 0] = '+';
                maze[i, 29] = '+';
            }
            for (j = 0; j < 30; j++)
            {
                maze[0, j] = '+';
                maze[19, j] = '+';
            }
        }
        static void ShowMaze()
        {
            for (i = 0; i < 20; i++)
                for (j = 0; j < 30; j++)
                {
                    Console.Write(maze[i, j]);
                    if (j == 29)
                        Console.WriteLine();
                }
            Console.WriteLine("Simple pathfinding v.1.0");
            Console.WriteLine("by Parvulescu Cosmin");
        }
        static void Positions()
        {
            Random rand = new Random();
            pxpos = rand.Next(1, 28);
            pypos = rand.Next(1, 18);
            maze[pypos, pxpos] = 'P';
            gxpos = rand.Next(1, 28);
            gypos = rand.Next(1, 18);
            maze[gypos, gxpos] = 'G';
        }
        static void Move(int y, int x)
        {
            //Type 1
            if (type == 1)
            {
                if ((y > gypos) && (x < gxpos)) { pypos--; pxpos++; }
                if ((y > gypos) && (x > gxpos)) { pypos--; pxpos--; }
                if ((y < gypos) && (x < gxpos)) { pypos++; pxpos++; }
                if ((y < gypos) && (x > gxpos)) { pypos++; pxpos--; }
                if ((y < gypos) && (x == gxpos)) pypos++;
                if ((y > gypos) && (x == gxpos)) pypos--;
                if ((y == gypos) && (x < gxpos)) pxpos++;
                if ((y == gypos) && (x > gxpos)) pxpos--;
            }

            //Type 2
            if (type == 2)
            {
                while (true)
                {
                    if (y > gypos) { pypos--; break; }
                    if (y < gypos) { pypos++; break; }
                    if (x > gxpos) { pxpos--; break; }
                    if (x < gxpos) { pxpos++; break; }
                }
            }

            //Type 3
            if (type == 3)
            {
                while (true)
                {
                    if (k % 2 == 0)
                    {
                        if (y > gypos) { pypos--; k++; break; }
                        if (y < gypos) { pypos++; k++; break; }
                    }
                    if (x > gxpos) { pxpos--; k++; break; }
                    if (x < gxpos) { pxpos++; k++; break; }
                }
            }
        }

        static void Main(string[] args)
        {
            bool check = false;

            Console.Write("Choose a tracking type: 1, 2 or 3 -> "); type = Convert.ToInt32(Console.ReadLine());

            FillMaze();
            Positions();
            ShowMaze();

            while (check == false)
            {
                Console.Clear();
                maze[pypos, pxpos] = Convert.ToChar(0x00);
                Move(pypos, pxpos);
                maze[pypos, pxpos] = 'P';
                if (pxpos == gxpos && pypos == gypos)
                {
                    maze[pypos, gxpos] = '+';
                    check = true;
                }
                ShowMaze();
                Thread.Sleep(100);
            }

            Console.Clear();
            Console.WriteLine("TARGET FOUND!");
            ShowMaze();
            Console.ReadLine();
        }
[/Source]
This is what I have until now, three ways in which point 'P' reaches point 'G' but now I want to add some obstacles 'x' on the course which 'P' has to go around, now my question is, do I check each position of 'P' for neighboring obstacles and then put rules for each possibility ( there are many I think ), or is there another easier way ? ( hope I didn't make this post too difficult to understand )

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!