Help with randomizing bunny program movements

Hi,

I have been working on the bunny program excercise from forum topic http://www.cplusplus.com/forum/articles/12974/. I would post this question there, but its been archived. I was hoping that people are still working on the program excercises in that forum who might be able to help me with my code. I have gotten very far into the program and am really close to getting it all to work. At the moment I am struggling with figuring out why my bunnies are not moving randomly and seem to be moving in straight lines after a couple turns. If there is someone who could help me I would be very greatful. I have attached links to all of my program files, 3 header files and 4 .cpp files. I think the problem is somehwere in how the bunnies are being moved or how the baby bunnies are being assigned new coordinates that are adjacent to their mothers. Both of those methods are located within the BunnyManager .cpp.

BunnyManager.cpp -> http://codepad.org/vmK5smbP
BunnyManager.h -> http://codepad.org/SCOJ08nF
Bunny.cpp -> http://codepad.org/A28BMC7c
Bunny.h -> http://codepad.org/ES8hvBvc
Coordinates.cpp -> http://codepad.org/cYNAmt3A
Coordinates.h -> http://codepad.org/7e3zGdss
Source.cpp -> http://codepad.org/pnprWXKS

That was definitely a lot you asked us to do, BUT I did it anyway because you phrased your question very well, stated what the issue was, stated where you think the issue resides, and gave us all the code we could possibly need to help you. And for that, you deserve help. :)

While I haven't examined your program thoroughly enough to definitely say where the error is, one bit of code definitely raised a LOT of red flags.

Firstly, though, congrats on getting this far with the program! I remember when it was first posted and there was a long thread in which people were discussing their solutions and difficulties with this program. Oh, the nostalgia. Sorry, where was I? Ah, red flags.

Take a second look at your set of CheckDirectionIsClear functions in BunnyManager.

I don't know if you have another edge check elsewhere (like in Coordinate). If you do, disregard this paragraph. Those functions should return false if the bunny is at 0 or 79 (not 80!) for either dimension. Instead, they'll return the value of the boolean that was passed as the first argument (which will be true if the check was performed).

The functions only check one coordinate, but not the other. Let's say we're checking if the bunny is blocked moving to the left. That means we're checking if the bunny can move in the -X direction. Assuming the edge check worked and we're comparing the bunny's location with another bunny, two things need to be checked.
One: that the other bunny's Y coordinate isn't the same.
Two: that the other bunny's X coordinate isn't one less than the first bunny's.
If either is true, we can say that the original bunny's movement isn't blocked.

HOWEVER, in your CheckLeftIsClear function, you only check the second, meaning that any bunny that's one column to the left of the original bunny will count as a block. You have similar issues in all the other functions.

I hope that helps in some way. It's entirely possible that this isn't the source of your trouble, though, and if it's not then I'll look again. Good luck with your programming!

-Albatross
Last edited on
Topic archived. No new replies allowed.