namespace 解猜谜结果游戏
{
class Program
{
static string[] winers = new string[5];//一百米、二百米、跳高、跳远和铅球
static List<string> nameList = new List<string> { "甲", "乙", "丙", "丁", "戊" };
static int count = 0;
static void Main(string[] args)
{
GetAllPossibleItem(winers,nameList);
Console.ReadLine();
}
private static void GetAllPossibleItem(string[] winers, List<string> nameList,int index=0)
{
if (index == winers.Length)
{
if (Test(winers))
{
Console.WriteLine("第{0}种符合要求的可能情况:{1}", ++count,string.Join(",", winers));
}
return;
}
for (int i = 0, len = nameList.Count; i < len;i++ )
{
string name = nameList[i];
winers[index] = name;
var newNameList = string.Join(",", nameList).Split(',').ToList();
newNameList.RemoveAt(i);
GetAllPossibleItem(winers, newNameList, index + 1);
}
}
/// <summary>
/// 数据是否同时满足所有要求
/// </summary>
/// <param name="winers">数据</param>
/// <returns>true,符合要求,false,不符合要求</returns>
private static bool Test(string[] winers)
{
if (TestA(winers)&&TestB(winers)&&TestC(winers)&&TestD(winers))
{
return true;
}
return false;
}
/// <summary>
/// A说:“乙获得铅球冠军,丁获得跳高冠军”
/// </summary>
/// <returns>true,符合要求,false,不符合要求</returns>
private static bool TestA(string[] winers)
{
bool a =winers[4] == "乙";
bool b =winers[2] == "丁";
return OneTrueOneFalse(a, b);
}
/// <summary>
/// B说:“甲获得一百米冠军,戊获得跳远冠军”
/// </summary>
/// <returns>true,符合要求,false,不符合要求</returns>
private static bool TestB(string[] winers)
{
bool a = winers[0] == "甲";
bool b = winers[3] == "戊";
return OneTrueOneFalse(a, b);
}
/// <summary>
/// C说:“丙获得跳远冠军,丁获得二百米冠军”
/// </summary>
/// <returns>true,符合要求,false,不符合要求</returns>
private static bool TestC(string[] winers)
{
bool a = winers[3] == "丙";
bool b = winers[1] == "丁";
return OneTrueOneFalse(a, b);
}
/// <summary>
/// D说:“乙获得跳高冠军,戊获得铅球冠军”
/// </summary>
/// <returns>true,符合要求,false,不符合要求</returns>
private static bool TestD(string[] winers)
{
bool a = winers[4] == "戊";
bool b = winers[2] == "乙";
return OneTrueOneFalse(a, b);
}
/// <summary>
/// 是否符合每个人的描述都对一句,错一句
/// </summary>
/// <param name="a">第一句真假</param>
/// <param name="b">第二句真假</param>
/// <returns>true,符合要求,false,不符合要求</returns>
private static bool OneTrueOneFalse(bool a,bool b)
{
if (a && !b) return true;
if (b && !a) return true;
return false;
}
}
}