-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.cs
61 lines (61 loc) · 2.05 KB
/
Game.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
namespace MineSweeper
{
internal class Game
{
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()
{
string[] command;
field.Draw();
while (true)
{
Console.Clear();
if (field.Check())
{
field.OpenAll(true);
return true;
}
field.Draw();
Console.Write("Enter your command: ");
try
{
command = Console.ReadLine().ToUpper().Split();
if (command.Length == 1)
{
int x = Convert.ToInt16(command[0][1..]) - 1;
int y = command[0].First() - 'A';
if (field.Open(y, x))
{
Console.Clear();
field.OpenAll(false);
return false;
}
}
else if (command.Length > 1)
{
if (command[0] != "M" && command[0] != "U")
throw new Exception("Invalid command.");
for (int i = 1; i < command.Length; i++)
{
int x = Convert.ToInt16(command[i][1..]) - 1;
int y = command[i].First() - 'A';
if (command[0] == "M")
field.Mark(y, x);
else
field.Unmark(y, x);
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message + "\nTry again.");
Thread.Sleep(1500);
}
}
}
}
}