privatestatic Hashtable hstableCheckSame =new Hashtable(); //用来检查输入答案是否有重复的数字 privatestatic Hashtable hstableAnswer =new Hashtable(); //用来存放随机生成的结果 privateconstint INT_TRY_TIMES =10; publicstatic Hashtable HstableAnswer ...{ get...{ return hstableAnswer; } } publicstaticvoid 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 ****************"); } privatestaticbool 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") ...{ returnfalse; } //检查输入是否合法 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! "); } } } returntrue; } privatestaticstring 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; } privatestaticbool 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) ...{ returntrue; } returnfalse; } privatestaticbool CheckValid(string strInput) ...{ //检查输入的长度是否合法 if (strInput.Trim().Length !=4) ...{ System.Console.WriteLine(" Input Error:Please input 4 digits. "); returnfalse; } //检查输入是否数字 try ...{ int.Parse(strInput); } catch ...{ System.Console.WriteLine(" Input Error:Only digits allowed. "); returnfalse; } //检查是否有重复的数字 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"); returnfalse; } } returntrue; }