Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace Task
- {
- internal struct SuperComputerQuery
- {
- /// <summary>
- /// The name of the person that asks the question.
- /// </summary>
- public readonly string AskersName;
- public readonly string Question;
- public SuperComputerQuery(string askersNamesName, string question)
- {
- AskersName = askersNamesName;
- Question = question;
- }
- }
- internal struct SuperComputerReply
- {
- /// <summary>
- /// Whether the super computer was able to fulfil the query, true if it managed to and false otherwise.
- /// </summary>
- public readonly bool WasQueryFulfilled;
- /// <summary>
- /// A string representing the literal answer of the super computer.
- /// </summary>
- public readonly string Answer;
- public SuperComputerReply(bool wasQueryFulfilled, string answer)
- {
- WasQueryFulfilled = wasQueryFulfilled;
- Answer = answer;
- }
- }
- internal interface SuperComputer
- {
- string ComputerName { get; }
- SuperComputerReply ProcessQuery(SuperComputerQuery query);
- /// <summary>
- /// Returns a super computer that is guaranteed to be more powerful than the current one.
- /// </summary>
- /// <returns>A more powerful super computer</returns>
- SuperComputer GetBetterComputer();
- }
- internal class DeepThought : SuperComputer
- {
- /// <summary>
- /// The answer to life, the universe and everything.
- /// </summary>
- private const int _theAnswer = 42;
- public string ComputerName => "DeepThought";
- public SuperComputerReply ProcessQuery(SuperComputerQuery query)
- {
- string answer = "";
- var succeeded = ProcessQuestion(query.Question, out answer);
- return new SuperComputerReply(succeeded, string.Format("The answer is.... {0}", answer));
- }
- public SuperComputer GetBetterComputer()
- {
- return new SuperPowerfulComputer(2);
- }
- private bool ProcessQuestion(string question, out string answer)
- {
- var n = 0;
- for (; n < _theAnswer; n++) ;
- answer = n.ToString();
- return false;
- }
- }
- internal class SuperPowerfulComputer : SuperComputer
- {
- public string ComputerName { get; private set; }
- private int _powerMultiplier;
- public SuperPowerfulComputer(int powerMultiplier)
- {
- _powerMultiplier = powerMultiplier;
- ComputerName = string.Format("Super Powerful Computer Times {0}", _powerMultiplier);
- }
- public SuperComputerReply ProcessQuery(SuperComputerQuery query)
- {
- return new SuperComputerReply(false, string.Format("I am sorry, {0}, even I who possess {1} times more compute power than DeepThought am unable to answer your question. If you want... I can create a more powerful computer to try and aswer it", query.AskersName, _powerMultiplier));
- }
- public SuperComputer GetBetterComputer()
- {
- return new SuperPowerfulComputer(_powerMultiplier + 1);
- }
- }
- internal class Program
- {
- /// <summary>
- /// The most sophisticated super computer humans can create.
- /// </summary>
- private static readonly SuperComputer _ourBestComputerYet = new DeepThought();
- public static void Main(string[] args)
- {
- var askersName = GetUsersName();
- Console.WriteLine(string.Format("Ok, {0}, lets find out the answer to the ultimate question of life!", askersName));
- GetAnswerToTheUltimateQuestionOfLife(askersName, _ourBestComputerYet);
- Console.WriteLine(string.Format("Goodbye, {0}!", askersName));
- }
- private static void GetAnswerToTheUltimateQuestionOfLife(string askersName, SuperComputer computer)
- {
- var query = new SuperComputerQuery(askersName, "What is the answer to the ultimate question of life?");
- var reply = computer.ProcessQuery(query);
- Console.WriteLine(reply.Answer);
- if (reply.WasQueryFulfilled)
- return;
- if (DoesUserWantToTryAgain())
- GetAnswerToTheUltimateQuestionOfLife(askersName, computer.GetBetterComputer());
- }
- /// <summary>
- /// Can be used to try again by saying yes (using the y key) or no (using the n key).
- /// </summary>
- /// <returns>Whether the user wants to try again or not</returns>
- private static bool DoesUserWantToTryAgain()
- {
- ConsoleKey response;
- do
- {
- Console.Write("You don't seem too happy with the answer... would you like to try again with a more powerful computer? [y/n] ");
- response = Console.ReadKey(false).Key;
- if (response != ConsoleKey.Enter)
- Console.WriteLine();
- } while (response != ConsoleKey.Y && response != ConsoleKey.N);
- return response == ConsoleKey.Y;
- }
- private static string GetUsersName()
- {
- Console.WriteLine("Hello, what is your name?");
- var askersName = Console.ReadLine();
- while (String.IsNullOrEmpty(askersName))
- {
- Console.WriteLine("Come on, don't be shy, what is your name?");
- askersName = Console.ReadLine();
- }
- return askersName;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement