Refactoring
This commit is contained in:
parent
ae8de489e3
commit
5f408f5b11
30
Cell.cs
30
Cell.cs
@ -2,33 +2,33 @@
|
|||||||
{
|
{
|
||||||
internal class Cell
|
internal class Cell
|
||||||
{
|
{
|
||||||
public string value = " ";
|
public string Value = " ";
|
||||||
public bool isMine = false;
|
public bool IsMine;
|
||||||
public bool isMarked = false;
|
public bool IsMarked;
|
||||||
|
|
||||||
public Cell(bool mine)
|
public Cell(bool mine)
|
||||||
{
|
{
|
||||||
if (mine)
|
if (mine)
|
||||||
{
|
{
|
||||||
isMine = true;
|
IsMine = true;
|
||||||
value = "¤";
|
Value = "¤";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public string show()
|
public string Show()
|
||||||
{
|
{
|
||||||
if (isMarked)
|
if (IsMarked)
|
||||||
return "X";
|
return "X";
|
||||||
else
|
else
|
||||||
return value;
|
return Value;
|
||||||
}
|
}
|
||||||
public void setMine() { isMine = true; }
|
public void SetMine() { IsMine = true; }
|
||||||
public void mark() { isMarked = true; }
|
public void Mark() { IsMarked = true; }
|
||||||
public void unmark() { isMarked = false; }
|
public void Unmark() { IsMarked = false; }
|
||||||
public void setWrong()
|
public void SetWrong()
|
||||||
{
|
{
|
||||||
isMarked = false;
|
IsMarked = false;
|
||||||
value = "!";
|
Value = "!";
|
||||||
}
|
}
|
||||||
public bool isEmpty() { return value == " "; }
|
public bool IsEmpty() { return Value == " "; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
84
Field.cs
84
Field.cs
@ -2,11 +2,11 @@
|
|||||||
{
|
{
|
||||||
internal class Field
|
internal class Field
|
||||||
{
|
{
|
||||||
private int width = 0;
|
private readonly int width = 0;
|
||||||
private int height = 0;
|
private readonly int height = 0;
|
||||||
private int size = 0;
|
private readonly int size = 0;
|
||||||
private List<Cell> cells = new List<Cell>();
|
private readonly List<Cell> cells = new();
|
||||||
private List<Cell> opened = new List<Cell>();
|
private readonly List<Cell> opened = new();
|
||||||
public Field(int input_width, int input_height, int nmines)
|
public Field(int input_width, int input_height, int nmines)
|
||||||
{
|
{
|
||||||
if (input_width < 0 || input_height < 0)
|
if (input_width < 0 || input_height < 0)
|
||||||
@ -23,23 +23,19 @@
|
|||||||
height = input_height;
|
height = input_height;
|
||||||
size = width * height;
|
size = width * height;
|
||||||
cells = new List<Cell>();
|
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));
|
cells.Add(new Cell(nmines-- > 0));
|
||||||
}
|
Random rnd = new();
|
||||||
Random rnd = new Random();
|
for (int i = 0; i < size; i++)
|
||||||
for (int i = 0; i < size; i++) // shuffling field
|
|
||||||
{
|
{
|
||||||
int dest = rnd.Next(size);
|
int dest = rnd.Next(size);
|
||||||
Cell tmp = cells[i];
|
(cells[i], cells[dest]) = (cells[dest], cells[i]);
|
||||||
cells[i] = cells[dest];
|
|
||||||
cells[dest] = tmp;
|
|
||||||
}
|
}
|
||||||
for (int y = 0; y < height; y++)
|
for (int y = 0; y < height; y++)
|
||||||
{
|
{
|
||||||
for (int x = 0; x < width; x++)
|
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++)
|
for (int y1 = y - 1; y1 <= y + 1; y1++)
|
||||||
{
|
{
|
||||||
@ -50,19 +46,19 @@
|
|||||||
if (x1 < 0 || x1 >= width)
|
if (x1 < 0 || x1 >= width)
|
||||||
continue;
|
continue;
|
||||||
Cell current = cells[y1 * width + x1];
|
Cell current = cells[y1 * width + x1];
|
||||||
if (current.isMine)
|
if (current.IsMine)
|
||||||
continue;
|
continue;
|
||||||
if (current.value == " ")
|
if (current.Value == " ")
|
||||||
current.value = "1";
|
current.Value = "1";
|
||||||
else
|
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(" ");
|
Console.Write(" ");
|
||||||
for (int i = 0; i < width;)
|
for (int i = 0; i < width;)
|
||||||
@ -80,7 +76,7 @@
|
|||||||
Console.Write((char)('A' + y) + " ║");
|
Console.Write((char)('A' + y) + " ║");
|
||||||
for (int x = 0; x < width; x++)
|
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(" ║");
|
Console.WriteLine(" ║");
|
||||||
}
|
}
|
||||||
@ -91,38 +87,38 @@
|
|||||||
}
|
}
|
||||||
Console.WriteLine("╝");
|
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)
|
if (y < 0 || y >= height || x < 0 || x >= width)
|
||||||
throw new Exception("Coordinates out of the field!");
|
throw new Exception("Coordinates out of the field!");
|
||||||
if (cells[y * width + x].isMine)
|
if (cells[y * width + x].IsMine)
|
||||||
return true;
|
return true;
|
||||||
if (cells[y * width + x].isMarked)
|
if (cells[y * width + x].IsMarked)
|
||||||
return false;
|
return false;
|
||||||
if (!opened.Contains(cells[y * width + x]))
|
if (!opened.Contains(cells[y * width + x]))
|
||||||
{
|
{
|
||||||
opened.Add(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)
|
if (x < width - 1)
|
||||||
this.open(y, x + 1);
|
this.Open(y, x + 1);
|
||||||
if (x > 0)
|
if (x > 0)
|
||||||
this.open(y, x - 1);
|
this.Open(y, x - 1);
|
||||||
if (y < height - 1)
|
if (y < height - 1)
|
||||||
this.open(y + 1, x);
|
this.Open(y + 1, x);
|
||||||
if (y > 0)
|
if (y > 0)
|
||||||
this.open(y - 1, x);
|
this.Open(y - 1, x);
|
||||||
if (x < width - 1 && y < height - 1)
|
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)
|
if (x > 0 && y < height - 1)
|
||||||
this.open(y + 1, x - 1);
|
this.Open(y + 1, x - 1);
|
||||||
if (x < width - 1 && y > 0)
|
if (x < width - 1 && y > 0)
|
||||||
this.open(y - 1, x + 1);
|
this.Open(y - 1, x + 1);
|
||||||
if (x > 0 && y > 0)
|
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;
|
int marked = 0;
|
||||||
for (int y1 = y - 1; y1 < y + 2; y1++)
|
for (int y1 = y - 1; y1 < y + 2; y1++)
|
||||||
@ -133,11 +129,11 @@
|
|||||||
{
|
{
|
||||||
if (x1 < 0 || x1 >= width || y1 == y && x1 == x)
|
if (x1 < 0 || x1 >= width || y1 == y && x1 == x)
|
||||||
continue;
|
continue;
|
||||||
if (cells[y1 * width + x1].isMarked)
|
if (cells[y1 * width + x1].IsMarked)
|
||||||
marked++;
|
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++)
|
for (int y1 = y - 1; y1 < y + 2; y1++)
|
||||||
{
|
{
|
||||||
if (y1 < 0 || y1 >= height)
|
if (y1 < 0 || y1 >= height)
|
||||||
@ -146,30 +142,30 @@
|
|||||||
{
|
{
|
||||||
if (x1 < 0 || x1 >= width || y1 == y && x1 == x)
|
if (x1 < 0 || x1 >= width || y1 == y && x1 == x)
|
||||||
continue;
|
continue;
|
||||||
if (!cells[y1 * width + x1].isMarked && !opened.Contains(cells[y1 * width + x1]))
|
if (!cells[y1 * width + x1].IsMarked && !opened.Contains(cells[y1 * width + x1]))
|
||||||
if (this.open(y1, x1))
|
if (this.Open(y1, x1))
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
public void openall()
|
public void OpenAll()
|
||||||
{
|
{
|
||||||
for (int i = 0; i < size; i++)
|
for (int i = 0; i < size; i++)
|
||||||
{
|
{
|
||||||
if (!opened.Contains(cells[i]))
|
if (!opened.Contains(cells[i]))
|
||||||
opened.Add(cells[i]);
|
opened.Add(cells[i]);
|
||||||
if (cells[i].isMarked && !cells[i].isMine)
|
if (cells[i].IsMarked && !cells[i].IsMine)
|
||||||
cells[i].setWrong();
|
cells[i].SetWrong();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public void mark(int y, int x) { cells[y * width + x].mark(); }
|
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 void Unmark(int y, int x) { cells[y * width + x].Unmark(); }
|
||||||
public bool check()
|
public bool Check()
|
||||||
{
|
{
|
||||||
for (int i = 0; i < size; i++)
|
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 false;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
24
Game.cs
24
Game.cs
@ -2,20 +2,20 @@
|
|||||||
{
|
{
|
||||||
internal class Game
|
internal class Game
|
||||||
{
|
{
|
||||||
private Field field;
|
private readonly Field field;
|
||||||
public Game(int field_width, int field_height, int nmines)
|
public Game(int field_width, int field_height, int nmines)
|
||||||
{
|
{
|
||||||
field = new Field(field_width, field_height, nmines);
|
field = new Field(field_width, field_height, nmines);
|
||||||
}
|
}
|
||||||
public bool run()
|
public bool Run()
|
||||||
{
|
{
|
||||||
string[] command;
|
string[] command;
|
||||||
field.draw();
|
field.Draw();
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
Console.Clear();
|
Console.Clear();
|
||||||
field.draw();
|
field.Draw();
|
||||||
if (field.check())
|
if (field.Check())
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -27,9 +27,9 @@
|
|||||||
{
|
{
|
||||||
int x = Convert.ToInt16(command[0].Substring(1)) - 1;
|
int x = Convert.ToInt16(command[0].Substring(1)) - 1;
|
||||||
int y = command[0].First() - 'A';
|
int y = command[0].First() - 'A';
|
||||||
if (field.open(y, x))
|
if (field.Open(y, x))
|
||||||
{
|
{
|
||||||
this.finish();
|
this.Finish();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -40,9 +40,9 @@
|
|||||||
int x = Convert.ToInt16(command[1].Substring(1)) - 1;
|
int x = Convert.ToInt16(command[1].Substring(1)) - 1;
|
||||||
int y = command[1].First() - 'A';
|
int y = command[1].First() - 'A';
|
||||||
if (command[0] == "M")
|
if (command[0] == "M")
|
||||||
field.mark(y, x);
|
field.Mark(y, x);
|
||||||
else
|
else
|
||||||
field.unmark(y, x);
|
field.Unmark(y, x);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
@ -52,11 +52,11 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
private void finish()
|
private void Finish()
|
||||||
{
|
{
|
||||||
field.openall();
|
field.OpenAll();
|
||||||
Console.Clear();
|
Console.Clear();
|
||||||
field.draw();
|
field.Draw();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
3
Main.cs
3
Main.cs
@ -4,7 +4,6 @@
|
|||||||
{
|
{
|
||||||
public static void Main(string[] args)
|
public static void Main(string[] args)
|
||||||
{
|
{
|
||||||
int width, height, nmines;
|
|
||||||
Game game;
|
Game game;
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
@ -31,7 +30,7 @@
|
|||||||
"Enter field width, height and number of mines separated with a space: ");
|
"Enter field width, height and number of mines separated with a space: ");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
bool result = game.run();
|
bool result = game.Run();
|
||||||
if (result)
|
if (result)
|
||||||
Console.WriteLine("You win! Congratulations!");
|
Console.WriteLine("You win! Congratulations!");
|
||||||
else
|
else
|
||||||
|
|||||||
Reference in New Issue
Block a user