public class GameManager { private const int MAX_TIMES = 6; private int times; private readonly IGuesser guesser; public bool IsGameOver { get; private set; } public GameManager(IGuesser guesser) { this.guesser = guesser; } private void Start() { times = 0; IsGameOver = false; OutputGameHeader(); } public void Run() { Start(); while (!IsGameOver) { Console.WriteLine(); Console.WriteLine(string.Format("[The {0}st time ] : please input number!", times + 1)); var input = Console.ReadLine(); if (IsExit(input)) continue; var result = Guess(input); Console.WriteLine(result); } OutputGamefooter(); } private bool IsExit(string input) { if (input.ToLower().Trim() == "exit") { Console.WriteLine("Make sure to exit game?(Y/N)"); var readLine = Console.ReadLine(); if (readLine != null) { var isexit = readLine.ToLower().Trim(); if (isexit == "y") { IsGameOver = true; } } return true; } return false; } public string Guess(string input) { times++; IsGameOver = times == MAX_TIMES; var validator = new Validator(); if (!validator.Validate(input)) { return "try again " + validator.ErrorMsg; } var result = guesser.Guess(input); if (result == "4a0b") { IsGameOver = true; return "You win!"; } return "try again result is " + result + "."; } private void OutputGameHeader() { Console.Clear(); Console.WriteLine(" --- Game Start! ---"); Console.WriteLine("---------------------------------------------------------------"); Console.WriteLine("| You can input a number or input exit for exiting this game! |"); Console.WriteLine("---------------------------------------------------------------"); } private void OutputGamefooter() { Console.WriteLine("--------------------------------"); Console.WriteLine("| Game Over! [Answer] is " + guesser.AnswerNumber + " |"); Console.WriteLine("--------------------------------"); } } Program.cs class Program { static void Main(string[] args) { var isRepeatGame = false; do { IGuesser guesser = new Guesser(new AnswerGenerator()); var game = new GameManager(guesser); game.Run(); Console.WriteLine("Try again?(Y/N)"); var line = Console.ReadLine(); if (line == null) continue; var readLine = line.ToLower().Trim(); isRepeatGame = readLine == "y"; } while (isRepeatGame); } } |