-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
128 lines (100 loc) · 3.53 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//Namespace
namespace NumberGuesser
{
//Main Class
class Program
{
//Entry Point Method
static void Main(string[] args)
{
GetAppInfo(); // Run GertAppInfo function to get info
GreetUser(); // Ask for user name and greet function
while (true)
{
// 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 betwenn 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 color 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, "You are correct!!");
// 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 = "BMO";
// Change text color
Console.ForegroundColor = ConsoleColor.Green;
// Write out app info
Console.WriteLine("{0}: Version {1} by {2}", appName, appVersion, appAuthor);
// Reset text color (to white)
Console.ResetColor();
}
// Ask users name and greet
static void GreetUser()
{
// Ask users name
Console.WriteLine("What is your name?");
// Get user input
string inputName = Console.ReadLine();
Console.WriteLine("Hello, {0}, lets play a game...", inputName);
}
// Print color message
static void PrintColorMessage(ConsoleColor color, string message)
{
// Change text color
Console.ForegroundColor = color;
// Tell user its a messege
Console.WriteLine(message);
// Reset text color (to white)
Console.ResetColor();
}
}
}