Recursive opening of empty cells

This commit is contained in:
Masahiko AMANO 2022-03-02 23:05:47 +03:00
parent 38f3e4db4d
commit cfa3baf253

View File

@ -95,8 +95,27 @@
if (y < 0 || y >= height || x < 0 || x >= width)
throw new Exception("Coordinates out of the field!");
cells[y * width + x].open();
if (cells[y * width + x].isEmpty())
this.recur_open(y, x, new List<Cell>());
return cells[y * width + x].isMine;
}
private void recur_open(int y, int x, List<Cell> opened)
{
if (!cells[y * width + x].isMine)
cells[y * width + x].open();
if (cells[y * width + x].isEmpty() && !opened.Contains(cells[y * width + x]))
{
opened.Add(cells[y * width + x]);
if (x < width - 1)
this.recur_open(y, x + 1, opened);
if (x > 0)
this.recur_open(y, x - 1, opened);
if (y < height - 1)
this.recur_open(y + 1, x, opened);
if (y > 0)
this.recur_open(y - 1, x, opened);
}
}
public void openall()
{
for (int i = 0; i < size; i++)