-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathC# code.txt
320 lines (265 loc) · 9.58 KB
/
C# code.txt
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
using System;
//used for using C# lists
using System.Collections.Generic;
//Small application for playing a simple command line based number guessing game!
//Written By Seabrook Ng
namespace NumberGuesser
{
//create a class as a global variable
static class globalList
{
static List<string> _list; // Static List instance
static globalList()
{
//
// Allocate the list.
//
_list = new List<string>();
}
public static void Record(string value)
{
//
// Record this value in the list.
//
_list.Add(value);
}
public static void Display()
{
//
// Write out the results.
//
foreach (var value in _list)
{
Console.WriteLine(value);
}
}
}
internal class Program
{
static void Main(string[] args)
{
//getAppInfo displays app version and creator
getAppInfo();
//Ask users name
Console.WriteLine("What is your name?");
//Get User input
string inputName = Console.ReadLine();
displayMainMenu();
moveToMenuOptions(inputName);
}
//Get and display app info
static void getAppInfo()
{
//set app vars
string appName = "Number Guesser";
string appVersion = "1.0.0";
string appAuthor = "Seabrook Ng";
// Change text color
Console.ForegroundColor = ConsoleColor.Green;
//write app info
Console.WriteLine("{0}: Version {1} by {2}", appName, appVersion, appAuthor);
// Change text color
Console.ResetColor();
}
// Print color message
static void printColorMessage(ConsoleColor color, string message)
{
//Change command line text color
Console.ForegroundColor = color;
Console.WriteLine(message);
//Reset text color
Console.ResetColor();
}
//print the main menu options
static void displayMainMenu()
{
Console.WriteLine("Welcome to Number Guesser!");
Console.WriteLine("[0] Play Game");
Console.WriteLine("[1] View Highscores");
Console.WriteLine("[2] Exit Game");
Console.WriteLine("Select an option and press enter:");
}
//move from main menu to an option in the menu
static void moveToMenuOptions(string inputName)
{
bool isAsking = true;
while (isAsking)
{
int option = askForStringInputReturnInt();
//let user out of menu if it is a valid option
//otherwise force user to enter a valid response
if (option == 0)
{
isAsking = false;
bool isPlaying = true;
while (isPlaying)
{
isPlaying = playGame(inputName);
}
//display the main menu again
displayMainMenu();
moveToMenuOptions(inputName);
}
else if (option == 1)
{
isAsking = false;
displayHighScores(inputName);
}
else if (option == 2)
{
isAsking = false;
exitGame();
}
else
{
Console.WriteLine("Please enter a valid response from 0-2!");
}
}
}
//return a string input
//with input validation and force a valid input
static int askForStringInputReturnInt()
{
bool isWaiting = true;
//by default assign negative number
//which is normally impossible for error debugging
int integer = -1;
while(isWaiting)
{
string input = Console.ReadLine();
//input validation make sure valid integer
//if not print error message
//and ask for another input
if (!int.TryParse(input, out integer))
{
//print error messsage
printColorMessage(ConsoleColor.Red, "Please enter a valid integer!");
}
//else if the input is a valid integer
else
{
isWaiting = false;
//convert string input to int
integer = Convert.ToInt32(input);
}
}
return integer;
}
static bool playGame(string inputName)
{
//by default keep the loop going of playGame()
//and correct this variable if the user chooses to stop playing
bool isPlaying = true;
Console.WriteLine("Hello {0}, let's play a game!", inputName);
//create a new random number
Random rnd = new Random();
//initialise random correct number
//returns random integers 1 to 100
int correctNumber = rnd.Next(1, 101);
//initialise guess var
int guess;
Console.WriteLine("Guess a number between 1 and 100");
bool isGuessing = true;
//initialise number of Guesses
int numGuesses = 0;
//begin loop to guess variable
while (isGuessing)
{
string input = Console.ReadLine();
//input validation make sure valid integer
if (!int.TryParse(input, out guess))
{
//print error messsage
printColorMessage(ConsoleColor.Red, "Please enter a valid integer!");
//go to next loop
continue;
}
//prompt the user to guess
//convert string to int
guess = Convert.ToInt32(input);
//input validation
//if input is not a valid number from 1 to 100
///print an error message
if (!(guess >= 1 && guess <= 100))
{
printColorMessage(ConsoleColor.Red, "Please enter an integer from 1 to 100!");
//go to next loop
continue;
}
//valid guesses begin now and only if the code gets to
//this point will it add it to the tally
//every loop is one guess so add one guess to the tally
numGuesses += 1;
if (guess == correctNumber)
{
//take user out of loop
isGuessing = false;
string successMessage = "Great job! You guessed correctly! You win " + inputName + " with " + numGuesses + " guesses!";
printColorMessage(ConsoleColor.Yellow, successMessage);
recordHighScore(inputName, numGuesses);
}
else if (guess > correctNumber)
{
printColorMessage(ConsoleColor.Red, "Your guess is too large! Guess again:");
}
else if (guess < correctNumber)
{
printColorMessage(ConsoleColor.Red, "Your guess is too small! Guess again:");
}
}
//Ask to play again
Console.WriteLine("Play Again? [Y/N]:");
string answer = Console.ReadLine().ToUpper();
if (answer == "Y")
{
//return true playGame() function is within a loop and will repeat by itself
isPlaying = true;
}
else if (answer == "N")
{
//retrun false to stop the loop of the playGame() function
isPlaying = false;
}
else
{
Console.WriteLine("Error: Y/N answer input is in invalid area of code!");
isPlaying = true;
}
return isPlaying;
}
//records
static void recordHighScore(string inputName, int numGuesses)
{
//convert numGuesses to string
string stringGuesses = numGuesses.ToString();
globalList.Record(inputName + ": " + stringGuesses);
}
//display high scores and back option
static void displayHighScores(string inputName)
{
globalList.Display();
Console.WriteLine("[0]Go back to main menu:");
bool isAsking = true;
while (isAsking)
{
int option = askForStringInputReturnInt();
//let user out of menu if it is a valid option
//otherwise force user to enter a valid response
if (option == 0)
{
isAsking = false;
}
else
{
Console.WriteLine("Please enter a valid response which is 0!");
}
}
displayMainMenu();
moveToMenuOptions(inputName);
}
static void exitGame()
{
printColorMessage(ConsoleColor.Yellow, "Thanks for Playing!");
}
}
}