Assistance Needed: 2D Path Finding

closed account (zb0S216C)
Since I want to learn how to develop AI, I'm starting now with basic 2D path finding. So far I have this code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>

//-------------------------
// Legend:
//     0: Empty space
//     1: Route node
//     2: End (finish line)
//-------------------------
int Grid[ 4 ][ 4 ] = 
{
	{ 0, 0, 1, 0 },
	{ 0, 1, 1, 0 },
	{ 0, 1, 0, 0 },
	{ 0, 2, 0, 0 }
};

#define SPACE "[   ]" 

bool Finished( false );

void FindNextPos( void )
{
	for( int Row( 0 ); Row < 4; Row++ )
	{
		for( int Col( 0 ); Col < 4; Col++ )
		{
			if( Grid[ Row ][ Col ] == 1 )
			{
				Grid[ Row ][ Col ] = 0;

				// Print the coords.
				std::cout << "Node at[ r ][ c ]: " << ( Row + 1 ) << " " << ( Col + 1 ) << std::endl;
				return;
			}

			else if( Grid[ Row ][ Col ] == 2 )
			{
				Finished = true;
				std::cout << "finish line at[ r ][ c ]: " << ( Row + 1 ) << " " << ( Col + 1 ) << std::endl;
				
				return;
			}
		}
	}
}

void PrintRoute( void )
{
	for( int Row( 0 ); Row < 4; Row++ )
	{
		for( int Col( 0 ); Col < 4; Col++ )
		{
			if( Grid[ Row ][ Col ] == 0 )
				std::cout << SPACE;

			else
				std::cout << "[ 1 ]";
		}

		std::cout << std::endl;
	}
}

int main( )
{
	PrintRoute( );

	while( !Finished )
		FindNextPos( );

	std::cin.get( );
	return 0;
}

When the finishing node is found, it prints a weird result. Can anybody see why?

Thanks :)

P.S: I'm sorry that the spacing is a little wide but that's how it appears when copied directly from VCE '10. [Note: indent is set to 4 spaces].

Wazzak
What weird result are you getting? I'm getting what I expect which is:
finish line at[ r ][ c ]: 4 2


When you say AI do you mean robotics? This was something I was interested if for a long time and something I still might get back into.
closed account (zb0S216C)
Computergeek01 wrote:
What weird result are you getting?

Originally, the second for loop of FindNextPos( ) was this: for( int Col( 4 ); Col > 0; Col-- ).
However, If you write it that way, you get: Node at[ r ][ c ]: 4 5 which is unexpected. So I changed the loop to what it's current at. Any thoughts on this?

Computergeek01 wrote:
When you say AI do you mean robotics?

AI in general I suppose. Since I'm starting out, I'm not leaning towards any specific types of AI.

Wazzak
I don't know, I flipped the for loop like you said and it still works for me. Sorry.
closed account (zb0S216C)
Hmmm... Perhaps it's me that's the issue. Thanks for looking anyway, Computergeek.

Wazzak
Topic archived. No new replies allowed.