-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.cs
96 lines (77 loc) · 2.7 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using Hangman.view;
namespace Hangman.model
{
internal class Game
{
const int MAX_LIVES = 5;
private int lives = MAX_LIVES;
private string word = "";
private List<char> guessedChars = new List<char>();
private int lettersToGuessCount = 0;
private int rightLettersCounter = 0;
private IView view = new ConsoleRenderer();
private DictionaryService dictionaryService = DictionaryService.getInstance();
public Game()
{
word = dictionaryService.GetRandomWord();
lettersToGuessCount = word.Distinct().Count();
}
private char ReadLetter()
{
var pressedKey = Console.ReadKey().KeyChar;
var isLetter = Char.IsLetter(pressedKey);
var isAlreadyUsed = guessedChars.Contains(pressedKey);
while (!isLetter || isAlreadyUsed)
{
if (!isLetter)
{
Console.WriteLine($"\n{pressedKey} не буква.");
}
if (isAlreadyUsed)
{
Console.WriteLine($"\n{pressedKey} уже была использована. Попробуй другую.");
}
view.PrintPrompt();
pressedKey = Console.ReadKey().KeyChar;
isLetter = Char.IsLetter(pressedKey);
isAlreadyUsed = guessedChars.Contains(pressedKey);
}
return pressedKey;
}
private void MakeTurn()
{
view.PrintFrame(MAX_LIVES - lives, lives, word, guessedChars);
view.PrintPrompt();
var pressedKey = ReadLetter();
guessedChars.Add(pressedKey);
if (word.Contains(pressedKey))
{
rightLettersCounter++;
}
else
{
lives--;
}
}
public void StartGame()
{
while (lives > 0 && rightLettersCounter < lettersToGuessCount)
{
MakeTurn();
}
view.PrintFrame(MAX_LIVES - lives, lives, word, guessedChars);
Console.WriteLine();
if (lives == 0)
{
Console.WriteLine("Вы проиграли");
Console.WriteLine($"Загаданное слово это {word}");
}
if (rightLettersCounter == lettersToGuessCount)
{
Console.WriteLine("Вы победили");
}
Console.WriteLine("\nНажмите людую кнопку, чтобы вернуться в меню.");
Console.ReadKey();
}
}
}