From 5f408f5b1186f8c9f777c4764f8d56b613d524fc Mon Sep 17 00:00:00 2001 From: Masahiko AMANO Date: Thu, 3 Mar 2022 01:12:28 +0300 Subject: [PATCH] Refactoring --- Cell.cs | 30 ++++++++++---------- Field.cs | 84 +++++++++++++++++++++++++++----------------------------- Game.cs | 24 ++++++++-------- Main.cs | 3 +- 4 files changed, 68 insertions(+), 73 deletions(-) diff --git a/Cell.cs b/Cell.cs index 4df93c0..f4d71a0 100644 --- a/Cell.cs +++ b/Cell.cs @@ -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 == " "; } } } diff --git a/Field.cs b/Field.cs index bbf1674..346ae3d 100644 --- a/Field.cs +++ b/Field.cs @@ -2,11 +2,11 @@ { internal class Field { - private int width = 0; - private int height = 0; - private int size = 0; - private List cells = new List(); - private List opened = new List(); + private readonly int width = 0; + private readonly int height = 0; + private readonly int size = 0; + private readonly List cells = new(); + private readonly List 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(); - 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; } diff --git a/Game.cs b/Game.cs index da28e29..7e6d1fe 100644 --- a/Game.cs +++ b/Game.cs @@ -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(); } } } diff --git a/Main.cs b/Main.cs index 9479ff8..196c30c 100644 --- a/Main.cs +++ b/Main.cs @@ -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