-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
143 lines (115 loc) · 4.31 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
136
137
138
139
140
141
142
143
using System;
// Namespace
namespace NumberGuesser
{
// Main Class
class Program
{
// Entry Point Method
static void Main(string[] args)
{
// Run GetAppInfo function
GetAppInfo();
// Ask for user's name and greet
GreetUser();
while (true)
{
// Create a new random object
Random random = new Random();
// Init correct number
int correctNumber = random.Next(1, 101);
// Init guess var
int guess = 0;
// Count var for attempts
int tries = 0;
// Ask user for number
PrintColorMessage(ConsoleColor.Magenta, "Guess a number between 1 and 100");
// While guess is incorrect
while (guess != correctNumber)
{
// Get user input
string input = Console.ReadLine();
// User input validation
if (!int.TryParse(input, out guess))
{
// Print error message
PrintColorMessage(ConsoleColor.Red, "Invalid number input! Please enter a number...");
// Keep Going
continue;
}
// Cast to int and put in guess variable
guess = Int32.Parse(input);
// Match guess to correct number
if (guess != correctNumber)
{
tries++;
// Print Error message
PrintColorMessage(ConsoleColor.Red, "WRONG Number, please try again!");
if(guess > correctNumber)
{
PrintColorMessage(ConsoleColor.Yellow, "Guess LOWER");
}
if (guess < correctNumber)
{
PrintColorMessage(ConsoleColor.Yellow, "Guess HIGHER");
}
}
}
// Output success message
PrintColorMessage(ConsoleColor.Green, "You are CORRECT!");
PrintColorMessage(ConsoleColor.Green, "Number of guesses: "+tries.ToString());
// Ask to play again
Console.WriteLine("Play Again? [Y or N]");
// Get Answer
string ans = Console.ReadLine().ToUpper();
if (ans == "Y")
{
continue;
}
else if (ans == "N")
{
return;
}
else
{
return;
}
}
}
// Get and display app info
static void GetAppInfo()
{
// Set app vars
string appName = "Number Guesser";
string appVersion = "1.1.0";
string appAuthor = "Nusaiba Mahmood";
// Change text color
Console.ForegroundColor = ConsoleColor.Cyan;
// Write out app info
Console.WriteLine("{0}: Version {1} by {2}", appName, appVersion, appAuthor);
// Reset text color
Console.ResetColor();
}
// Ask user's name and greet
static void GreetUser()
{
// Ask user's name
PrintColorMessage(ConsoleColor.Magenta, "What is your name?");
// Get user input
string inputName = Console.ReadLine();
Console.ForegroundColor = ConsoleColor.Magenta;
Console.WriteLine("Hello {0}, let's play a game!", inputName);
Console.ResetColor();
}
// Print color message
static void PrintColorMessage(ConsoleColor color, string message)
{
// Change text color
Console.ForegroundColor = color;
// Tell user guess is incorrect
Console.WriteLine(message);
// Reset text color
Console.ResetColor();
}
}
}