Sign in to follow this  
PCosmin89

[Question] Simple pathfinding, obstacles question

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this