Write a C# application that chooses a number in the range between 1 and 1000. It
ID: 662731 • Letter: W
Question
Write a C# application that chooses a number in the range between 1 and 1000. It should display a prompt saying Guess a number between 1 and 1000. The player enters the number and the computer should answer either Too high, Try again or Too low, Try Again to help the user zero in on the number. The app should prompt the user for the next guess. When the user enters the correct answer, display Congratulations, you're (somewehat) of a genius and allow the user to choose if they want to play again.
Count the number of guesses the player make. If the number of guesses is less than 10, display the text You must be a SUPER GENIUS!. If the player makes more than 10 guesses, display the text You disappoint me for you are supposed by a brilliant computer scientist.
Explanation / Answer
using System.Collections.Generic; using System.Linq; public class Program { public class Result { public int Index { get; set; } public bool Flag { get; set; } } static void Main() { Program game = new Program(); game.Run(); } void Run() { // initialize the number of attempts int numberOfAttempts = 5; Console.WriteLine(" Welcome to Random Number Guessing Game."); Console.WriteLine(" Guess the 4 digit random number XXXX."); Console.WriteLine(" For each digit, the number is chosen from 1 to 5 Numbers can repeat."); Console.WriteLine(string.Format(" You have {0} attempts to win the game.", numberOfAttempts)); // Call the method to Generate the Random Number string randomNumber = GenerateRandomNumber(); for (int i = 1; i f.Flag == true).Count(); // Get the place(s)/index of digits that are correct string digitsCorrect = string.Join(",", result.Where(f => f.Flag == true) .Select(c => (++c.Index).ToString())); // check the flag count and display appropriate message if (flagCount == 4) { Console.WriteLine("Random Number:{0} , Your Input:{1}", randomNumber, userInput); Console.WriteLine("You guess is correct! Game Won..hurray...:)"); break; } else if (i == numberOfAttempts) { Console.WriteLine("sorry, You missed it! Game Lost..:("); Console.WriteLine("Random Number is {0}", randomNumber); } else { digitsCorrect = flagCount == 0 ? "none" : digitsCorrect; Console.WriteLine(string.Format("Digit(s) in place {0} correct", digitsCorrect)); } } Console.ReadLine(); } public List GetResult(string randomNumber, string userInput) { char[] splitRandomNumber = randomNumber.ToCharArray(); char[] splitUserInput = userInput.ToCharArray(); List results = new List(); for (int index = 0; indexRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.