This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.

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.

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!

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

Seamus GalIagher
Greenhorn

Joined: Aug 08, 2005
Posts: 20

posted

0

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? thanks

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.

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
Greenhorn

Joined: Aug 08, 2005
Posts: 20

posted

0

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.

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.