Some more recursion...
This commit is contained in:
parent
cfa3baf253
commit
f7ffbebdae
10
Cell.cs
10
Cell.cs
@ -4,8 +4,7 @@
|
||||
{
|
||||
public string value = " ";
|
||||
public bool isMine = false;
|
||||
public bool isOpened = false;
|
||||
private bool isMarked = false;
|
||||
public bool isMarked = false;
|
||||
|
||||
public Cell(bool mine)
|
||||
{
|
||||
@ -17,15 +16,12 @@
|
||||
}
|
||||
public string show()
|
||||
{
|
||||
if (isOpened)
|
||||
return value;
|
||||
else if (isMarked)
|
||||
if (isMarked)
|
||||
return "X";
|
||||
else
|
||||
return "■";
|
||||
return value;
|
||||
}
|
||||
public void setMine() { isMine = true; }
|
||||
public void open() { isOpened = true; }
|
||||
public void mark() { isMarked = true; }
|
||||
public void unmark() { isMarked = false; }
|
||||
public bool isEmpty() { return value == " "; }
|
||||
|
||||
81
Field.cs
81
Field.cs
@ -6,6 +6,7 @@
|
||||
private int height = 0;
|
||||
private int size = 0;
|
||||
private List<Cell> cells = new List<Cell>();
|
||||
private List<Cell> opened = new List<Cell>();
|
||||
public Field(int input_width, int input_height, int nmines)
|
||||
{
|
||||
if (input_width < 0 || input_height < 0)
|
||||
@ -79,7 +80,7 @@
|
||||
Console.Write((char)('A' + y) + " ║");
|
||||
for (int x = 0; x < width; x++)
|
||||
{
|
||||
Console.Write(" " + cells[y * width + x].show());
|
||||
Console.Write(" " + ((opened.Contains(cells[y * width + x]) || cells[y * width + x].isMarked) ? cells[y * width + x].show() : "■"));
|
||||
}
|
||||
Console.WriteLine(" ║");
|
||||
}
|
||||
@ -94,33 +95,71 @@
|
||||
{
|
||||
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]))
|
||||
if (cells[y * width + x].isMine)
|
||||
return true;
|
||||
if (cells[y * width + x].isMarked)
|
||||
return false;
|
||||
if (!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);
|
||||
if (cells[y * width + x].isEmpty())
|
||||
{
|
||||
if (x < width - 1)
|
||||
this.open(y, x + 1);
|
||||
if (x > 0)
|
||||
this.open(y, x - 1);
|
||||
if (y < height - 1)
|
||||
this.open(y + 1, x);
|
||||
if (y > 0)
|
||||
this.open(y - 1, x);
|
||||
if (x < width - 1 && y < height - 1)
|
||||
this.open(y + 1, x + 1);
|
||||
if (x > 0 && y < height - 1)
|
||||
this.open(y + 1, x - 1);
|
||||
if (x < width - 1 && y > 0)
|
||||
this.open(y - 1, x + 1);
|
||||
if (x > 0 && y > 0)
|
||||
this.open(y - 1, x - 1);
|
||||
}
|
||||
}
|
||||
else if (!cells[y * width + x].isEmpty())
|
||||
{
|
||||
int marked = 0;
|
||||
for (int y1 = y - 1; y1 < y + 2; y1++)
|
||||
{
|
||||
if (y1 < 0 || y1 >= height)
|
||||
continue;
|
||||
for (int x1 = x - 1; x1 < x + 2; x1++)
|
||||
{
|
||||
if (x1 < 0 || x1 >= width || y1 == y && x1 == x)
|
||||
continue;
|
||||
if (cells[y1 * width + x1].isMarked)
|
||||
marked++;
|
||||
}
|
||||
}
|
||||
if (marked == Convert.ToInt16(cells[y * width + x].value))
|
||||
for (int y1 = y - 1; y1 < y + 2; y1++)
|
||||
{
|
||||
if (y1 < 0 || y1 >= height)
|
||||
continue;
|
||||
for (int x1 = x - 1; x1 < x + 2; x1++)
|
||||
{
|
||||
if (x1 < 0 || x1 >= width || y1 == y && x1 == x)
|
||||
continue;
|
||||
if (!cells[y1 * width + x1].isMarked && !opened.Contains(cells[y1 * width + x1]))
|
||||
if (this.open(y1, x1))
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
public void openall()
|
||||
{
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
cells[i].open();
|
||||
if (!opened.Contains(cells[i]))
|
||||
opened.Add(cells[i]);
|
||||
}
|
||||
}
|
||||
public void mark(int y, int x) { cells[y * width + x].mark(); }
|
||||
@ -128,7 +167,7 @@
|
||||
public bool check()
|
||||
{
|
||||
for (int i = 0; i < size; i++)
|
||||
if (!cells[i].isMine && !cells[i].isOpened)
|
||||
if (!cells[i].isMine && !opened.Contains(cells[i]))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user