-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHangmanController.cs
64 lines (55 loc) · 1.73 KB
/
HangmanController.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
62
63
64
using Hangman.controller.commands;
namespace Hangman.controller
{
internal class HangmanController
{
private List<ICommand> commands = new List<ICommand>();
private State state = new State();
private void PrintCommands()
{
Console.Clear();
for (int i = 0; i < commands.Count; i++)
{
var command = commands[i];
Console.WriteLine($"{i + 1}: {command.Description()}");
}
}
private int ReadIndex()
{
try
{
Console.Write("Опция>");
var index = Convert.ToInt32(Console.ReadLine()) ;
if (index > commands.Count || index < 1)
{
PrintCommands();
Console.WriteLine("Не знаю такую команду. Попробуй снова");
return ReadIndex();
}
return index;
}
catch
{
PrintCommands();
Console.WriteLine("Не удалось прочитать номер опции. Попробуй снова");
return ReadIndex();
}
}
private void SelectCommand()
{
Console.WriteLine("Выберите опцию");
PrintCommands();
var commandIndex = ReadIndex() - 1;
commands[commandIndex].Execute(state);
}
public void Bootstrap()
{
commands.Add(new StartGameCommand());
commands.Add(new QuitCommand());
while (state.isRunning)
{
SelectCommand();
}
}
}
}