File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Anyone know an algorithm for this? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Anyone know an algorithm for this?" Watch "Anyone know an algorithm for this?" New topic

Anyone know an algorithm for this?

Seamus GalIagher

Joined: Aug 08, 2005
Posts: 20

This is just a simple program that finds the adjcanet squares ofa selected square. As the range increases, so will the number of LOC, using my current method. anyone any ideas how to implement this in a nested loop?

"movephase" is used afterwards to change the graphics.


Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
not sure what you're doing, but is this like a wordfinder?
(where you go off in all directions to find the word)

if so, you might get some good stuff from this link
Seamus GalIagher

Joined: Aug 08, 2005
Posts: 20

i asked a similar question a coupe of days ago, and it could have been you that gave me an idea to work on - but i deleted post by mistake.

its part of a turnbased game. x + y is the position of the current unit, in a grid eg 10x10. Then each surrounding square changes graphic and attributes to allow movemement to that square. try..catch.. stops arrayoutofbounds exceptions. However, the above code is for one square each way. If i wanted 5 squares each way, without an algorithm, i would need to type out every single square, and have a try..cathc for every square also!

Stuart Gray
Ranch Hand

Joined: Apr 21, 2005
Posts: 410
Just use a simple for loop:

And then replace the -1 and +1 with -range and +range.
Seamus GalIagher

Joined: Aug 08, 2005
Posts: 20
hi and thnaks for reply!

i understand your loop, and have used it. However, there is a "bug" in it, although i could fix it with trial and error. Basically, the loop goes in a straight line - ie down, left, down/left etc - however, it does not go to squares that are not in a straight line - such as down two and left one. any ideas?

[ August 15, 2005: Message edited by: Seamus GalIagher ]
Joel McNary

Joined: Aug 20, 2001
Posts: 1824

Well, you will definately want some sort of loop. But you don't want to loop over the squares -- use the loop to perform mathematical function to locate the squares.

And the checkSquareAt method is where you do whatever logic you need to with the square. And, of course, you could parameterize the 3 so that you cound find sqaures 5 moves away. And this also assumes only one turn -- if you go 1 square right, 1 square up, and 1 square left, this doesn't find that square. (There's a different algorithm for that, but that's an exercise left to the reader).

Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I think I did this once in Pascal with an array of deltas:

{0,-1},{1,-1},{1,0},{1,1} ...

Then you can loop through the array to compute the x,y of each neighbor. I was dropping in tiles with plumbing on them and they edges of the tile that had a pipe sticking out had to mate with a pipe on the neighbor. I'm pretty sure I did this only once at setup time and gave each square a collection of pointers to its neighbors.

Can you get any mileage that way? I'm not sure what you mean by 5 squares each way ... do you change the image potentially 100 squares all at once? Or just try to validate a move?
[ August 15, 2005: Message edited by: Stan James ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Seamus GalIagher

Joined: Aug 08, 2005
Posts: 20
hi and thanks for replies and help!

Stan, i think i worded my question wrong. when i said range 5, i didnt mean to go 5 squares each direction. "range 5" means a radius of 5 squares.

so a range of 2 is this:


X is the centre. x is a square within a range of 2.

I think i have worked it out.

-I do a double loop.
-x = square column
-y = square row.
-r = range.
- a range of "2" takes 2 from both x + y to get a lower.
- a range of "2" adds 2 to both x + y to get upper.
-the loops goes through each square from lower to upper.

well thats the theory anyway.

I agree. Here's the link:
subject: Anyone know an algorithm for this?
jQuery in Action, 3rd edition