443,552 Members | 893 Online
Need help? Post your question and get tips & solutions from a community of 443,552 IT Pros & Developers. It's quick & easy.

# for loops

 P: n/a I have this situation 25 26 ........ 9 10 11 12 13 24 1 2 3 15 23 8 x 4 15 22 7 6 5 16 21 20 19 18 17 I need a way to iterate through that grid in spiral order strating from 1 -- Cheers, Crirus ------------------------------ If work were a good thing, the boss would take it all from you ------------------------------ Nov 20 '05 #1
20 Replies

 P: n/a You allways come up with the interesting ones. What exactly are you trying to do ? are you like trying to go 1,10,9,24,23,8,x,2,11 ( and then where ? ) Or when you say starting from 1 do you mean 9 ? Crirus wrote: I have this situation 25 26 ........ 9 10 11 12 13 24 1 2 3 15 23 8 x 4 15 22 7 6 5 16 21 20 19 18 17 I need a way to iterate through that grid in spiral order strating from 1 -- Best Regards - OHM O_H_M{at}BTInternet{dot}com Nov 20 '05 #2

 P: n/a None of this I want to pick up element 1 first, then 2 the 3 and so on... When one of this fit a particular condition, I want to break out the loops In fact, what I do is this (is about the game I work on) I have a tile in my map(512x512) called X(i,j) I need to test for closer tile to this one that is unoccupied by something...like if a player send a unit right on a tree, I correct the target to a tile right next to that tree Got the ideea? So I need a spiral loop starting from the point and wrapping around it wider and wider -- Cheers, Crirus ------------------------------ If work were a good thing, the boss would take it all from you ------------------------------ "One Handed Man [ OHM# ]" wrote in message news:ew**************@TK2MSFTNGP11.phx.gbl... You allways come up with the interesting ones. What exactly are you trying to do ? are you like trying to go 1,10,9,24,23,8,x,2,11 ( and then where ? ) Or when you say starting from 1 do you mean 9 ? Crirus wrote: I have this situation 25 26 ........ 9 10 11 12 13 24 1 2 3 15 23 8 x 4 15 22 7 6 5 16 21 20 19 18 17 I need a way to iterate through that grid in spiral order strating from 1 -- Best Regards - OHM O_H_M{at}BTInternet{dot}com Nov 20 '05 #3

 P: n/a Crirus, Is your array and number of elements always the same size? If so, my poor solution would be to create a func that pulls that exact element from the array X(1,2) in your case for 1, X(2,2) for 2, X(3,2) for 3, X(3,3) for 4 and so on. Bad solution, I know, but I haven't had my coffee yet. Aiax "Crirus" wrote in message news:#t**************@tk2msftngp13.phx.gbl... None of this I want to pick up element 1 first, then 2 the 3 and so on... When one of this fit a particular condition, I want to break out the loops In fact, what I do is this (is about the game I work on) I have a tile in my map(512x512) called X(i,j) I need to test for closer tile to this one that is unoccupied by something...like if a player send a unit right on a tree, I correct the target to a tile right next to that tree Got the ideea? So I need a spiral loop starting from the point and wrapping around it wider and wider -- Cheers, Crirus ------------------------------ If work were a good thing, the boss would take it all from you ------------------------------ "One Handed Man [ OHM# ]" wrote in message news:ew**************@TK2MSFTNGP11.phx.gbl... You allways come up with the interesting ones. What exactly are you trying to do ? are you like trying to go 1,10,9,24,23,8,x,2,11 ( and then where ? ) Or when you say starting from 1 do you mean 9 ? Crirus wrote: I have this situation 25 26 ........ 9 10 11 12 13 24 1 2 3 15 23 8 x 4 15 22 7 6 5 16 21 20 19 18 17 I need a way to iterate through that grid in spiral order strating from 1 -- Best Regards - OHM O_H_M{at}BTInternet{dot}com Nov 20 '05 #4

 P: n/a If you just wanted the closest empty slot in the array, assuming empty = 0, and x, y is the starting point.. Public Function test2(ByVal array As Integer(,), ByVal x As Integer, ByVal y As Integer, ByVal offset As Integer) As Point For ix As Integer = Math.Max(0, x - offset) To Math.Min(UBound(array, 1), x + offset) For iy As Integer = Math.Max(0, y - offset) To Math.Min(UBound(array, 2), y + offset) If iy = y And ix = x Then Else If array(ix, iy) = 0 Then Return New Point(ix, iy) End If End If Next Next If x - offset <= 0 And x + offset >= UBound(array, 1) And y - offset <= 0 And y + offset >= UBound(array, 2) Then Return Nothing End If Return test2(array, x, y, offset + 1) End Function you'll have to test for nothing in case there is not one found.. and it doesn't quite do it in your circular order Caution.. I haven't tested this so it might be complete b*****ks HTH "Crirus" wrote in message news:#t**************@tk2msftngp13.phx.gbl... None of this I want to pick up element 1 first, then 2 the 3 and so on... When one of this fit a particular condition, I want to break out the loops In fact, what I do is this (is about the game I work on) I have a tile in my map(512x512) called X(i,j) I need to test for closer tile to this one that is unoccupied by something...like if a player send a unit right on a tree, I correct the target to a tile right next to that tree Got the ideea? So I need a spiral loop starting from the point and wrapping around it wider and wider -- Cheers, Crirus ------------------------------ If work were a good thing, the boss would take it all from you ------------------------------ "One Handed Man [ OHM# ]" wrote in message news:ew**************@TK2MSFTNGP11.phx.gbl... You allways come up with the interesting ones. What exactly are you trying to do ? are you like trying to go 1,10,9,24,23,8,x,2,11 ( and then where ? ) Or when you say starting from 1 do you mean 9 ? Crirus wrote: I have this situation 25 26 ........ 9 10 11 12 13 24 1 2 3 15 23 8 x 4 15 22 7 6 5 16 21 20 19 18 17 I need a way to iterate through that grid in spiral order strating from 1 -- Best Regards - OHM O_H_M{at}BTInternet{dot}com Nov 20 '05 #6

 P: n/a I need a flexible size of the "range" -- Cheers, Crirus ------------------------------ If work were a good thing, the boss would take it all from you ------------------------------ "aiax" wrote in message news:%2****************@tk2msftngp13.phx.gbl... Crirus, Is your array and number of elements always the same size? If so, my poor solution would be to create a func that pulls that exact element from the array X(1,2) in your case for 1, X(2,2) for 2, X(3,2) for 3, X(3,3) for 4 and so on. Bad solution, I know, but I haven't had my coffee yet. Aiax "Crirus" wrote in message news:#t**************@tk2msftngp13.phx.gbl... None of this I want to pick up element 1 first, then 2 the 3 and so on... When one of this fit a particular condition, I want to break out the loops In fact, what I do is this (is about the game I work on) I have a tile in my map(512x512) called X(i,j) I need to test for closer tile to this one that is unoccupied by something...like if a player send a unit right on a tree, I correct the target to a tile right next to that tree Got the ideea? So I need a spiral loop starting from the point and wrapping around it wider and wider -- Cheers, Crirus ------------------------------ If work were a good thing, the boss would take it all from you ------------------------------ "One Handed Man [ OHM# ]" wrote in message news:ew**************@TK2MSFTNGP11.phx.gbl... You allways come up with the interesting ones. What exactly are you trying to do ? are you like trying to go 1,10,9,24,23,8,x,2,11 ( and then where ? ) Or when you say starting from 1 do you mean 9 ? Crirus wrote: > I have this situation > > 25 26 ........ > 9 10 11 12 13 > 24 1 2 3 15 > 23 8 x 4 15 > 22 7 6 5 16 > 21 20 19 18 17 > > > I need a way to iterate through that grid in spiral order strating > from 1 -- Best Regards - OHM O_H_M{at}BTInternet{dot}com Nov 20 '05 #7

 P: n/a I need it in exact that order because that way I can select closest point free I think I may do it using a loop to increase the range to next level and 4 loops to extract each side of the square So first time range is 1 and I search within inner square next I increase range by 1 and looping accordingly on second square border around my spot so first loop extract 123 second loop extract 6 7 5 now on columns third loop will extract 8 forth loop will extract 4 increase radius by 1 reenter the 4 loops and extract 9, 10, 11,12 ,13 etc. It may work, but still need to figure out the loops :) - -- Cheers, Crirus ------------------------------ If work were a good thing, the boss would take it all from you ------------------------------ "Rigga" wrote in message news:fJ*********************@stones.force9.net... If you just wanted the closest empty slot in the array, assuming empty = 0, and x, y is the starting point.. Public Function test2(ByVal array As Integer(,), ByVal x As Integer, ByVal y As Integer, ByVal offset As Integer) As Point For ix As Integer = Math.Max(0, x - offset) To Math.Min(UBound(array, 1), x + offset) For iy As Integer = Math.Max(0, y - offset) To Math.Min(UBound(array, 2), y + offset) If iy = y And ix = x Then Else If array(ix, iy) = 0 Then Return New Point(ix, iy) End If End If Next Next If x - offset <= 0 And x + offset >= UBound(array, 1) And y - offset <= 0 And y + offset >= UBound(array, 2) Then Return Nothing End If Return test2(array, x, y, offset + 1) End Function you'll have to test for nothing in case there is not one found.. and it doesn't quite do it in your circular order Caution.. I haven't tested this so it might be complete b*****ks HTH "Crirus" wrote in message news:#t**************@tk2msftngp13.phx.gbl... None of this I want to pick up element 1 first, then 2 the 3 and so on... When one of this fit a particular condition, I want to break out the loops In fact, what I do is this (is about the game I work on) I have a tile in my map(512x512) called X(i,j) I need to test for closer tile to this one that is unoccupied by something...like if a player send a unit right on a tree, I correct the target to a tile right next to that tree Got the ideea? So I need a spiral loop starting from the point and wrapping around it wider and wider -- Cheers, Crirus ------------------------------ If work were a good thing, the boss would take it all from you ------------------------------ "One Handed Man [ OHM# ]" wrote in message news:ew**************@TK2MSFTNGP11.phx.gbl... You allways come up with the interesting ones. What exactly are you trying to do ? are you like trying to go 1,10,9,24,23,8,x,2,11 ( and then where ? ) Or when you say starting from 1 do you mean 9 ? Crirus wrote: > I have this situation > > 25 26 ........ > 9 10 11 12 13 > 24 1 2 3 15 > 23 8 x 4 15 > 22 7 6 5 16 > 21 20 19 18 17 > > > I need a way to iterate through that grid in spiral order strating > from 1 -- Best Regards - OHM O_H_M{at}BTInternet{dot}com Nov 20 '05 #8

 P: n/a > Caution.. I haven't tested this so it might be complete b*****ks ROTFLMAO Regards - OHM Rigga wrote: If you just wanted the closest empty slot in the array, assuming empty = 0, and x, y is the starting point.. Public Function test2(ByVal array As Integer(,), ByVal x As Integer, ByVal y As Integer, ByVal offset As Integer) As Point For ix As Integer = Math.Max(0, x - offset) To Math.Min(UBound(array, 1), x + offset) For iy As Integer = Math.Max(0, y - offset) To Math.Min(UBound(array, 2), y + offset) If iy = y And ix = x Then Else If array(ix, iy) = 0 Then Return New Point(ix, iy) End If End If Next Next If x - offset <= 0 And x + offset >= UBound(array, 1) And y - offset <= 0 And y + offset >= UBound(array, 2) Then Return Nothing End If Return test2(array, x, y, offset + 1) End Function you'll have to test for nothing in case there is not one found.. and it doesn't quite do it in your circular order Caution.. I haven't tested this so it might be complete b*****ks HTH "Crirus" wrote in message news:#t**************@tk2msftngp13.phx.gbl... None of this I want to pick up element 1 first, then 2 the 3 and so on... When one of this fit a particular condition, I want to break out the loops In fact, what I do is this (is about the game I work on) I have a tile in my map(512x512) called X(i,j) I need to test for closer tile to this one that is unoccupied by something...like if a player send a unit right on a tree, I correct the target to a tile right next to that tree Got the ideea? So I need a spiral loop starting from the point and wrapping around it wider and wider -- Cheers, Crirus ------------------------------ If work were a good thing, the boss would take it all from you ------------------------------ "One Handed Man [ OHM# ]" wrote in message news:ew**************@TK2MSFTNGP11.phx.gbl... You allways come up with the interesting ones. What exactly are you trying to do ? are you like trying to go 1,10,9,24,23,8,x,2,11 ( and then where ? ) Or when you say starting from 1 do you mean 9 ? Crirus wrote: I have this situation 25 26 ........ 9 10 11 12 13 24 1 2 3 15 23 8 x 4 15 22 7 6 5 16 21 20 19 18 17 I need a way to iterate through that grid in spiral order strating from 1 -- Best Regards - OHM O_H_M{at}BTInternet{dot}com -- Best Regards - OHM O_H_M{at}BTInternet{dot}com Nov 20 '05 #9

 P: n/a The routine should return the closest point, just not in strictly 1,2,3,4,5,6,7,8,9 order.. more like 1,2,3,8,4,7,6,5,9,10,11,12,13,24,14,23,15,22,16,21 ,20,19,18,17 but it should work... also it may be quite slow if the offset becomes large.. even if it doesn't work at least I gave OHM a good laugh :-) "Crirus" wrote in message news:eV**************@TK2MSFTNGP09.phx.gbl... I need it in exact that order because that way I can select closest point free I think I may do it using a loop to increase the range to next level and 4 loops to extract each side of the square So first time range is 1 and I search within inner square next I increase range by 1 and looping accordingly on second square border around my spot so first loop extract 123 second loop extract 6 7 5 now on columns third loop will extract 8 forth loop will extract 4 increase radius by 1 reenter the 4 loops and extract 9, 10, 11,12 ,13 etc. It may work, but still need to figure out the loops :) - -- Cheers, Crirus ------------------------------ If work were a good thing, the boss would take it all from you ------------------------------ "Rigga" wrote in message news:fJ*********************@stones.force9.net... If you just wanted the closest empty slot in the array, assuming empty = 0, and x, y is the starting point.. Public Function test2(ByVal array As Integer(,), ByVal x As Integer, ByVal y As Integer, ByVal offset As Integer) As Point For ix As Integer = Math.Max(0, x - offset) To Math.Min(UBound(array, 1), x + offset) For iy As Integer = Math.Max(0, y - offset) To Math.Min(UBound(array, 2), y + offset) If iy = y And ix = x Then Else If array(ix, iy) = 0 Then Return New Point(ix, iy) End If End If Next Next If x - offset <= 0 And x + offset >= UBound(array, 1) And y - offset <= 0 And y + offset >= UBound(array, 2) Then Return Nothing End If Return test2(array, x, y, offset + 1) End Function you'll have to test for nothing in case there is not one found.. and it doesn't quite do it in your circular order Caution.. I haven't tested this so it might be complete b*****ks HTH "Crirus" wrote in message news:#t**************@tk2msftngp13.phx.gbl... None of this I want to pick up element 1 first, then 2 the 3 and so on... When one of this fit a particular condition, I want to break out the loops In fact, what I do is this (is about the game I work on) I have a tile in my map(512x512) called X(i,j) I need to test for closer tile to this one that is unoccupied by something...like if a player send a unit right on a tree, I correct the target to a tile right next to that tree Got the ideea? So I need a spiral loop starting from the point and wrapping around it wider and wider -- Cheers, Crirus ------------------------------ If work were a good thing, the boss would take it all from you ------------------------------ "One Handed Man [ OHM# ]" wrote in message news:ew**************@TK2MSFTNGP11.phx.gbl... > You allways come up with the interesting ones. What exactly are you trying > to do ? are you like trying to go 1,10,9,24,23,8,x,2,11 ( and then where ? ) > Or when you say starting from 1 do you mean 9 ? > > > > > > > Crirus wrote: > > I have this situation > > > > 25 26 ........ > > 9 10 11 12 13 > > 24 1 2 3 15 > > 23 8 x 4 15 > > 22 7 6 5 16 > > 21 20 19 18 17 > > > > > > I need a way to iterate through that grid in spiral order strating > > from 1 > > -- > Best Regards - OHM > > O_H_M{at}BTInternet{dot}com > > Nov 20 '05 #10

 P: n/a On Wed, 14 Jan 2004 13:45:51 +0200, Crirus wrote: In fact, what I do is this (is about the game I work on) I have a tile in my map(512x512) called X(i,j) I need to test for closer tile to this one that is unoccupied by something...like if a player send a unit right on a tree, I correct the target to a tile right next to that tree It sounds like you're trying to work out the pathfinding AI for a RTS game. I assume the X in the middle of your sample array is the tree? Anyway, here is a link to game related resources. Click the link for pathfinding at the bottom to see several articles. http://www.gameai.com/ Also try this link: http://www.gamedev.net/ HTH -- Chris To send me an E-mail, remove the underscores and lunchmeat from my E-Mail address. Nov 20 '05 #11

 P: n/a Here is another link: http://www.gamedev.net/reference/pro...eatures/astar/ -- Chris To send me an E-mail, remove the underscores and lunchmeat from my E-Mail address. Nov 20 '05 #12

 P: n/a I allready implemented pathfinding and I'm very happy with it Just need some tricks when a player try to send a unit in a spot inaccesible... I will choose a range about 5 tiles around that spot and if I can find a good spot in that range I change the initial target to that tile -- Cheers, Crirus ------------------------------ If work were a good thing, the boss would take it all from you ------------------------------ "Chris Dunaway" wrote in message news:2e*****************************@40tude.net... On Wed, 14 Jan 2004 13:45:51 +0200, Crirus wrote: In fact, what I do is this (is about the game I work on) I have a tile in my map(512x512) called X(i,j) I need to test for closer tile to this one that is unoccupied by something...like if a player send a unit right on a tree, I correct the target to a tile right next to that tree It sounds like you're trying to work out the pathfinding AI for a RTS game. I assume the X in the middle of your sample array is the tree? Anyway, here is a link to game related resources. Click the link for pathfinding at the bottom to see several articles. http://www.gameai.com/ Also try this link: http://www.gamedev.net/ HTH -- Chris To send me an E-mail, remove the underscores and lunchmeat from my E-Mail address. Nov 20 '05 #14