Refactoring

This commit is contained in:
Masahiko AMANO
2022-03-03 01:12:28 +03:00
parent ae8de489e3
commit 5f408f5b11
4 changed files with 68 additions and 73 deletions
+40 -44
View File
@@ -2,11 +2,11 @@
{
internal class Field
{
private int width = 0;
private int height = 0;
private int size = 0;
private List<Cell> cells = new List<Cell>();
private List<Cell> opened = new List<Cell>();
private readonly int width = 0;
private readonly int height = 0;
private readonly int size = 0;
private readonly List<Cell> cells = new();
private readonly List<Cell> opened = new();
public Field(int input_width, int input_height, int nmines)
{
if (input_width < 0 || input_height < 0)
@@ -23,23 +23,19 @@
height = input_height;
size = width * height;
cells = new List<Cell>();
for (int i = 0; i < size; i++) // initializing field
{
for (int i = 0; i < size; i++)
cells.Add(new Cell(nmines-- > 0));
}
Random rnd = new Random();
for (int i = 0; i < size; i++) // shuffling field
Random rnd = new();
for (int i = 0; i < size; i++)
{
int dest = rnd.Next(size);
Cell tmp = cells[i];
cells[i] = cells[dest];
cells[dest] = tmp;
(cells[i], cells[dest]) = (cells[dest], cells[i]);
}
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
if (cells[y * width + x].isMine)
if (cells[y * width + x].IsMine)
{
for (int y1 = y - 1; y1 <= y + 1; y1++)
{
@@ -50,19 +46,19 @@
if (x1 < 0 || x1 >= width)
continue;
Cell current = cells[y1 * width + x1];
if (current.isMine)
if (current.IsMine)
continue;
if (current.value == " ")
current.value = "1";
if (current.Value == " ")
current.Value = "1";
else
current.value = Convert.ToString(Convert.ToInt16(current.value) + 1);
current.Value = Convert.ToString(Convert.ToInt16(current.Value) + 1);
}
}
}
}
}
}
public void draw()
public void Draw()
{
Console.Write(" ");
for (int i = 0; i < width;)
@@ -80,7 +76,7 @@
Console.Write((char)('A' + y) + " ║");
for (int x = 0; x < width; x++)
{
Console.Write(" " + ((opened.Contains(cells[y * width + x]) || cells[y * width + x].isMarked) ? 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(" ║");
}
@@ -91,38 +87,38 @@
}
Console.WriteLine("╝");
}
public bool open(int y, int x)
public bool Open(int y, int x)
{
if (y < 0 || y >= height || x < 0 || x >= width)
throw new Exception("Coordinates out of the field!");
if (cells[y * width + x].isMine)
if (cells[y * width + x].IsMine)
return true;
if (cells[y * width + x].isMarked)
if (cells[y * width + x].IsMarked)
return false;
if (!opened.Contains(cells[y * width + x]))
{
opened.Add(cells[y * width + x]);
if (cells[y * width + x].isEmpty())
if (cells[y * width + x].IsEmpty())
{
if (x < width - 1)
this.open(y, x + 1);
this.Open(y, x + 1);
if (x > 0)
this.open(y, x - 1);
this.Open(y, x - 1);
if (y < height - 1)
this.open(y + 1, x);
this.Open(y + 1, x);
if (y > 0)
this.open(y - 1, x);
this.Open(y - 1, x);
if (x < width - 1 && y < height - 1)
this.open(y + 1, x + 1);
this.Open(y + 1, x + 1);
if (x > 0 && y < height - 1)
this.open(y + 1, x - 1);
this.Open(y + 1, x - 1);
if (x < width - 1 && y > 0)
this.open(y - 1, x + 1);
this.Open(y - 1, x + 1);
if (x > 0 && y > 0)
this.open(y - 1, x - 1);
this.Open(y - 1, x - 1);
}
}
else if (!cells[y * width + x].isEmpty())
else if (!cells[y * width + x].IsEmpty())
{
int marked = 0;
for (int y1 = y - 1; y1 < y + 2; y1++)
@@ -133,11 +129,11 @@
{
if (x1 < 0 || x1 >= width || y1 == y && x1 == x)
continue;
if (cells[y1 * width + x1].isMarked)
if (cells[y1 * width + x1].IsMarked)
marked++;
}
}
if (marked == Convert.ToInt16(cells[y * width + x].value))
if (marked == Convert.ToInt16(cells[y * width + x].Value))
for (int y1 = y - 1; y1 < y + 2; y1++)
{
if (y1 < 0 || y1 >= height)
@@ -146,30 +142,30 @@
{
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))
if (!cells[y1 * width + x1].IsMarked && !opened.Contains(cells[y1 * width + x1]))
if (this.Open(y1, x1))
return true;
}
}
}
return false;
}
public void openall()
public void OpenAll()
{
for (int i = 0; i < size; i++)
{
if (!opened.Contains(cells[i]))
opened.Add(cells[i]);
if (cells[i].isMarked && !cells[i].isMine)
cells[i].setWrong();
if (cells[i].IsMarked && !cells[i].IsMine)
cells[i].SetWrong();
}
}
public void mark(int y, int x) { cells[y * width + x].mark(); }
public void unmark(int y, int x) { cells[y * width + x].unmark(); }
public bool check()
public void Mark(int y, int x) { cells[y * width + x].Mark(); }
public void Unmark(int y, int x) { cells[y * width + x].Unmark(); }
public bool Check()
{
for (int i = 0; i < size; i++)
if (!cells[i].isMine && !opened.Contains(cells[i]))
if (!cells[i].IsMine && !opened.Contains(cells[i]))
return false;
return true;
}