-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.cs
218 lines (190 loc) · 6.03 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
using System;
using System.IO;
using System.Text;
namespace HangManWithGameClass
{
/// <summary>
/// Itay getahun 1070
/// /The game class load a vacabulary of 500 names in english and choose random name for the game
/// It manage the guesses, failours count and guessed array
/// </summary>
public enum GameState { Lost, Winnery, Play }
public class Game
{
private GameState _state;
private string _word;
private StringBuilder _current;
private char[] _totalguessedCharecters;
private char[] _correctGuessedCharecters;
private int _failsCount;
private int _totalGuessCount;
private int _level;
private const int _ENGLISH_CHARECTERS = 26;
private const int _MAXFAILURES = 10;
private static Random _random;
private static readonly string[] _vocabulary;
private string Word
{
get { return _word; }
set { _word = value; }
}
public int WordLength
{
get { return Word.Length; }
}
private StringBuilder Current
{
get { return _current; }
set { _current = value; }
}
public string CurrentString
{
get { return Current.ToString();}
}
public int FailsCount
{
get { return _failsCount; }
private set { _failsCount = value; }
}
public int TotalGuessCount
{
get { return _totalGuessCount; }
private set { _totalGuessCount = value; }
}
public int Level
{
get { return _level; }
private set { _level = value <= 0 || value > 3 ? 1 : value; }
}
public GameState State
{
get { return _state; }
}
public char[] TotalGuessedCharecters
{
get { return _totalguessedCharecters; }
private set { _totalguessedCharecters = value; }
}
public char[] CorrectGuessedCharecters
{
get { return _correctGuessedCharecters; }
private set { _correctGuessedCharecters = value; }
}
public int MaxFailures
{
get { return _MAXFAILURES; }
}
static Game()
/*One random obj and one vacabulary for all instances */
{
_random = new Random();
_vocabulary = LoadWordVacabulary();
}
public string GetTotalCharectersGueesed()
{
StringBuilder sb = new StringBuilder("");
foreach (char letter in TotalGuessedCharecters)
{
sb.Append(letter + " ");
}
return sb.ToString();
}
public void ChangeLevel()
{
Level++;
}
public Game()
{
Level = 1;
NewGme();
}
public void NewGme()
/*Initilize the new game state or replay it*/
{
TotalGuessedCharecters = new char[_ENGLISH_CHARECTERS];
FailsCount = 0;
TotalGuessCount = 0;
_state = GameState.Play;
TotalGuessedCharecters = new char[_ENGLISH_CHARECTERS];
GenarateWord();
CorrectGuessedCharecters = new char[WordLength];
}
public void GuessLetter(char guessedChar)
/*Check letter IFF the game is playing , its a letter and didnt guessed it already
Then update game state if needed calling CheckGame methods */
{
if (char.IsLetter(guessedChar) && !IsGuessed(char.ToLower(guessedChar)) && State == GameState.Play)
{
guessedChar = char.ToLower(guessedChar);
TotalGuessedCharecters[TotalGuessCount] = guessedChar;
TotalGuessCount++;
if (Word.IndexOf(guessedChar) != -1)
{
CorrectGuessedCharecters[TotalGuessCount - FailsCount - 1] = guessedChar;
for (int i = 0; i < WordLength; i++)
{
if (Word[i] == guessedChar)
{
Current[i] = guessedChar;
}
}
}
else
{
FailsCount += Level;
}
}
CheckGame();
}
private bool IsGuessed(char guessedChar)
/*Return whether the letter already guessed*/
{
char.ToLower(guessedChar);
foreach (char letter in TotalGuessedCharecters)
{
if (letter == guessedChar)
{
return true;
}
}
return false;
}
private void GenarateWord()
/* Genarate a random word in lower case
initialize an apropriate Current var*/
{
Word = _vocabulary[_random.Next(0, _vocabulary.Length)].ToLower();
Current = new StringBuilder("");
Current.Append('_', WordLength);
for (int i = 0; i < WordLength; i++)
{
if (Word[i] == ' ')
Current[i] = ' ';
else if (Word[i] == '-')
{
Current[i] = '-';
}
}
}
private void CheckGame()
/*Check whether the game state need to change*/
{
if (_failsCount >= _MAXFAILURES)
{
_state = GameState.Lost;
}
else if (Word == Current.ToString())
{
_state = GameState.Winnery;
}
}
public static string[] LoadWordVacabulary()
/*Read local text file with names*/
{
string st = File.ReadAllText(@"Assets\Name.txt");
string separator = @""", """;
string[] wordsToReturn = st.Split(separator, StringSplitOptions.None);
return wordsToReturn;
}
}
}