AStar code:

path.Clear( ); // Rensa listan, när GeneratePath-metoden kallas så betyder det att vi letar efter en ny väg

            pathNode = -1;

            while ( true )
            {
                //    
                //    Console.SetCursorPosition( 0, 0 );
                //    Console.Write( new string( ' ', 50 ) );
                //    Console.SetCursorPosition( 0, 0 );
                //    Console.Write( "Distance: " + DistanceBetween( px, py, gx, gy ).ToString( ) + " Path.Count = " + path.Count.ToString());
                //    Console.SetCursorPosition( px, py );

                testPath.Clear( ); // Så att vi får en fräsch ny lista
                string[] coords; // Håller koordinaterna som vi utgår ifrån
                if ( pathNode == -1 ) // Om det är första noden som ska läggas till så är path-listan tom
                {
                    coords = (gx.ToString() + "|" + gy.ToString()).Split('|'); // Eftersom path-listan är tom så kör vi med de nuvaranda koordinaterna
                    testPath.Add( ( gx - 1 ).ToString( ) + "|" + gy.ToString( ) ); // Kolla till vänster
                    testPath.Add( ( gx + 1 ).ToString( ) + "|" + gy.ToString( ) ); // Kolla till höger
                    testPath.Add( gx.ToString( ) + "|" + ( gy - 1 ).ToString( ) ); // Kolla uppåt
                    testPath.Add( gx.ToString( ) + "|" + ( gy + 1 ).ToString( ) ); // Kolla neråt
                }
                else // Annars så utgår vi ifrån den sista tillagda noden (sista elementet i path-listan)
                {
                    coords = path[pathNode].Split( '|' );
                    testPath.Add( ( makeInt( coords[0] ) - 1 ).ToString( ) + "|" + coords[1].ToString( ) );
                    testPath.Add( ( makeInt( coords[0] ) + 1 ).ToString( ) + "|" + coords[1].ToString( ) );
                    testPath.Add( coords[0].ToString( ) + "|" + ( makeInt( coords[1] ) - 1 ).ToString( ) );
                    testPath.Add( coords[0].ToString( ) + "|" + ( makeInt( coords[1] ) + 1 ).ToString( ) );
                }


                double l = -1; // Avståndet mellan spelaren och den rutan vi kollar med
                int r = -1; // Positionen i listan för den rutan vi väljer
                bool isClosed = false; // Om koordinaten finns med i closed-listan: isClosed = true;
                for ( int i = 0; i < testPath.Count; i++ ) // Loopa genom koordinat-listan som vi ska testa
                {
                    string[] cs = testPath[i].Split( '|' ); // Splitta upp koordinaterna (som sparas som x|y)
                    if (l == -1 || DistanceBetween( px, py, makeInt( cs[0] ), makeInt( cs[1] ) ) < l && !tiles.isBlocked( makeInt( cs[0] ), makeInt( cs[1] ) ))
                    {
                        for ( int j = 0; j < closedPath.Count; j++ )
                        {
                            if ( testPath[i] == closedPath[j] )
                            {
                                isClosed = true;
                            }
                        }
                        if ( isClosed == false )
                        {
                            l = DistanceBetween( px, py, makeInt( coords[0] ), makeInt( coords[1] ) );
                            r = i;
                        }
                        else
                        {
                            closedPath.Add( testPath[i] );
                        }
                    }
                    else
                    {
                        for ( int j = 0; j < closedPath.Count; j++ )
                        {
                            if ( testPath[i] == closedPath[j] )
                            {
                                isClosed = true;
                            }
                        }
                        if ( isClosed == false )
                        {
                            closedPath.Add( testPath[i] );
                        }
                    }
                }

                //if ( r == -1 )
                //{
                    //r = 2;
                    //for ( int i = 0; i < testPath.Count; i++ )
                    //{
                    //    string[] cs = testPath[i].Split( "|".ToCharArray( ) );
                    //    if ( !tiles.isBlocked( makeInt( cs[0] ), makeInt( cs[1] ) ) )
                    //    {
                    //        if ( makeInt( coords[0] ) != makeInt( cs[0] ) && makeInt( coords[1] ) != makeInt( cs[1] ) )
                    //        {
                    //            r = i;
                    //            break;
                    //        }
                    //    }
                    //}
                //}


                path.Add( testPath[r].ToString( ) );
                closedPath.Add( testPath[r].ToString( ) );
                pathNode = path.Count - 1;

                string[] last = path[path.Count - 1].Split( "|".ToCharArray( ) );

                if ( makeInt( last[0] ) == px && makeInt( last[1] ) == py ) // Om vi har nått målet
                {
                    path.RemoveAt( path.Count - 1 ); // Ta bort sista, eftersom att vi inte kan gå igenom spelaren
                    pathNode = 0; // Så att spöket går igenom hela listen
                    hasPath = true; // Vi har hittat nyckeln till frihet
                    break; // Bryt ut ur loopen
                }

Follow path code:

int px = Console.CursorLeft;
            int py = Console.CursorTop;
            string[] currentMove = path[pathNode].Split( "|".ToCharArray( ) );

            /*Console.WriteLine();
            Console.WriteLine();
            for ( int i = 0; i < path.Count; i++ )
            {
                Console.Write( path[i].ToString( ) + "      " );
            }
            Console.SetCursorPosition( Console.CursorLeft, Console.CursorTop );
            */

            if ( gx < makeInt( currentMove[0] ) )
            {
                MoveRight( px, py );
            }
            else if ( gx > makeInt( currentMove[0] ) )
            {
                MoveLeft( px, py );
            }
            else if ( gy > makeInt( currentMove[1] ) )
            {
                MoveUp( px, py );
            }
            else if ( gy < makeInt( currentMove[1] ) )
            {
                MoveDown( px, py );
            }
            pathNode += 1;
            if ( path.Count < pathNode + 1 || path.Count < 1)
            {
                pathNode = -1;
                hasPath = false;
            }

I have absoluetly no idea what I'm doing wrong, and I'm sorry the comments are in Swedish :(

I think I'm going to have to rewrite it, but please, if you can find my errors, I'd love some help, I've been trying to find it out for several hours :<

What it does:
Adds all coordinates to the left, right, top and bottom of the ghost to a list called testPath.
Loops through the testPath list, and checks if the current tile is closest to the target, if it is, it adds it to the path list, which is the true list (that the ghost follows when it has generated the path), otherwise, it adds it to the closed list.

What it doesn't do:
It doesn't do it correctly, and sometimes adds the same coordinates to the closed list over and over again
It follows the path somewhat correctly, other times it gets stuck if there's a wall between (even if there's a way around it).


So basically, what am I doing wrong? :(

Well, you aren't following the A* method exactly. At the start there should only be one node in your test path (the starting node). You add nodes to the test path only if they don't exist in the closed list.

It's hard to tell what you are doing since it looks like you convert everything to strings, then convert everything back from strings.

You could take a look at this project

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.