Thread: Too Many Loops young man!

  1. #1
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472

    Too Many Loops young man!

    In a pathfinding routine i am working on i would like to chart the estimated distance(in movement cost) from each square on my search area to the given target node. I am doing this before doing the actual search to initialise the search area ahead of time.

    * horizontal and vertical steps only
    * Each step costs 10movement points.
    * all squares are valid steps

    The target can be anywhere but for now i will say its in the centre of a 20 x 20 square area.

    My current solution to this problem uses four (while) loops
    each loop works outwards from the target node to a corner of the search area incrementing the cost by 10 on each move and storing in Hscore[Down][Across]

    1st loop > back and up to top left
    2nd > forward and up to top right
    3rd > forward and down to bottom right
    4th > back and down to bottom left

    I am sure that i can just use one loop to achieve this! using a little calculation in the loop i would like to just sweep the search area from top left to bottom right adding the value as i go using a sum of some kind,
    Ihad a quick go at a couple of versions but think i was getting my precedence in wrong order becasue it was producing all kinds of minus numbers.

    any ideas??

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    In your 20 x 20 grid, every row that a sqr is on = 10 * abs(target's row - sqr's row) + 10 * abs(target's column - sqr's column), imo.

    One while or for loop, should do fine.

  3. #3
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    Whee! Cheers, that sounds like just the ticket.... here let me try.... my productivity in work is about to drop a little methinks..

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    W-H-A-T Productivity?? j/k

  5. #5
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    aww noo man! that kinda worked but not the way i mean,i think i need to tweak this a bit, i will post in bit with desired output...

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Dude! Been 40 minutes. Are you now in the Geritol Time Zone, or what?

  7. #7
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    haha, just bin tooooo busy for play! doing it now tho

  8. #8
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    ok so for example, this is how its supposed to look if i printed out output of array contents,

    i have just compiled this in work but the setup is not same as at home and am wasting time messing about getting display right, so am just going to type example for array 5 x 5 grid


    40 30 20 30 40
    30 20 10 20 30
    20 10 0 10 20
    30 20 10 20 30
    40 30 20 30 40

    as you can see the target square in red has a value of 0, and all the other surrounding squares increase the movement cost outwards in all directions as the distance increases, (bearing in mind the target square could be placed anywhere)

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by rogster001 View Post
    ok so for example, this is how its supposed to look if i printed out output of array contents,

    i have just compiled this in work but the setup is not same as at home and am wasting time messing about getting display right, so am just going to type example for array 5 x 5 grid


    40 30 20 30 40
    30 20 10 20 30
    20 10 0 10 20
    30 20 10 20 30
    40 30 20 30 40

    as you can see the target square in red has a value of 0, and all the other surrounding squares increase the movement cost outwards in all directions as the distance increases, (bearing in mind the target square could be placed anywhere)
    In your 20 x 20 grid, every row that a sqr is on = 10 * abs(target's row - sqr's row) + 10 * abs(target's column - sqr's column), imo.
    Yep, perfect!

    e.g., upper left corner is 0, 0, let's say. Target is 2,2.

    Upper left sqr = 10 * abs(2 - 0) + 10 * abs(2 - 0)
    sqr = 10 * 2 + 10 * 2
    sqr = 20 + 20
    sqr = 40

    If your array numbering begins with 1, the numbers change, but the value of the same sqr is still going to be 40.

  10. #10
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    haha, super, will give that a go then, thanks for having a look, this pesky work is getting in the way of me doing it right now!

  11. #11
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    haha!!!! Man that is an absolute peach, works a treat! am chuffed, i love neat fixes like that! that will be goin straight in a function, an i can then see the wood for the trees and start on the problem of 'parent' squares...erm...!

    cheers!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. casting - pointer to pointer
    By terminator in forum C Programming
    Replies: 56
    Last Post: 04-23-2008, 12:54 AM
  2. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  3. we of the cage
    By mithrandir in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 08-21-2002, 10:14 AM
  4. Gender Humour Thread
    By stevey in forum A Brief History of Cprogramming.com
    Replies: 39
    Last Post: 06-01-2002, 01:12 PM