-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathHangman.cs
223 lines (201 loc) · 4.86 KB
/
Hangman.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
217
218
219
220
221
222
223
using System;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Linq;
using System.Net;
class Hangman{
static void Main(string[] args){
Console.ForegroundColor = ConsoleColor.Green;
Console.OutputEncoding = System.Text.Encoding.UTF8;
Console.WriteLine("<--------------Hangman---------------->");
Console.WriteLine("Created by Morasiu ([email protected])");
Console.WriteLine("Press any key to start.");
Console.ReadKey();
Display(GetWord(), new List<char>());
Console.ReadKey();
}
static void Display(string word, List<char> letters){
Console.Clear();
int fails = 0;
char[] guessWordLetters = new char[word.Length];
//Console.WriteLine(word);
for(int i = 0; i < letters.Count(); i++){
if(word.Contains(letters[i])){
for(int j = 0; j < word.Length; j++){
if(word[j] == letters[i]){
guessWordLetters[j] = word [j];
}
}
} else {
fails++;
}
}
for (int i = 0; i < guessWordLetters.Length; i++){
if (guessWordLetters[i] == '\0')
guessWordLetters[i] = '_';
}
Console.WriteLine(GetHangman(fails) + "\n");
Console.Write("Letters: ");
foreach(char l in letters){
Console.Write(l + ", ");
}
Console.Write("\nWord: ");
foreach(char c in guessWordLetters){
Console.Write(c + " ");
}
// Game Over :(
if (fails == 7){
Console.WriteLine("\nGame Over");
Console.WriteLine("Your word was: " + word);
Console.WriteLine("Definition:" + GetDefinition(word));
System.Environment.Exit(1);
}else if (word == String.Concat(guessWordLetters)){
Console.WriteLine("\nYou won!");
Console.WriteLine("Definition:" + GetDefinition(word));
System.Environment.Exit(1);
}
char letter = GetLetter();
if(!letters.Contains(letter))
letters.Add(letter);
Display(word, letters);
}
static char GetLetter(){
char letter;
while(true){
Console.Write("\nGive me a letter (a-z): ");
string input = Console.ReadLine();
if (input == ""){
Console.WriteLine("Some guess");
return '0';
}else if(char.TryParse(input, out letter)){
return letter;
} else {
Console.WriteLine("Invalid letter");
}
}
}
static string GetDefinition(string word){
//Get definition to prove that is a real word.
WebClient client = new WebClient();
string url = "https://www.thefreedictionary.com/" + word;
string site = client.DownloadString(url);
string[] lines = site.Split(new [] { '\r', '\n' });
string definition = "";
foreach(string line in lines){
if(line.Contains("id=\"Definition\"")){
definition = line;
break;
}
}
definition = definition.Substring(definition.IndexOf("1.") + 3);
definition = definition.Substring(0, definition.IndexOf("</div>"));
definition = Regex.Replace(definition, "<.*?>", String.Empty);
return definition;
}
static string GetWord(){
//Get a word from https://fakena.me/random-english-words/one/
WebClient client = new WebClient();
string url = "https://fakena.me/random-english-words/one/";
string line = "";
//Check internet connection
try{
line = client.DownloadString(url).Split('\n')[27];
} catch (System.Net.WebException) {
Console.WriteLine("Network connection problem :(");
System.Environment.Exit(1);
}
string word = line.Substring(39).Split('<')[0];
return word;
}
static string GetHangman(int fail){
//Hangman ASCII art
string hangman = "";
switch (fail){
case 0:
hangman = @"
";
break;
case 1:
hangman = @"
________
|/ |
|
|
|
|
|
___|___";
break;
case 2:
hangman = @"
________
|/ |
| (_)
|
|
|
|
___|___";
break;
case 3:
hangman = @"
________
|/ |
| (_)
| |
| |
|
|
___|___";
break;
case 4:
hangman = @"
________
|/ |
| (_)
| |
| |
| /
|
___|___";
break;
case 5:
hangman = @"
________
|/ |
| (_)
| |
| |
| / \
|
___|___";
break;
case 6:
hangman = @"
________
|/ |
| (_)
| \|
| |
| / \
|
___|___";
break;
case 7:
hangman = @"
________
|/ |
| (_)
| \|/
| |
| / \
|
___|___";
break;
default:
hangman = "Got wrong number.";
break;
}
return hangman;
}
}