# if statements not working

## Recommended Posts

test

##### Share on other sites
paulecoyote    1065
A couple of observations

1. Try and keep your ball moving physically seperate from your actual rendering. Move things the render things...

MoveThings();
RenderThings();

2. You seem to be using 2 flags for direction... one gowest, and one direction=west. And some array that also has a property of "west". Lots of places for west. Try and simplify.

3. Define your concepts. Do you have a player? What does it need to know? It needs to know if it can move to a square... what would know the answer to that? I'd say a Maze should know if you can move from one MazePosition to the next... and if you ask it MoveWest(Player) it should be able to reply "okay, moved" or "nope, can't move there".

That argument puts the emphisis on the Maze knowing where the Player is and what is possible for the player. Also means a player might not even need to know where it is (does a person always know where it is in a maze?). Perhaps a player only need know it's colour and where it wants to go (in your case if a wall isn't on the west, go north, otherwise go west).

Anyway those are design symantics, you've probably worked that stuff out already.

So summary, do your moving seperate from rendering (do it first - also this means your movement isn't tied in to your computers speed of rendering), try and do one thing then drop out of your function and not get into nested-if hell, make sure you've properly assigned responsibilties to the correct components of your design and not unnecessarily coding around yourself.

##### Share on other sites
thanks paulecoyote for your help, i've taken your comments into account, it still isn't working properly yet, but i tried changing the .west statement to 1 (which means theres a wall there) and the ball did move but it go stuck in a continuous loop. This is the current if statement for if the facing direction is north:

http://img.photobucket.com/albums/v38/bigbrother_200uk/mazeai.jpg

if ((direction == bnorth) && (moving == 0)) {
goeast = 0;
gonorth = 0;
gowest = 0;
gosouth = 0;
if (firstmaze.maze[mazebot1x][mazebot1z].west == 0) {
gowest = 1;
moving = 10;
mazebot1x = mazebot1x - 1;
direction = bwest;
}
else {
if(firstmaze.maze[mazebot1x][mazebot1z].north == 0) {
gonorth = 1;
moving = 10;
mazebot1z = mazebot1z - 1;
direction = bnorth;
}
else {
if(firstmaze.maze[mazebot1x][mazebot1z].east == 0) {
goeast = 1;
moving = 10;
mazebot1x = mazebot1x + 1;
direction = beast;
}
else {
if(firstmaze.maze[mazebot1x][mazebot1z].south == 0) {
gosouth = 1;
moving = 10;
mazebot1z = mazebot1z + 1;
direction = bsouth;
}
}
}
}
}

##### Share on other sites
the problem seems to be that all variables are equal to 1, although the make is created by this during initialise directx:

Maze firstmaze = Maze();
firstmaze.generate();

i then test to see if there is not a wall there, although there always is because the values seem to reset to 1, does the firstmaze object lose its current data when it is used in the render function?

##### Share on other sites
mozie    194
Quote:
 Original post by bigbrother2000i tried changing the .west statement to 1 (which means theres a wall there) and the ball did move but it go stuck in a continuous loop. This is the current if statement for if the facing direction is north:http://img.photobucket.com/albums/v38/bigbrother_200uk/mazeai.jpgif ((direction == bnorth) && (moving == 0)) { goeast = 0; gonorth = 0; gowest = 0; gosouth = 0;**snip**}

okay, I see it stuck in the wall, I looked over the for()s, and I am kinda wondering why there needs to be an
if((direction == b*****) && (moving == 0)).

if(moving==0) {   // find first direction open:     // test your favorite direction first, west IIRC   // try to move}

i assume that moving = 0 when you hit a wall, so i kinda think that it shouldnt matter what direction we _were_ moving, as long as the next step in taken in our favorite direction (or attempted first, like your for nest).

not trying to nit-pick, but you can also do:

mazebot1x -= 1;

in place of the

mazebot1x = mazebot1x - 1;

if you have a:
if ((direction == bnorth) && (moving == 0)) {

for each direction (bnorth,bsouth,beast,bwest), it could be
a mistake in the code there too, because they were not posted here.

maybe want to look on google for maze solving algorythims, some are fast, some are for show.

Good Luck!

PS. sorry about the lack of capitol letters.

##### Share on other sites
All I see is 0's, lots and lots of 0's, like no matter what direction you're facing I think is always 0.

First thing I'd do is press <Ctrl + Alt + Delete> to bring up task manager and check for any memory leaks and/or CPU being at 100%, it's likely if this is the case your program may stop responding thus not do what it's told let alone understand any incoming window messages.

Depending on exactly where this code is if it were VB.Net I'd say use Application.DoEvents() and try some good old fashioned error handling to try and catch an unknown exception.

But you're close so don't give up.

##### Share on other sites
thanks guys for helping, i'm going to look into them, although i commented out the whole algorithm and replaced it with this. This should turn the ball red if there is not a wall in the current cell, however it never changes, as soon as i turn any of these values to 1 it changes straight away. This is impossible as the maze is generated using the depth first algorithm, all cells are visited and have at least one wall knocked down. So is my firstmaze losing its initialised values and reseting all the ones to 1 like in the constructor, as this happens no matter which cell the bot is placed in.

if (firstmaze.maze[mazebot1x][mazebot1z].north == 0) {
m_pSphere2->SetTexture("red.jpg");
}
else {if(firstmaze.maze[mazebot1x][mazebot1z].east == 0) {
m_pSphere2->SetTexture("red.jpg");
}
else {if(firstmaze.maze[mazebot1x][mazebot1z].south == 0) {
m_pSphere2->SetTexture("red.jpg");
}
else {if(firstmaze.maze[mazebot1x][mazebot1z].west == 0) {
m_pSphere2->SetTexture("red.jpg");
}
}
}
}

##### Share on other sites
paulecoyote    1065
You ever thought about munging that into a single int?

0 = no direction
1 = north
2 = east
4 = south
8 = west

Then say if you could possibly more north and east it would be

possibleDirections = north | east;

then you could test possibleDirections using the & operator. You can also knock down specific walls by using xor to flip a bit, or using not.

Not that it really matters these days as an int is nothing anyway, but it might interest you as far as another techinque goes.

Probably should just get it working first though ;-)

##### Share on other sites
Finally, after many hours (5), its working, one of the biggest problems was that this was not in the class initialisation but instead in the DirectX initialisation which meant my algorithm was based on a maze which didn't exist which is why all the walls were set to 1.

Maze firstmaze = Maze();

thanks to everyone who helped out, i really appreciate it, heres a mini movie of it working, (red sphere - robot, grey - player), no collision detection on players object yet.
www.davward.com/mazeworking.wmv

##### Share on other sites
paulecoyote    1065
well done! [grin]
Looked at your video by the way, that's pretty damn cool [grin]

##### Share on other sites
thanks paulecoyote and everyone else who helped out, sure i'll experience many other problems along the way, now i know where to get help :)