• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Anyone know an algorithm for this?

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

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.

thankyou,

seamus.
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

https://coderanch.com/t/35083/Programming/June-Newsletter-Puzzle
 
Seamus GalIagher
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

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!

Seamus
 
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just use a simple for loop:

And then replace the -1 and +1 with -range and +range.
 
Seamus GalIagher
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

seamus
[ August 15, 2005: Message edited by: Seamus GalIagher ]
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Seamus GalIagher
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

0000000
0xxxxx0
0xxxxx0
0xxXxx0
0xxxxx0
0xxxxx0
0000000

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.

seamus
reply
    Bookmark Topic Watch Topic
  • New Topic