文曲星上的猜数字游戏实现起来比较简单。下面这个例子中,生成number时用集合list提高效率,验证输入是否合法和验证结果正确用hashtable提高效率。相比用数组循环来说会好了很多。

private static Hashtable hstableCheckSame = new Hashtable(); //用来检查输入答案是否有重复的数字
private static Hashtable hstableAnswer = new Hashtable(); //用来存放随机生成的结果
private const int INT_TRY_TIMES = 10;
public static Hashtable HstableAnswer
...{
get ...{ return hstableAnswer; }
}
public static void Main(string[] args)
...{
Console.WriteLine("***************** Game Guess Number ***************** ");
Console.WriteLine("The result will be given in the way of "2A1B".");
Console.WriteLine(""A" means the value and position both are correct.");
Console.WriteLine(""B" means the value is correct but the position is not.");
Console.WriteLine("You have total " + INT_TRY_TIMES + " times to guess the number.");
Console.WriteLine(" ***************************************************** ");
while (true)
...{
Console.WriteLine("Enter "start" to begin the game or "end" to exit.");
switch (Console.ReadLine().ToLower())
...{
case "start":
if (BeginGame() == false)
...{
goto End;
}
break;
case "end":
goto End;
default:
break;
}
}
End:
Console.WriteLine(" ************* End ****************");
}
private static bool BeginGame()
...{
//随机生成一个结果
string strAnswer = Generate();
for (int i = 0; i < INT_TRY_TIMES; i++)
...{
System.Console.Write("Please input your guess (or enter "end" to end the game):");
string strInput = System.Console.ReadLine();
//判断是否要退出游戏
if (strInput.Trim().ToLower() == "end")
...{
return false;
}
//检查输入是否合法
if (CheckValid(strInput) == false)
...{
i--;
continue;
}
//检查输入的答案是否正确
if (CheckInput(strInput, strAnswer) == true)
...{
System.Console.WriteLine(" >>> Well done.Congratulations! ");
break;
}
else
...{
if (i >= INT_TRY_TIMES - 1)
...{
System.Console.WriteLine(" >>> You lose.The answer is " + strAnswer + " ");
}
else
...{
System.Console.WriteLine(" Chances left: " + (INT_TRY_TIMES - 1 - i) + ". Good luck! ");
}
}
}
return true;
}
private static string Generate()
...{
string strNumberGenerated = string.Empty;
//初始化数字列表
List<string> numberList = new List<string>();
for (int intNum = 0; intNum < 10; intNum++)
...{
numberList.Add(intNum.ToString());
}
//随机生成一个结果
Random random = new Random(DateTime.Now.Millisecond);
hstableAnswer.Clear();
for (int i = 0; i < 4; i++)
...{
int intIndex = random.Next(numberList.Count);
strNumberGenerated += numberList[intIndex];
hstableAnswer.Add(numberList[intIndex], numberList[intIndex]); //添加到hashtable
numberList.RemoveAt(intIndex);
}
return strNumberGenerated;
}
private static bool CheckInput(string strInput, string strAnswer)
...{
int intValuePosition = 0; //数值和所处位置都正确的数字个数
int intValue = 0; //数值正确但是所处位置不正确的数字个数
for (int i = 0; i < 4; i++)
...{
if (strInput[i] == strAnswer[i])
...{
//数值和所处位置都正确
intValuePosition++;
}
else
...{
//数值正确但是所处位置不正确
if (hstableAnswer[strInput[i].ToString()] != null)
...{
intValue++;
}
}
}
//输出判断结果
System.Console.WriteLine(" Result : " + intValuePosition + "A" + intValue + "B");
if (intValuePosition >= 4)
...{
return true;
}
return false;
}
private static bool CheckValid(string strInput)
...{
//检查输入的长度是否合法
if (strInput.Trim().Length != 4)
...{
System.Console.WriteLine(" Input Error:Please input 4 digits. ");
return false;
}
//检查输入是否数字
try
...{
int.Parse(strInput);
}
catch
...{
System.Console.WriteLine(" Input Error:Only digits allowed. ");
return false;
}
//检查是否有重复的数字
hstableCheckSame.Clear();
for (int i = 0; i < strInput.Length; i++)
...{
if (hstableCheckSame[strInput[i]] == null)
...{
hstableCheckSame.Add(strInput[i], strInput[i]);
}
else
...{
System.Console.WriteLine(" Input Error:Each digit can only be used once");
return false;
}
}
return true;
}
本文介绍了一种使用哈希表和列表来高效实现猜数字游戏的方法。通过哈希表检查输入答案是否有重复数字,并验证结果正确性。游戏提供10次猜测机会,每次给出“A”和“B”的提示。

被折叠的 条评论
为什么被折叠?



