public EinsteinResult Run()
        {
            this.Init();
            var r = EinsteinResult.Failure;

            this._stopwatch.Start();

            /*
             *  產生 Person 資料
             */
            for (var houseColor = 1; houseColor <= 5; houseColor++)
            {
                for (var nationality = 1; nationality <= 5; nationality++)
                {
                    for (var pet = 1; pet <= 5; pet++)
                    {
                        for (var beverage = 1; beverage <= 5; beverage++)
                        {
                            for (var cigaret = 1; cigaret <= 5; cigaret++)
                            {
                                var person = new Person(houseColor, nationality, pet, beverage, cigaret);

                                //使用逆否命題判斷關連
                                if (Contrapositive(person))
                                {
                                    //分類

                                    //如果國籍為挪威,存入 _1 序列中
                                    if (person.Nationality == Nationality.Norway)
                                    {
                                        this._1.Add(person);
                                    }

                                    //如果房屋為藍色,存入 _2 序列中
                                    else if (person.HouseColor == HouseColor.Blue)
                                    {
                                        this._2.Add(person);
                                    }

                                    //如果喜歡的飲料為牛奶,存入 _3 序列中
                                    else if (person.Beverage == Beverage.Milk)
                                    {
                                        this._3.Add(person);
                                    }

                                    //剩餘無法分類的存進 _45 序列中,後面要做交集用
                                    else
                                    {
                                        this._45.Add(person);
                                    }
                                }
                            }
                        }
                    }
                }
            }

            foreach (var p1 in _1)
            {
                //鎖定一號位置
                this._persons[1] = p1;
                foreach (var p2 in _2)
                {
                    //鎖定二號位置
                    this._persons[2] = p2;

                    //確定 1~2號 符合條件
                    if (PropertiesChecker(1, 2))
                    {
                        foreach (var p3 in _3)
                        {
                            //鎖定三號位置
                            this._persons[3] = p3;

                            //確定 1~3號 符合條件
                            if (PropertiesChecker(1, 3))
                            {
                                foreach (var p4 in _45)
                                {
                                    //鎖定四號位置
                                    this._persons[4] = p4;

                                    //確定 1~4號 符合條件
                                    if (PropertiesChecker(1, 4))
                                    {
                                        foreach (var p5 in _45)
                                        {
                                            //鎖定五號位置
                                            _persons[5] = p5;

                                            //確定 1~5號 符合條件
                                            if (PropertiesChecker(1, 5) && RelativePosition())
                                            {
                                                //成功
                                                this._stopwatch.Stop();
                                                r = EinsteinResult.Success(this._persons.Skip(1).Take(5), this._stopwatch.Elapsed);
                                                this.OnRunCompleted?.Invoke(r);
                                                return(r);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            //失敗
            this._stopwatch.Stop();
            this.OnRunCompleted?.Invoke(r);
            return(r);



            bool PropertiesChecker(int min, int max)
            {
                for (int i = min; i < max; i++)
                {
                    if (this._persons[max].PartitalEquals(this._persons[i]))
                    {
                        return(false);
                    }
                }
                return(true);
            }
        }
Example #2
0
 static EinsteinResult()
 {
     Failure = new EinsteinResult();
 }