????????????TDD(3)
???????????? ???????[ 2014/1/20 15:46:21 ] ???????????????
?????GameManager????????CASE PASS.
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 + ".";
}
?????2?????
???????????
?????????
???????????
????????2???
??????????GameManager???work flow??
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);
}
}
???????????е?????
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11