repl.it
@AProgrammer/

Hangman

C#

No description

fork
loading
Files
  • main.cs
  • dictionary.txt
  • main.exe
main.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*

~ ~ ~ Welcome to Hangman! ~ ~ ~

Written by @AProgrammer - 4/24/18

The game is simple enough, just guess letters and save the guy 
from being hanged. 

*/

// Using essential namespaces
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;

// Helper functions 
static class Helper
{
	public static string Multiply(this string source, int n)
	{
		StringBuilder sb = new StringBuilder(n * source.Length);
		
		for(int i = 0; i < n; i++)
		{
			sb.Append(source);
		}
		
		return sb.ToString();
	}
	
	public static void ClearConsole()
	{
		Console.WriteLine("\n".Multiply(40));
	}
}

class Program
{
	// Whether or not you want the letters you have not guessed to be shown
	static bool showNotGuessed = false;
	
	// Main method
	public static void Main(string[] args) 
	{
		//Game();
		Random r = new Random();
		Console.WriteLine(r.Next(2));
	}
	
	// Draws ASCII Hangman to console
	public static void DrawHangman(int n)
	{
		string[] hangman = {@"
		  +---+
		  |   |
		      |
		      |
		      |
		      |
		=========", @"
		  +---+
		  |   |
		  O   |
		      |
		      |
		      |
		=========", @"
		  +---+
		  |   |
		  O   |
		  |   |
		      |
		      |
		=========", @"
		  +---+
		  |   |
		  O   |
		 /|   |
		      |
		      |
		=========", @"
		  +---+
		  |   |
		  O   |
		 /|\  |
		      |
		      |
		=========", @"
		  +---+
		  |   |
		  O   |
		 /|\  |
		 /    |
		      |
		=========", @"
		  +---+
		  |   |
		  O   |
		 /|\  |
		 / \  |
		      |
		=========" };
		Console.WriteLine(hangman[n]);
	}
	
	// Printing guesses to console
	public static void ShowGuessed(List<char> g)
	{
		string gs = new string(g.ToArray());
		Console.WriteLine("");
		Console.WriteLine("Guessed: {0}", string.Join(", ", gs.ToCharArray()));
	}
	
	// Printing letters left to console
	public static void ShowNotGuessed(List<char> g)
	{
		if(showNotGuessed)
		{
			string gs = new string(g.ToArray());
			Console.WriteLine("");
			Console.WriteLine("Remaining: {0}", string.Join(", ", gs.ToCharArray()));
		}
	}
	
	// Printing formatted hidden word to console
	public static void ShowHiddenWord(string h)
	{
		char[] w = h.ToCharArray();
		foreach(char c in w)
		{
			Console.Write(" " + c + " ");
		}
	}
	
	// Main game functionality
	public static void Game()
	{
		Random r = new Random();
		List<string> words = new List<string>();
		List<char> notGuessed = new List<char>();
		foreach(char c in "ABCDEFGHIJKLMNOPQRSTUVWXYZ")
		{
			notGuessed.Add(c);
		}
		foreach(string line in File.ReadLines("dictionary.txt"))
		{
			words.Add(line);
		}
		List<char> guessed = new List<char> {};
		string word = words[r.Next(words.Count)].ToUpper();
		string hiddenWord = "_".Multiply(word.Length);
		int n = 0;
		int nMax = 7;
		while(hiddenWord != word && n < nMax)
		{
			if(n == nMax - 1)
			{
				Helper.ClearConsole();
				ShowGuessed(guessed);
				ShowNotGuessed(notGuessed);
				Console.WriteLine("");
				DrawHangman(n);
				Console.WriteLine("");
				ShowHiddenWord(word);
				Console.WriteLine("");
				Console.WriteLine("");
				Console.WriteLine("Game Over!\nYou couldn't save the poor guy ):");
				break;
			}
			
			Helper.ClearConsole();
			ShowGuessed(guessed);
			ShowNotGuessed(notGuessed);
			Console.WriteLine("");
			DrawHangman(n);
			Console.WriteLine("");
			ShowHiddenWord(hiddenWord);
			Console.WriteLine("");
			Console.WriteLine("");
			
			if(n == nMax - 2) 
			{
				Console.Write("Last chance to guess a new letter:");
			}
			else
			{
				Console.Write("Guess a letter:");
			}
			
			char inp = Console.ReadLine().ToString().ToUpper()[0];
			
			if(word.Contains(inp))
			{
				if(!guessed.Contains(inp))
				{
					guessed.Add(inp);
				}
				
				if(notGuessed.Contains(inp))
				{
					notGuessed.RemoveAt(notGuessed.IndexOf(inp));
					Console.WriteLine(notGuessed.IndexOf(inp));
				}

				StringBuilder sb = new StringBuilder(hiddenWord);
				int i = 0;
				
				foreach(char c in word)
				{
					if(c == inp)
					{
						sb[i] = inp;
					}
					i++;
				}
				
				hiddenWord = sb.ToString();
				Console.WriteLine("");
			}
			else
			{
				if(!guessed.Contains(inp))
				{
					guessed.Add(inp);
					n++;
				}
				
				if(notGuessed.Contains(inp))
				{
					notGuessed.RemoveAt(notGuessed.IndexOf(inp));
					Console.WriteLine(notGuessed.IndexOf(inp));
				}
				
				Console.WriteLine("");
			}
			
		}
		if(hiddenWord == word)
		{
			Helper.ClearConsole();
			ShowGuessed(guessed);
			ShowNotGuessed(notGuessed);
			Console.WriteLine("");
			DrawHangman(n);
			Console.WriteLine("");
			ShowHiddenWord(hiddenWord);
			Console.WriteLine("");
			Console.WriteLine("");
			Console.WriteLine("You Win!");
		}
	}
}
?