-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
159 lines (112 loc) · 4.7 KB
/
Program.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
using System;
namespace hangman_game;
class Program
{
const string NO_GUESS = "_";
const int WRONG_GUESS_MAX = 3;
const int DEFAULT_INT = 0;
const char DEFAULT_CHAR = '0';
static void Main(string[] args)
{
Random random = new Random();
char guess = DEFAULT_CHAR;
int wrongGuessCount = DEFAULT_INT;
char playGame = DEFAULT_CHAR;
List<string> emptyWord = new List<string>();
List<string> listOfWords = new List<string>();
listOfWords.Add("cellphone");
listOfWords.Add("notepad");
listOfWords.Add("nutcracker");
listOfWords.Add("beaver");
listOfWords.Add("starfish");
Console.WriteLine("Wanna guess a word one letter at the time?");
//keep asking for answer if answer is not a walid letter
do
{
Console.WriteLine("\npress y - to START \nany other key - to EXIT\n");
playGame = Char.ToUpper(Console.ReadKey().KeyChar);
if (playGame == 'Y')
{
Console.Clear();
int randomWord = random.Next(listOfWords.Count());
string wordToGuess = listOfWords[randomWord];
int charCount = wordToGuess.Length;
foreach (char letter in wordToGuess) // creates empty wordspace list; letter is the iterator variable
{
emptyWord.Add(NO_GUESS);
}
Console.WriteLine("\nOnly " + charCount + " characters!");
printList(emptyWord);
//Console.WriteLine("\nhint *" + wordToGuess + "*"); //print the random word selected
while (true)
{
Console.WriteLine("Enter a letter:\n");
guess = Char.ToLower(Console.ReadKey().KeyChar);
if (!Char.IsLetter(guess))
{
Console.WriteLine("\nnot a letter");
continue;
}
//put wrong guess counter and game exit here
if (!wordToGuess.Contains(guess))
{
wrongGuessCount++;
int lives = WRONG_GUESS_MAX - wrongGuessCount;
Console.WriteLine($"\n\nNope, {lives} wrong guesses left.");
if (wrongGuessCount == WRONG_GUESS_MAX)
{
Console.WriteLine("\nOut of tries, maybe next time. Press any key to exit.");
Console.ReadKey();
return;
}
Console.WriteLine("\nTry again. ");
}
else
{
Console.Clear();
Console.WriteLine("\nYou guessed!\n"); // correct ***
//iterate through word, looking for the entered letter; index < charCount - because execution should iterate untill, and break when condition is false -> inex == charCount
for (int index = DEFAULT_INT; index < charCount; index++)
{
if (wordToGuess[index] == guess) // adding guessed letters into the correct spaces in the empty word
{
emptyWord.RemoveAt(index);
emptyWord.Insert(index, guess.ToString());
}
}
printList(emptyWord);
}
//check if all letters in the word are guessed
if (!emptyWord.Contains(NO_GUESS))
{
Console.WriteLine("\nHUGE WIN! Start over?\n");
playGame = DEFAULT_CHAR;
guess = DEFAULT_CHAR;
emptyWord.Clear();
break;
}
guess = DEFAULT_CHAR;
}
}
else
{
return;
}
} while (playGame == guess);
}
public static void printList(List<string> word)
{
Console.WriteLine("\n\n");
foreach (string letter in word) // prints empty wordspace list (with guessed letters)
{
Console.Write(letter + " ");
}
Console.WriteLine("\n\n");
}
//public static char continueOrStop(char answer) //doesn't work as expected
//{
// Console.WriteLine("yes - y, any key to exit \n");
// answer = Char.ToLower(Console.ReadKey().KeyChar);
// return answer;
//}
}