Random growth algorithm not working as intended

1 day ago 1
ARTICLE AD BOX

For a game I'm working on, I'm starting with an 8x8 int array with all values zero. Then I'm randomly placing a single non-zero value in each row/column in such a way that there's only a single non-zero value in each row and a single non-zero value in each column. Like so:

0 1 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 3 0 4 0 0 0 0 0 0 0 0 0 5 0 0 0 0 0 0 0 0 0 0 0 0 6 0 0 0 0 0 7 0 0 0 0 0 8 0 0 0 0

That part works great. I get a different grid every time I generate one. But then I'm trying to extend each of the numbers randomly into the zeros so that I get 8 regions, each one populated by one of the numbers. Here's an example of one I've gotten:

4 1 1 5 2 2 2 3 4 1 1 5 2 2 3 3 4 1 5 5 2 2 3 3 4 4 5 5 5 2 3 6 4 4 5 5 5 2 2 6 4 4 5 5 5 2 2 6 4 4 5 8 5 7 2 6 4 4 4 8 8 7 2 6

Notice that while the 1 is able to extend downwards and the 8 is able to extend upwards, the 6, which is in the farthest column on the right, never escapes that column. And the 4, which is in the first column on the left, never allows any other number into that column. Here's the function that I use to do the extensions. I keep a counter of how many 0s there are, and loop this function until they're all gone:

In this case, size is 8, and the color array goes from 0 to 9 in each dimension, even though I'm only using from 1 to 8. I did that so that I didn't have to use conditionals when looking at the cell next to the one I'm checking.

private void ExpandColors() { //1 is up, 2 is right, 3 is down, 4 is left int cell = rand.Next(1, size * size); string coord = CellToCoords(cell); string[] c = coord.Split('ÿ'); int row = Convert.ToInt16(c[0]); int col = Convert.ToInt16(c[1]); if (color[row, col] > 0) { switch (rand.Next(1, 4)) { case 1: if (row == 1) return; //can't look up if (color[row - 1, col] == 0) { color[row - 1, col] = color[row, col]; uncolored--; return; } break; case 2: if (col == size) return; //can't look right if (color[row, col + 1] == 0) { color[row, col + 1] = color[row, col]; uncolored--; return; } break; case 3: if (row == size) return; //can't look down if (color[row + 1, col] == 0) { color[row + 1, col] = color[row, col]; uncolored--; return; } break; case 4: if (col == 1) return; //can't look left if (color[row, col - 1] == 0) { color[row, col - 1] = color[row, col]; uncolored--; return; } break; } } } private string CellToCoords(int cell) { int col = cell % size; if (col == 0) col = size; int row = (cell - col) / size + 1; return row.ToString() + "ÿ" + col.ToString(); // so 2, 5 would be 2ÿ5 and can easily be split }

I've double checked that CellToCoords works correctly. And there doesn't seem to be any functional difference in ExtendColors that would make any difference between horizontal and vertical. And yet I still have no issue with values in the top and bottom rows extending normally, and I still do have issues with values in the leftmost and rightmost columns extending normally. I've run it dozens of times, and the same anomalous behavior in the leftmost and rightmost columns occurs every single time.

Am I missing something obvious?

Read Entire Article