-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
120 lines (89 loc) · 3.5 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
using System;
using System.Text.RegularExpressions;
// NameSpace
namespace NumberGuesser {
// Main Class
class Program {
// Entry Point Method
static void Main(string[] args){
GetAppInfo(); // Run GetAppInfo function to get info
GreetUser(); // Ask for users name & Greet
while(true) {
// Init correct number
// int correctNumber = 7;
// Create a new Random object
Random random = new Random();
// Init correct number
int correctNumber = random.Next(1,11);
// Init guess var
int guess = 0;
// Ask user for number
Console.WriteLine("Guess a number between 1 and 10");
// While guess is not correct
while(guess != correctNumber) {
// Get users input
string? input = Console.ReadLine();
// Make sure its a number
if(!int.TryParse(input, out guess)) {
// Print Error Message
PrintColorMessage(ConsoleColor.Red, "Please use an actual number");
// Keep Going
continue;
}
// Cast to int and put in guess
guess = Int32.Parse(input);
// Match guess to correct number
if(guess != correctNumber) {
// Print error message
PrintColorMessage(ConsoleColor.Red, "Wrong number, please try again");
}
}
// Print success message
PrintColorMessage(ConsoleColor.Yellow, "CORRECT!! You guessed it!");
// Ask to play again
Console.WriteLine("Play Again? [Y or N]");
// Get Answer
string answer = Console.ReadLine()!.ToUpper();
if(answer == "Y") {
continue;
}
else if (answer == "N") {
return;
}
else {
return;
}
}
}
// Get and display app info
static void GetAppInfo() {
// Set app vars
string appName = "Number Guesser";
string appVersion = "1.0.0";
string appAuthor = "Veepanshu Kasana";
// Change text color
Console.ForegroundColor = ConsoleColor.DarkMagenta;
// Write out app info
Console.WriteLine("{0}: Version {1} by {2}", appName, appVersion, appAuthor);
// Reset text color
Console.ResetColor();
}
// Ask users name and greet
static void GreetUser() {
// Ask users name
Console.WriteLine("What is your name?");
// Get users input
string? inputName = Console.ReadLine();
Console.WriteLine("Hello {0}, Let's play a game...", inputName);
}
// Print Color Message
static void PrintColorMessage(ConsoleColor color, string message) {
// Change text color
Console.ForegroundColor = color;
// Tell user its not a number
Console.WriteLine(message);
// Resets text color
Console.ResetColor();
}
}
}