do/while loop - infinite

Ok, I really need a fresh set of eyes on this. I've been trying to figure it out for too long now.

So, here's the code and an explanation:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void Robot::spawnRobot()
{
	int spawnX, spawnY;
	bool spawnable = false;

	do
	{
		spawnX = rand() % WIDTH;
		spawnY = rand() % HEIGHT;

		if( arena[ spawnX ][ spawnY ] == ' ' )
			spawnable = true;

	}while( ! spawnable );

	displayRobot( spawnX, spawnY );
}


Here's the constants/globals:
1
2
3
#define WIDTH 80
#define HEIGHT 24
static char arena[ WIDTH ][ HEIGHT ];


The arena array is filled by getline( file, input, '\n' ), then seperated in to single chars.

The problem here is that, the above loop is infinite. The loop should finally find a 'free space' to spawn. I've even set it up so that there is only a border around the console screen, for the robot to spawn in. I belive it's the check that is causing the problem:
if( arena[ spawnX ][ spawnY ] == ' ' )

But... I really can't see why it's causing a problem, as I use a similar check to get the arena file in to the array:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
switch( input[ j ] )
	{
	case '0':
		arena[ j ][ i ] = ( char )219;
		break;

	case ' ':
		arena[ j ][ i ] = ' ';
		break;

	case '1':
		arena[ j ][ i ] = ( char )254;
		break;

	default:
		arena[ j ][ i ] = ' ';
		break;
				}


Any help would be greatly appreciated!
Have you confirmed that there are actually spaces in the matrix? There may be a bug in the way you fill the matrix, or read the data.
This is a poor method to pick an element with a give value, anyway. It's better to make a list of all the elements that match the constraint and pick one at random from there. This way you're guaranteed to find something at the first try (as long as the list is not empty).
I couldn't confirm any spaces within the matrix.
So I tried changing:
static char arena[ WIDTH ][ HEIGHT ];

to:
static int arena[ WIDTH ][ HEIGHT ];

Even checking for if( arena[ spawnX ][ spawnY ] == 1 ) doesn't return true at any point.

This is how I am filling the matrix:
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
	std::fstream fArena( "Arena.txt" );

	if( fArena.is_open() )
	{
		std::string input;

		for( int i = 0; i < HEIGHT; ++i )
		{
			std::getline( fArena, input, '\n' );

			for( int j = 0; j < WIDTH; ++j )
			{
				switch( input[ j ] )
				{
					case '0':
						arena[ j ][ i ] = 0;
						break;

					case ' ':
						arena[ j ][ i ] = 1;
						break;

					case '1':
						arena[ j ][ i ] = 2;
						break;

					default:
						arena[ j ][ i ] = 1;
						break;
				}
				std::cout << arena[ j ][ i ];
			}
		}
	
		fArena.close();


I've never used lists before. I did try a while ago, but I never understood them. I should probably have a look in to them again.
There's probably something screwing up with your I/O. std::fstream opens by default for both input and output, so even if the file doesn't exist, it won't fail to open unless it can't create a new file. Change fArena to std::ifstream. Also check what your working directory is set to. If you're running from an IDE's debugger, it will probably be set to something totally useless. Alternatively, you could use an absolute path to the file.

When I say list, I mean it in the ordinary sense of the word (an enumeration of items). An actual linked list wouldn't be suitable for this because they don't have random access.
Maybe you should start debugging, rather than just reading the code. The obvious first thing to do is stick something like

std::cout <<arena[ spawnX ][ spawnY ];

in the while loop in the spawnRobot() function.

Topic archived. No new replies allowed.