-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
100 lines (88 loc) · 3.32 KB
/
Copy pathProgram.cs
File metadata and controls
100 lines (88 loc) · 3.32 KB
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
using System.Reflection;
namespace hangman
{
internal class Program
{
static async Task Main(string[] args)
{
Console.Title = "hangman";
List<string> wordList = new List<string>();
Assembly assembly = Assembly.GetExecutingAssembly();
Stream? stream = assembly.GetManifestResourceStream("hangman.words.txt");
StreamReader reader;
if (stream == null)
{
HttpClient client = new();
HttpResponseMessage res = await client.GetAsync("https://raw.githubusercontent.com/bubblxs/hangman/main/words.txt");
Stream content = res.Content.ReadAsStream();
reader = new(content);
}
else
{
reader = new(stream);
}
string? line = string.Empty;
while ((line = reader.ReadLine()) != null)
{
wordList.Add(line);
}
if (wordList.Count == 0)
{
throw new Exception("words not found");
}
do
{
Sprites sprite = new();
Word word = new(wordList[new Random().Next(0, wordList.Count - 1)]);
int attempt = 0;
int maxNumAttempts = sprite.GetNumOfSprites() - 1;
char[] mask = word.GetWordMask().ToCharArray();
while (true)
{
Console.Clear();
Console.WriteLine(sprite.GetSprite());
Console.WriteLine($"{new string(mask)}");
Console.WriteLine($"\n[!] wrong attempts: {attempt}/{maxNumAttempts}\n");
if (attempt >= maxNumAttempts)
{
Console.WriteLine($"[-] you lost. the word was \"{word.GetWord()}\"");
break;
}
if (word.IsEqual(mask))
{
Console.WriteLine("[-] you won!");
break;
}
Console.Write("[+] try: ");
char letter = Console.ReadKey().KeyChar;
List<int> indexes = word.GetIndexesOf(letter);
if (indexes.Count == 0)
{
attempt += 1;
sprite.UpdateSprite(attempt);
}
else
{
indexes.ForEach((l) => mask[l] = letter);
}
}
Console.Write("[-] would you like to play again? [y/n]: ");
switch (Console.ReadKey().Key)
{
case ConsoleKey.Y:
Console.WriteLine("\n[-] starting a new game");
Thread.Sleep(1500);
break;
case ConsoleKey.N:
Console.WriteLine("\n[-] quitting");
Environment.Exit(0);
break;
default:
Console.WriteLine("\n[-] i will take that as a no :3");
Environment.Exit(0);
break;
}
} while (true);
}
}
}