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

30
Cell.cs
View File

@ -2,33 +2,33 @@
{
internal class Cell
{
public string value = " ";
public bool isMine = false;
public bool isMarked = false;
public string Value = " ";
public bool IsMine;
public bool IsMarked;
public Cell(bool mine)
{
if (mine)
{
isMine = true;
value = "¤";
IsMine = true;
Value = "¤";
}
}
public string show()
public string Show()
{
if (isMarked)
if (IsMarked)
return "X";
else
return value;
return Value;
}
public void setMine() { isMine = true; }
public void mark() { isMarked = true; }
public void unmark() { isMarked = false; }
public void setWrong()
public void SetMine() { IsMine = true; }
public void Mark() { IsMarked = true; }
public void Unmark() { IsMarked = false; }
public void SetWrong()
{
isMarked = false;
value = "!";
IsMarked = false;
Value = "!";
}
public bool isEmpty() { return value == " "; }
public bool IsEmpty() { return Value == " "; }
}
}

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;
}

24
Game.cs
View File

@ -2,20 +2,20 @@
{
internal class Game
{
private Field field;
private readonly Field field;
public Game(int field_width, int field_height, int nmines)
{
field = new Field(field_width, field_height, nmines);
}
public bool run()
public bool Run()
{
string[] command;
field.draw();
field.Draw();
while (true)
{
Console.Clear();
field.draw();
if (field.check())
field.Draw();
if (field.Check())
{
return true;
}
@ -27,9 +27,9 @@
{
int x = Convert.ToInt16(command[0].Substring(1)) - 1;
int y = command[0].First() - 'A';
if (field.open(y, x))
if (field.Open(y, x))
{
this.finish();
this.Finish();
return false;
}
}
@ -40,9 +40,9 @@
int x = Convert.ToInt16(command[1].Substring(1)) - 1;
int y = command[1].First() - 'A';
if (command[0] == "M")
field.mark(y, x);
field.Mark(y, x);
else
field.unmark(y, x);
field.Unmark(y, x);
}
}
catch (Exception ex)
@ -52,11 +52,11 @@
}
}
}
private void finish()
private void Finish()
{
field.openall();
field.OpenAll();
Console.Clear();
field.draw();
field.Draw();
}
}
}

View File

@ -4,7 +4,6 @@
{
public static void Main(string[] args)
{
int width, height, nmines;
Game game;
while (true)
{
@ -31,7 +30,7 @@
"Enter field width, height and number of mines separated with a space: ");
}
}
bool result = game.run();
bool result = game.Run();
if (result)
Console.WriteLine("You win! Congratulations!");
else