Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.78 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Task
  4. {
  5.     internal struct SuperComputerQuery
  6.     {
  7.         /// <summary>
  8.         /// The name of the person that asks the question.
  9.         /// </summary>
  10.         public readonly string AskersName;
  11.        
  12.         public readonly string Question;
  13.  
  14.         public SuperComputerQuery(string askersNamesName, string question)
  15.         {
  16.             AskersName = askersNamesName;
  17.             Question = question;
  18.         }
  19.     }
  20.  
  21.     internal struct SuperComputerReply
  22.     {
  23.         /// <summary>
  24.         /// Whether the super computer was able to fulfil the query, true if it managed to and false otherwise.
  25.         /// </summary>
  26.         public readonly bool WasQueryFulfilled;
  27.  
  28.         /// <summary>
  29.         /// A string representing the literal answer of the super computer.
  30.         /// </summary>
  31.         public readonly string Answer;
  32.  
  33.         public SuperComputerReply(bool wasQueryFulfilled, string answer)
  34.         {
  35.             WasQueryFulfilled = wasQueryFulfilled;
  36.             Answer = answer;
  37.         }
  38.     }
  39.    
  40.     internal interface SuperComputer
  41.     {
  42.         string ComputerName { get; }
  43.        
  44.         SuperComputerReply ProcessQuery(SuperComputerQuery query);
  45.  
  46.         /// <summary>
  47.         /// Returns a super computer that is guaranteed to be more powerful than the current one.
  48.         /// </summary>
  49.         /// <returns>A more powerful super computer</returns>
  50.         SuperComputer GetBetterComputer();
  51.     }
  52.  
  53.     internal class DeepThought : SuperComputer
  54.     {
  55.         /// <summary>
  56.         /// The answer to life, the universe and everything.
  57.         /// </summary>
  58.         private const int _theAnswer = 42;
  59.  
  60.         public string ComputerName => "DeepThought";
  61.  
  62.         public SuperComputerReply ProcessQuery(SuperComputerQuery query)
  63.         {
  64.             string answer = "";
  65.             var succeeded = ProcessQuestion(query.Question, out answer);
  66.            
  67.             return new SuperComputerReply(succeeded, string.Format("The answer is.... {0}", answer));
  68.         }
  69.  
  70.         public SuperComputer GetBetterComputer()
  71.         {
  72.             return new SuperPowerfulComputer(2);
  73.         }
  74.  
  75.         private bool ProcessQuestion(string question, out string answer)
  76.         {
  77.             var n = 0;
  78.  
  79.             for (; n < _theAnswer; n++) ;
  80.            
  81.             answer = n.ToString();
  82.  
  83.             return false;
  84.         }
  85.     }
  86.  
  87.     internal class SuperPowerfulComputer : SuperComputer
  88.     {
  89.         public string ComputerName { get; private set; }
  90.  
  91.         private int _powerMultiplier;
  92.  
  93.         public SuperPowerfulComputer(int powerMultiplier)
  94.         {
  95.             _powerMultiplier = powerMultiplier;
  96.             ComputerName = string.Format("Super Powerful Computer Times {0}", _powerMultiplier);
  97.         }
  98.  
  99.         public SuperComputerReply ProcessQuery(SuperComputerQuery query)
  100.         {
  101.             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));
  102.         }
  103.  
  104.         public SuperComputer GetBetterComputer()
  105.         {
  106.             return new SuperPowerfulComputer(_powerMultiplier + 1);
  107.         }
  108.     }
  109.    
  110.    
  111.     internal class Program
  112.     {
  113.         /// <summary>
  114.         /// The most sophisticated super computer humans can create.
  115.         /// </summary>
  116.         private static readonly SuperComputer _ourBestComputerYet = new DeepThought();
  117.        
  118.         public static void Main(string[] args)
  119.         {
  120.             var askersName = GetUsersName();
  121.             Console.WriteLine(string.Format("Ok, {0}, lets find out the answer to the ultimate question of life!", askersName));
  122.             GetAnswerToTheUltimateQuestionOfLife(askersName, _ourBestComputerYet);            
  123.             Console.WriteLine(string.Format("Goodbye, {0}!", askersName));
  124.  
  125.         }
  126.  
  127.         private static void GetAnswerToTheUltimateQuestionOfLife(string askersName, SuperComputer computer)
  128.         {
  129.             var query = new SuperComputerQuery(askersName, "What is the answer to the ultimate question of life?");
  130.             var reply = computer.ProcessQuery(query);
  131.            
  132.             Console.WriteLine(reply.Answer);
  133.  
  134.             if (reply.WasQueryFulfilled)
  135.                 return;
  136.            
  137.             if (DoesUserWantToTryAgain())
  138.                 GetAnswerToTheUltimateQuestionOfLife(askersName, computer.GetBetterComputer());
  139.         }
  140.  
  141.         /// <summary>
  142.         /// Can be used to try again by saying yes (using the y key) or no (using the n key).
  143.         /// </summary>
  144.         /// <returns>Whether the user wants to try again or not</returns>
  145.         private static bool DoesUserWantToTryAgain()
  146.         {
  147.             ConsoleKey response;
  148.             do
  149.             {
  150.                 Console.Write("You don't seem too happy with the answer... would you like to try again with a more powerful computer? [y/n] ");
  151.                 response = Console.ReadKey(false).Key;
  152.                
  153.                 if (response != ConsoleKey.Enter)
  154.                     Console.WriteLine();
  155.  
  156.             } while (response != ConsoleKey.Y && response != ConsoleKey.N);
  157.  
  158.             return response == ConsoleKey.Y;
  159.         }
  160.  
  161.         private static string GetUsersName()
  162.         {
  163.             Console.WriteLine("Hello, what is your name?");
  164.             var askersName = Console.ReadLine();
  165.  
  166.             while (String.IsNullOrEmpty(askersName))
  167.             {
  168.                 Console.WriteLine("Come on, don't be shy, what is your name?");
  169.                 askersName = Console.ReadLine();
  170.             }
  171.  
  172.             return askersName;
  173.         }
  174.     }
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement