Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Coder's Corner Lounge (http://www.programmingforums.org/forum11.html)
-   -   Checking possible moves recursivley (http://www.programmingforums.org/showthread.php?t=14299)

Wizard1988 Oct 30th, 2007 1:15 AM

Checking possible moves recursivley
 
I have a map which is composed of 16x16 squares, There are six types of squares which can have walls on different sides (left + top, top, top+right,right, left+bottom,bottom, bottom+left) Those squares are the obstacles within the map. I also have special squares which move vertically and horizontally. My problem is figuring out how far a certain square can move before it collides with a block. I am trying to accomplish this recursively but I am not sure how I can do this since the values are increasing and not decreasing. I am not sure how to test for the base case where the function should return.

I have spent a couple hours trying to figure this out, and I am simply stumped. Any hints will be really appreciated.

Arevos Oct 30th, 2007 6:39 AM

Re: Checking possible moves recursivley
 
Just keep recursing until:

a) You hit a wall
b) You run out of unexplored spaces
c) You hit some internal limit on how far the function should explore

Presumably you're only interested in places you haven't been before, so keep a record of what places have been explored. If your map is 16 by 16, there's only 255 additional places your special squares can move to.

SamReidHughes Oct 30th, 2007 11:30 AM

Re: Checking possible moves recursivley
 
I presume your data structure describing barriers is a set of edges, where an edge is an ordered pair of square coordinates normalized in sorted order. Right? And you have some kind of function such as "existsBarrier(board, squarecoordinate, squarecoordinate)", right?

This algorithm -- moving a square in one direction until it has to stop -- does not need to be recursive; it could just be done iteratively. So if you write the algorithm recursively, the recursive call would be done in the return statement.

Arevos Oct 30th, 2007 11:45 AM

Re: Checking possible moves recursivley
 
Quote:

Originally Posted by SamReidHughes (Post 136048)
This algorithm -- moving a square in one direction until it has to stop

I rather assumed that the square could move in any horizontal or vertical direction, and that this was more a path-finding problem than a collision detection problem. Otherwise, as you say, you wouldn't need recursion.

Could you explain a little further, Wizard1988? It's not clear what exactly you want to do.

WaltP Oct 30th, 2007 12:42 PM

Re: Checking possible moves recursivley
 
When you enter a square, pass into the function the direction entered from (i.e. if you moved left out of the previous square, this value is right)

Each direction must test the direction entered before calling the function again and skip that call.
Enter a square and, if no wall, go left. When the function returns, go up, then skip right, then go down. At this point you return from the function and end up in the previous square to continue it's journey 'round the horn.

Wizard1988 Oct 30th, 2007 10:00 PM

Re: Checking possible moves recursivley
 
I managed to fix my problem. I had to check the current square as well as the one ahead of it.


All times are GMT -5. The time now is 3:17 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC