Guest User

Reddit project 1h

a guest
Feb 12th, 2012
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * we all know the classic "guessing game" with higher or lower prompts.
  3.  * lets do a role reversal; you create a program that
  4.  * will guess numbers between 1-100, and respond appropriately
  5.  * based on whether users say that the number is too high or too low.
  6.  * Try to make a program that can guess your number based on user input and great code!
  7.  */
  8.  
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Globalization;
  14.  
  15. namespace First_Hard
  16. {
  17.     class Program
  18.     {
  19.         static void Main(string[] args)
  20.         {
  21.             string[] enumValues = Enum.GetNames(typeof(Opoments));
  22.             Console.WriteLine("Choose the opponent for guessing:");
  23.             for (int i = 0; i < enumValues.Length; i++)
  24.             {
  25.                 Console.WriteLine("[{0}] -> {1} Opoment", i + 1, enumValues[i]);
  26.             }
  27.  
  28.             int userInput = 0;
  29.             string line;
  30.             do
  31.             {
  32.                 line = Console.ReadLine();
  33.             } while (!int.TryParse(line, out userInput) || userInput > enumValues.Length || userInput <= 0);
  34.  
  35.             userInput--;
  36.             IGuesser guesser;
  37.             string opponentClass = enumValues[userInput];
  38.             opponentClass = new CultureInfo("en-GB").TextInfo.ToTitleCase(opponentClass);
  39.             opponentClass += "Guesser";
  40.             opponentClass = "First_Hard." + opponentClass;
  41.             Type opponentType = Type.GetType(opponentClass, true, true);
  42.             guesser = opponentType.GetConstructor(new Type[0]).Invoke(new object[0]) as IGuesser;
  43.  
  44.             if (guesser == null) return;
  45.             Console.WriteLine("Game is starting.");
  46.             Console.WriteLine("First Guess is {0}", guesser.FirstGuess());
  47.             GuessResult result;
  48.             while ((result = AskUser()) != GuessResult.exact)
  49.             {
  50.                 if (result == GuessResult.higher)
  51.                 {
  52.                     Console.WriteLine("Is it {0}?", guesser.GuessHigher());
  53.                 }
  54.                 else
  55.                 {
  56.                     Console.WriteLine("How about {0}?", guesser.GuessLower());
  57.                 }
  58.             }
  59.             Console.WriteLine("Thank you for playing");
  60.             Console.ReadKey();
  61.         }
  62.  
  63.         static GuessResult AskUser()
  64.         {
  65.             string answer;
  66.             do
  67.             {
  68.                 Console.WriteLine("[H]igher, [L]ower or [E]xact");
  69.                 answer = Console.ReadLine();
  70.                 answer.ToLower();
  71.             } while (answer != "h" && answer != "l" && answer != "e");
  72.             switch (answer)
  73.             {
  74.                 case "h":
  75.                     return GuessResult.higher;
  76.                 case "l":
  77.                     return GuessResult.lower;
  78.                 case "e":
  79.                     return GuessResult.exact;
  80.             }
  81.             throw new InvalidOperationException("I should never be thrown");
  82.  
  83.         }
  84.     }
  85.  
  86.     interface IGuesser
  87.     {
  88.         int FirstGuess();
  89.         int GuessLower();
  90.         int GuessHigher();
  91.     }
  92.  
  93.     enum Opoments
  94.     {
  95.         dumb, smart, random
  96.     }
  97.  
  98.     enum GuessResult
  99.     {
  100.         higher, lower, exact
  101.     }
  102.  
  103.     class DumbGuesser : IGuesser
  104.     {
  105.         private int top;
  106.         private int bottom;
  107.         private int last;
  108.         private Random rand;
  109.  
  110.         public DumbGuesser()
  111.         {
  112.             top = 100;
  113.             bottom = 1;
  114.             last = -1;
  115.             rand = new Random();
  116.         }
  117.  
  118.         public int FirstGuess()
  119.         {
  120.             if (last != -1) throw new InvalidOperationException("I alredy guessed first time");
  121.             last = rand.Next(100);
  122.             return last;
  123.         }
  124.  
  125.         public int GuessLower()
  126.         {
  127.             Check();
  128.             top = last;
  129.             last = rand.Next(bottom, top);
  130.             return last;
  131.         }
  132.  
  133.         public int GuessHigher()
  134.         {
  135.             Check();
  136.             bottom = last;
  137.             last = rand.Next(bottom, top);
  138.             return last;
  139.         }
  140.  
  141.         private void Check()
  142.         {
  143.             if (last == -1)
  144.                 throw new InvalidOperationException("I did not have first guess");
  145.             if (bottom >= top)
  146.             {
  147.                 Console.WriteLine("CHEATER!!!!!!");
  148.                 Console.ReadKey();
  149.                 Environment.Exit(0);
  150.             }
  151.         }
  152.     }
  153.  
  154.     class SmartGuesser : IGuesser
  155.     {
  156.         private int top;
  157.         private int bottom;
  158.         private int last;
  159.  
  160.         public SmartGuesser()
  161.         {
  162.             top = 100;
  163.             bottom = 1;
  164.             last = -1;
  165.         }
  166.  
  167.         public int FirstGuess()
  168.         {
  169.             last = 50;
  170.             return 50;
  171.         }
  172.  
  173.         public int GuessLower()
  174.         {
  175.             top = last;
  176.             return Guess();
  177.         }
  178.  
  179.         public int GuessHigher()
  180.         {
  181.             bottom = last;
  182.             return Guess();
  183.         }
  184.  
  185.         private int Guess()
  186.         {
  187.             Check();
  188.             int retVal = (top - bottom + 1) / 2;
  189.             retVal += bottom;
  190.             last = retVal;
  191.             return retVal;
  192.         }
  193.  
  194.         private void Check()
  195.         {
  196.             if (last == -1)
  197.                 throw new InvalidOperationException("I did not have first guess");
  198.             if (bottom >= top)
  199.             {
  200.                 Console.WriteLine("CHEATER!!!!!!");
  201.                 Console.ReadKey();
  202.                 Environment.Exit(0);
  203.             }
  204.         }
  205.     }
  206.  
  207.     public class RandomGuesser : IGuesser
  208.     {
  209.         private IGuesser innerGuesser;
  210.         public RandomGuesser()
  211.         {
  212.             if ((new Random().Next() % 2) == 0)
  213.             {
  214.                 innerGuesser = new SmartGuesser();
  215.             }
  216.             else
  217.             {
  218.                 innerGuesser = new DumbGuesser();
  219.             }
  220.         }
  221.  
  222.         public int FirstGuess()
  223.         {
  224.             return innerGuesser.FirstGuess();
  225.         }
  226.  
  227.         public int GuessLower()
  228.         {
  229.             return innerGuesser.GuessLower();
  230.         }
  231.  
  232.         public int GuessHigher()
  233.         {
  234.             return innerGuesser.GuessHigher();
  235.         }
  236.     }
  237. }
Add Comment
Please, Sign In to add comment