From cfa3baf2539c7790eb973113a94b8910372fe89d Mon Sep 17 00:00:00 2001 From: Masahiko AMANO Date: Wed, 2 Mar 2022 23:05:47 +0300 Subject: [PATCH] Recursive opening of empty cells --- Field.cs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/Field.cs b/Field.cs index 13abc93..034ef2c 100644 --- a/Field.cs +++ b/Field.cs @@ -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()); return cells[y * width + x].isMine; } + private void recur_open(int y, int x, List 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++)