您的位置:首页 > 编程语言

文曲星猜数游戏,无测试代码

2009-07-08 15:34 691 查看
懒人王给了个有关测试的东西

TDD by example (1) -- 挑战

没时间搞测试,仅仅搞了个游戏实现出来。希望别让懒人王测出太多毛病。呵呵

警告一下所有同行哦,程序功能多了以后,会有很多异常参数和情况的,不能太想当然就去写代码的。

我这个程序主要是在部分位置尝试使用了Linq技术

另外一个闪光点么就是在10个数字里取4个的运算是我自己发明的,在可抽取状态在整型数的可用范围内很管用的。

如果有啥意见尽管提哦,让我好提高点水平。

代码下载

namespace 猜数字
{
class Program
{
static void Main(string[] args)
{
bool go_on=true ;
Console.WriteLine("欢迎进入猜数字游戏。");
while (go_on)
{
go_on = Play();
}
Console.WriteLine("谢谢使用");

}

private static bool Play()
{
bool end=false;
int count = 6;
Console.WriteLine("新的一局开始了");
int rand = new Random().Next(1,5041);
List<char> list=Enumerable.Range(48, 10).Select(item=>(char)item).ToList();
List<int> point = new List<int>();
point.Add(rand / 504);
point.Add((rand - point[0] * 504) / 56);
point.Add((rand - point[0] * 504 - point[1] * 56) / 7);
point.Add(rand - point[0] * 504 - point[1] * 56 - point[2] * 7);

string question="";
for (int i = 0; i < 4; i++)
{
question+=list[point[i]];
list.RemoveAt(point[i]);
}

StringBuilder builder = new StringBuilder();
builder.AppendLine("您的输入\t评估结果");
while (count > 0 && end == false)
{
Console.WriteLine("输入你的选择,退出本局请按X或x:");

string input = Console.ReadLine();
if (Checkinput(input) == true)
{
string ans = Validate(input, question);
builder.Append(input);
builder.Append("\t\t");
builder.AppendLine(ans);

Console.WriteLine(builder.ToString());
if (ans == "4A0B")
{
end = true;
}
count--;
}
else
{
if (input.ToLower() == "x")
{
break;
}
else
{
Console.WriteLine("输入错误,请重新输入。");
}
}
}
if(count>0&& end==true)
{
Console.WriteLine("你胜利了!");
}
else if (count < 0 && end == false)
{
Console.WriteLine("你失败了了!");
}
Console.WriteLine("答案是{0}",question);
Console.WriteLine("是否继续新游戏?按X或x退出");
char c = (char)Console.Read();
Console.ReadLine();
return c.ToString().ToLower() != "x";
}

private static string Validate(string input, string question)
{
char[] arr = input.ToCharArray();

int a=input.Select((c, i) =>new { Value=(c==question[i])}).Count(item=>item.Value==true);

int b = input.Select((c, i) => new { Value = (question.Contains(c)) }).Count(item => item.Value == true);

return string.Format("{0}A{1}B",a,b-a);
}

private static bool Checkinput(string input)
{
if (input.Length != 4)
return false;

char[] arr=input.ToCharArray();

if(arr.Where(item=>item<'0' || item>'9').Count()>1)
return false;

if (arr
.GroupBy(item => item)
.Select(g => new { Char = g.Key, Count = g.Count() })
.Where(item => item.Count > 1)
.Count() > 0)
return false;

return true;
}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: