Created
October 16, 2019 15:17
-
-
Save SudiDav/41e36be7316bb94189698e9f649b9e2f to your computer and use it in GitHub Desktop.
CsharpNumberGuess
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace NumberGuesser | |
{ | |
class Program | |
{ | |
//App Entry point | |
static void Main(string[] args) | |
{ | |
GetAppInfo(); | |
GreetUser(); | |
//initial correct number | |
//int correctNumber = 7; | |
while (true) | |
{ | |
Random random = new Random(); | |
int correctNumber = random.Next(1, 10); | |
//initial guess number | |
int guess = 0; | |
Console.WriteLine("Guess a number between 1 and 10"); | |
// look when the guess nuber is not correct | |
while (guess != correctNumber) | |
{ | |
//Get the user's input | |
string input = Console.ReadLine(); | |
//to make sure it is a number | |
if (!int.TryParse(input, out guess)) | |
{ | |
PrintColorMessage(ConsoleColor.Red, "Sorry it is a number, please enter a number!"); | |
//keep going | |
continue; | |
} | |
//Convert to int | |
guess = Int32.Parse(input); | |
//Match the guess number to the input | |
if (guess != correctNumber) | |
{ | |
PrintColorMessage(ConsoleColor.Red, "Sorry wrong number, please enter a correct number!"); | |
} | |
} | |
PrintColorMessage(ConsoleColor.DarkBlue, "There we go. That is the number! "); | |
//Ask to play again! | |
Console.WriteLine("Play again?[Y or N]"); | |
//Get the answer | |
string answer = Console.ReadLine().ToUpper(); | |
if (answer == "Y") | |
{ | |
continue; | |
} | |
else if(answer == "N") | |
{ | |
return; | |
} | |
} | |
} | |
static void GetAppInfo() | |
{ | |
// Ap setting | |
string appName = "Number Guesser"; | |
string appVersion = "1.1.1"; | |
string appAuthor = "Sudi Dav"; | |
//Change text color | |
Console.ForegroundColor = ConsoleColor.Green; | |
//Write out app info | |
Console.WriteLine($"{appName}: {appVersion} by {appAuthor}"); | |
//Reset color to the default after changing the color of the app info | |
Console.ResetColor(); | |
} | |
static void GreetUser() { | |
// ask the user their name | |
Console.WriteLine("What is your name?"); | |
//Get the input from the user | |
string inputName = Console.ReadLine(); | |
Console.WriteLine($"Hello {inputName}, let play a game...!"); | |
} | |
static void PrintColorMessage(ConsoleColor color,string message) | |
{ | |
//change the color of the | |
Console.ForegroundColor = color; | |
//user customer message for wrong input | |
Console.WriteLine(message); | |
//Reset color to the default after changing the color of the app info | |
Console.ResetColor(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment