Код: Выделить всё
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
Код: Выделить всё
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
В этом случае размер равен 8, а массив цветов изменяется от 0 до 9 в каждом измерении, хотя я использую только от 1 до 8. Я сделал это для того, чтобы мне не приходилось использовать условные выражения при просмотре ячейки рядом с той, которую я проверяю.
Код: Выделить всё
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
}
Я упустил что-то очевидное?