Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * we all know the classic "guessing game" with higher or lower prompts.
- * lets do a role reversal; you create a program that
- * will guess numbers between 1-100, and respond appropriately
- * based on whether users say that the number is too high or too low.
- * Try to make a program that can guess your number based on user input and great code!
- */
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Globalization;
- namespace First_Hard
- {
- class Program
- {
- static void Main(string[] args)
- {
- string[] enumValues = Enum.GetNames(typeof(Opoments));
- Console.WriteLine("Choose the opponent for guessing:");
- for (int i = 0; i < enumValues.Length; i++)
- {
- Console.WriteLine("[{0}] -> {1} Opoment", i + 1, enumValues[i]);
- }
- int userInput = 0;
- string line;
- do
- {
- line = Console.ReadLine();
- } while (!int.TryParse(line, out userInput) || userInput > enumValues.Length || userInput <= 0);
- userInput--;
- IGuesser guesser;
- string opponentClass = enumValues[userInput];
- opponentClass = new CultureInfo("en-GB").TextInfo.ToTitleCase(opponentClass);
- opponentClass += "Guesser";
- opponentClass = "First_Hard." + opponentClass;
- Type opponentType = Type.GetType(opponentClass, true, true);
- guesser = opponentType.GetConstructor(new Type[0]).Invoke(new object[0]) as IGuesser;
- if (guesser == null) return;
- Console.WriteLine("Game is starting.");
- Console.WriteLine("First Guess is {0}", guesser.FirstGuess());
- GuessResult result;
- while ((result = AskUser()) != GuessResult.exact)
- {
- if (result == GuessResult.higher)
- {
- Console.WriteLine("Is it {0}?", guesser.GuessHigher());
- }
- else
- {
- Console.WriteLine("How about {0}?", guesser.GuessLower());
- }
- }
- Console.WriteLine("Thank you for playing");
- Console.ReadKey();
- }
- static GuessResult AskUser()
- {
- string answer;
- do
- {
- Console.WriteLine("[H]igher, [L]ower or [E]xact");
- answer = Console.ReadLine();
- answer.ToLower();
- } while (answer != "h" && answer != "l" && answer != "e");
- switch (answer)
- {
- case "h":
- return GuessResult.higher;
- case "l":
- return GuessResult.lower;
- case "e":
- return GuessResult.exact;
- }
- throw new InvalidOperationException("I should never be thrown");
- }
- }
- interface IGuesser
- {
- int FirstGuess();
- int GuessLower();
- int GuessHigher();
- }
- enum Opoments
- {
- dumb, smart, random
- }
- enum GuessResult
- {
- higher, lower, exact
- }
- class DumbGuesser : IGuesser
- {
- private int top;
- private int bottom;
- private int last;
- private Random rand;
- public DumbGuesser()
- {
- top = 100;
- bottom = 1;
- last = -1;
- rand = new Random();
- }
- public int FirstGuess()
- {
- if (last != -1) throw new InvalidOperationException("I alredy guessed first time");
- last = rand.Next(100);
- return last;
- }
- public int GuessLower()
- {
- Check();
- top = last;
- last = rand.Next(bottom, top);
- return last;
- }
- public int GuessHigher()
- {
- Check();
- bottom = last;
- last = rand.Next(bottom, top);
- return last;
- }
- private void Check()
- {
- if (last == -1)
- throw new InvalidOperationException("I did not have first guess");
- if (bottom >= top)
- {
- Console.WriteLine("CHEATER!!!!!!");
- Console.ReadKey();
- Environment.Exit(0);
- }
- }
- }
- class SmartGuesser : IGuesser
- {
- private int top;
- private int bottom;
- private int last;
- public SmartGuesser()
- {
- top = 100;
- bottom = 1;
- last = -1;
- }
- public int FirstGuess()
- {
- last = 50;
- return 50;
- }
- public int GuessLower()
- {
- top = last;
- return Guess();
- }
- public int GuessHigher()
- {
- bottom = last;
- return Guess();
- }
- private int Guess()
- {
- Check();
- int retVal = (top - bottom + 1) / 2;
- retVal += bottom;
- last = retVal;
- return retVal;
- }
- private void Check()
- {
- if (last == -1)
- throw new InvalidOperationException("I did not have first guess");
- if (bottom >= top)
- {
- Console.WriteLine("CHEATER!!!!!!");
- Console.ReadKey();
- Environment.Exit(0);
- }
- }
- }
- public class RandomGuesser : IGuesser
- {
- private IGuesser innerGuesser;
- public RandomGuesser()
- {
- if ((new Random().Next() % 2) == 0)
- {
- innerGuesser = new SmartGuesser();
- }
- else
- {
- innerGuesser = new DumbGuesser();
- }
- }
- public int FirstGuess()
- {
- return innerGuesser.FirstGuess();
- }
- public int GuessLower()
- {
- return innerGuesser.GuessLower();
- }
- public int GuessHigher()
- {
- return innerGuesser.GuessHigher();
- }
- }
- }
Add Comment
Please, Sign In to add comment