Ejemplo n.º 1
0
        private void ButtonExec_Click(object sender, EventArgs e)
        {
            //入力値チェック
            string seedString = "";

            seedString = this.textBox5.Text +
                         this.textBoxHolizontalKey1.Text +
                         this.textBoxHolizontalKey2.Text +
                         this.textBoxHolizontalKey3.Text +
                         this.textBoxVarticalKey1.Text +
                         this.textBoxVarticalKey2.Text +
                         this.textBoxVarticalKey3.Text;

            int seed = 0;

            if (!int.TryParse(seedString, out seed))
            {
                return;
            }
            MagProbremsKey key = new MagProbremsKey(seed);

            //計算処理実行
            List <MagProbremsAnswer> answers = MagSolver.Solve(key);

            if (answers.Count > 0)
            {
                MagProbremsAnswer answer = answers[0];

                this.textBox1.Text = answer.UpperLeftValue.ToString();
                this.textBox2.Text = answer.UpperCenterValue.ToString();
                this.textBox3.Text = answer.UpperRightValue.ToString();

                this.textBox4.Text = answer.MiddleLeftValue.ToString();
                this.textBox6.Text = answer.MiddleRightValue.ToString();

                this.textBox7.Text = answer.LowerLeftValue.ToString();
                this.textBox8.Text = answer.LowerCenterValue.ToString();
                this.textBox9.Text = answer.LowerRightValue.ToString();
            }

            StringBuilder sb = new StringBuilder();

            foreach (MagProbremsAnswer answer in answers)
            {
                sb.AppendFormat("{1}{2}{3}{0}{4}{5}{6}{0}{7}{8}{9}{0}{0}",
                                Environment.NewLine,
                                answer.UpperLeftValue, answer.UpperCenterValue, answer.UpperRightValue,
                                answer.MiddleLeftValue, answer.MiddleCenterValue, answer.MiddleRightValue,
                                answer.LowerLeftValue, answer.LowerCenterValue, answer.LowerRightValue);
            }
            this.AnswerList.Text = sb.ToString();

            this.textBox5.Focus();
            this.textBox5.SelectAll();
        }
Ejemplo n.º 2
0
        public static List <MagProbremsAnswer> Solve(MagProbremsKey key)
        {
            List <MagProbremsAnswer> answers = new List <MagProbremsAnswer>();

            MagProbremsAnswer answer1 = new MagProbremsAnswer();

            answer1.MiddleCenterValue = key.CenterValue;

            //★1
            for (int upperCenterValue = 1; upperCenterValue <= 9; upperCenterValue++)
            {
                MagProbremsAnswer answer2 = answer1.Clone();
                answer2.UpperCenterValue = upperCenterValue;
                if (ContainsDoubledValueIn(answer2))
                {
                    continue;
                }

                //★2
                for (int lowerCenterValue = 1; lowerCenterValue <= 9; lowerCenterValue++)
                {
                    MagProbremsAnswer answer3 = answer2.Clone();
                    answer3.LowerCenterValue = lowerCenterValue;
                    if (ContainsDoubledValueIn(answer3))
                    {
                        continue;
                    }

                    //◇1中縦
                    if (answer3.KeyValueOf(MagProbremKeyPosition.CenterVartical) != key.CenterVarticalKey)
                    {
                        continue;
                    }

                    //★3
                    for (int middleLeftValue = 1; middleLeftValue <= 9; middleLeftValue++)
                    {
                        MagProbremsAnswer answer4 = answer3.Clone();
                        answer4.MiddleLeftValue = middleLeftValue;
                        if (ContainsDoubledValueIn(answer4))
                        {
                            continue;
                        }

                        //★4
                        for (int middleRightValue = 1; middleRightValue <= 9; middleRightValue++)
                        {
                            MagProbremsAnswer answer5 = answer4.Clone();
                            answer5.MiddleRightValue = middleRightValue;
                            if (ContainsDoubledValueIn(answer5))
                            {
                                continue;
                            }

                            //◇2中横
                            if (answer5.KeyValueOf(MagProbremKeyPosition.MiddleHolizontal) != key.MiddleHolizontalKey)
                            {
                                continue;
                            }

                            //★5
                            for (int upperLeftValue = 1; upperLeftValue <= 9; upperLeftValue++)
                            {
                                MagProbremsAnswer answer6 = answer5.Clone();
                                answer6.UpperLeftValue = upperLeftValue;
                                if (ContainsDoubledValueIn(answer6))
                                {
                                    continue;
                                }

                                //★6
                                for (int lowerLeftValue = 1; lowerLeftValue <= 9; lowerLeftValue++)
                                {
                                    MagProbremsAnswer answer7 = answer6.Clone();
                                    answer7.LowerLeftValue = lowerLeftValue;
                                    if (ContainsDoubledValueIn(answer7))
                                    {
                                        continue;
                                    }

                                    //◇3左縦
                                    if (answer7.KeyValueOf(MagProbremKeyPosition.LeftVartical) != key.LeftVarticalKey)
                                    {
                                        continue;
                                    }

                                    //★7
                                    for (int upperRightValue = 1; upperRightValue <= 9; upperRightValue++)
                                    {
                                        MagProbremsAnswer answer8 = answer7.Clone();
                                        answer8.UpperRightValue = upperRightValue;
                                        if (ContainsDoubledValueIn(answer8))
                                        {
                                            continue;
                                        }

                                        //◇4上横
                                        if (answer8.KeyValueOf(MagProbremKeyPosition.UpperHolizontal) != key.UpperHolizontalKey)
                                        {
                                            continue;
                                        }

                                        //★8
                                        for (int lowerRightValue = 1; lowerRightValue <= 9; lowerRightValue++)
                                        {
                                            MagProbremsAnswer answer9 = answer8.Clone();
                                            answer9.LowerRightValue = lowerRightValue;
                                            if (ContainsDoubledValueIn(answer9))
                                            {
                                                continue;
                                            }

                                            //◇5右縦
                                            if (answer9.KeyValueOf(MagProbremKeyPosition.RightVartical) != key.RightVarticalKey)
                                            {
                                                continue;
                                            }

                                            //◇6下横
                                            if (answer9.KeyValueOf(MagProbremKeyPosition.LowerHolizontal) != key.LowerHolizontalKey)
                                            {
                                                continue;
                                            }

                                            //全チェック通過した候補を回答リストに追加
                                            answers.Add(answer9);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return(answers);
        }