Jump to content
  • Advertisement
Sign in to follow this  
legofreak211

Second item in a linked list?

This topic is 2906 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I created a (while inefficiently programmed) linked list called WALL, and for some reason my function (which should run for each instance in the list). Here is the struct:

struct WALL
{
int x, y;
int height;
WALL *nextWall;
};

I programmed the linked list manually:

void initWalls(WALL* wall)
{
wall->x = 500;
wall->height = 100;
wall->y = 100;
wall->nextWall = new WALL;

wall->nextWall->x = 600;
wall->nextWall->height = 100;
wall->nextWall->y = 0;
wall->nextWall->nextWall = NULL;
}

Finally, I created a function to see whether the character could walk, or would hit the wall (and thus not go, returning false):

bool calcMoveLeft(CHARACTER* mainchar, WALL* wall)
{
if(!wall->passFromRight)
{
if((mainchar->x >= wall->x && mainchar->x - 3 < wall->x)
&& (wall->y <= mainchar->y && wall->height + wall->y > mainchar->y))
{
mainchar->x = (float)wall->x;
return false;
}
}

if(wall->nextWall != NULL)
{
if(!calcMoveLeft(mainchar, wall->nextWall))
return false;
}

return true;
}

I know most of it is unnecessary, but some people are like "Give me all the code or I'm going to eat your brains". All that doesn't work is the last function:

if(wall->nextWall != NULL)
{
if(!calcMoveLeft(mainchar, wall->nextWall))
return false;
}

For some reason, the function does not realize that "wall" points to a second wall! Is there any reason why it skips that code?

Share this post


Link to post
Share on other sites
Advertisement
How do you know it skips the code? Also, have you single-stepped through the code, checking variable values as you go?

Share this post


Link to post
Share on other sites
Quote:
I know most of it is unnecessary, but some people are like "Give me all the code or I'm going to eat your brains".
Not really; people just want to see whatever code is relevant to the problem. (When people generally protest is when someone asks a question that, due to lack of context, is essentially unanswerable.)

Anyway, like Buckeye said, the debugger is your friend here; if you step through the code carefully, it will most likely become clear what's going wrong and where.

Regarding the coding style, I'll go ahead and point out that you're essentially writing C-style code here, and that C++ (which is what you appear to be using) offers a lot of tools that make this kind of thing a lot easier to write and a lot less error-prone. (Note that I'm not saying anything about how you 'should' write your code; I'm just making an observation about how it's written currently.)

Share this post


Link to post
Share on other sites
Original post by jyk
Quote:

Regarding the coding style, I'll go ahead and point out that you're essentially writing C-style code here, and that C++ (which is what you appear to be using) offers a lot of tools that make this kind of thing a lot easier to write and a lot less error-prone. (Note that I'm not saying anything about how you 'should' write your code; I'm just making an observation about how it's written currently.)


Could you name a couple differences that would possibly make my coding style 'better'? I guess im not quite as experienced as I might hope :P

Share this post


Link to post
Share on other sites
Do some debugging!...

also what is mainchar defined as? i'm assuming another struct.

for..
if((mainchar->x >= wall->x && mainchar->x - 3 < wall->x)
&& (wall->y <= mainchar->y && wall->height + wall->y > mainchar->y))

output the values for mainchar x,y and wall x,y and height onto the screen or into a debug file before they are processed by the if... should show what reference you've pulled.

If you supply this to us, we might be able to help more.

Share this post


Link to post
Share on other sites
Okay well I just finished stepping it through a debugger...I don't exactly remember what I changed (oops) but now it works fine.
Thanks for the advice :D I also learned how to debug from this

Share this post


Link to post
Share on other sites
Is there any reason why you aren't just using this :

#include <list>

struct Wall{
Point2 p;
unsigned height;
};

int main(){
std::list<Wall> listOfWalls;
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!