Hey there,
I'm building a simple 2D game. The grid is a 2D int array which is populated with 0's. The 0 means a unit can move to that tile and any other number means you can't, let's call it a 1 though. The player places structures which set the value in that row and column to 1 which means a unit can't move there.
I'm wanting to calculate the fastest way for a unit to move through this kind of grid. The grid is 10x15 but let's say it's 5x5:
The unit will start where the 5 is. The fastest route would be to go up and left, and the longest but most obvious would be to go left, down and left. How would I go about navigating my 2D array and finding the (first) quickest path?
This is something I've never dealt with before so I'm in new territory here. I've spent a couple of hours trying to figure this out and so far I've thought of this:
Have a "pathsArray" and in some kind of loop add every possible combination path which makes it to the end (i.e, isn't blocked) to an array and then add that array to pathsArray.
Then, all I have to do is choose from the arrays in pathsArray and pick the shortest one. This is my code so far:
I'm not exactly sure where to start. Is two for loops the correct way to go about this? Or should I use a while loop? Oi! This is far above my head. Would appreciate ANY input! Thanks.
I'm building a simple 2D game. The grid is a 2D int array which is populated with 0's. The 0 means a unit can move to that tile and any other number means you can't, let's call it a 1 though. The player places structures which set the value in that row and column to 1 which means a unit can't move there.
I'm wanting to calculate the fastest way for a unit to move through this kind of grid. The grid is 10x15 but let's say it's 5x5:
Code:
(0/1/ 2/ 3/ 4 column)
0, 0, 0, 0, 0 (row 0)
0, 1, 1, 1, 0 (row 1)
0, 1, 0, 0, 5 (row 2)
0, 1, 0, 1, 0 (row 3)
0, 0, 0, 1, 0 (row 4)
The unit will start where the 5 is. The fastest route would be to go up and left, and the longest but most obvious would be to go left, down and left. How would I go about navigating my 2D array and finding the (first) quickest path?
This is something I've never dealt with before so I'm in new territory here. I've spent a couple of hours trying to figure this out and so far I've thought of this:
Have a "pathsArray" and in some kind of loop add every possible combination path which makes it to the end (i.e, isn't blocked) to an array and then add that array to pathsArray.
Then, all I have to do is choose from the arrays in pathsArray and pick the shortest one. This is my code so far:
Code:
NSMutableArray *pathsArray = [[NSMutableArray alloc] init]; //add each array with the paths in it to this list and then look for the one with the fewest movements
for (int i = colMax; i > 0; i--) {
for (int k = rowMax; k > 0; k--) {
//start searching from the very bottom right of the grid, going up
}
}
I'm not exactly sure where to start. Is two for loops the correct way to go about this? Or should I use a while loop? Oi! This is far above my head. Would appreciate ANY input! Thanks.