解猜数字(XAXB)

我的想法是:把所有备选答案当做正确答案和猜的数字对比,如果得出XAXB和给出的XAXB相同则保留

代码

        int a = 0;
        int b = 0;
        List<string> number = new List<string>();

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox2.Text == "")
            {
                return;
            }
            if (button1.Text == "重置")
            {
                init();
                return;
            }
            try
            {
                string[] state = textBox2.Text.Split('.');
                a = Int32.Parse(state[0]);
                b = Int32.Parse(state[1]);
                if (a + b > 4 || a < 0 || b < 0)
                {
                    return;
                }
                if (a == 4)
                {
                    button1.Text = "重置";
                    return;
                }

                for (int i = number.Count - 1; i >= 0; i--)
                {
                    if (check(number[i], number[0], a, b))
                    {
                        continue;
                    }
                    number.RemoveAt(i);
                }

                richTextBox1.Text = "";
                for (int i = 0; i < number.Count; i++)
                {
                    richTextBox1.Text += number[i] + "
";
                }
                textBox2.Text = "";
                textBox1.Text = number[0];
            }
            catch
            {
                return;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            init();
        }

        private void init()
        {
            a = 0;
            b = 0;
            button1.Text = "确定";
            number.Clear();
            textBox1.Text = "";
            textBox2.Text = "";
            richTextBox1.Text = "";
            for (int aa = 0; aa < 10; aa++)
            {
                for (int bb = 0; bb < 10; bb++)
                {
                    if (bb == aa) continue;
                    for (int cc = 0; cc < 10; cc++)
                    {
                        if (cc == aa || cc == bb) continue;
                        for (int dd = 0; dd < 10; dd++)
                        {
                            if (dd == aa || dd == bb || dd == cc) continue;
                            number.Add(aa + "," + bb + "," + cc + "," + dd);
                        }
                    }
                }
            }
            textBox1.Text = number[0];
        }

        private bool check(string num, string guess, int a, int b)
        {
            int a1 = 0;
            int b1 = 0;
            string[] nums = num.Split(',');
            string[] guesss = guess.Split(',');
            for (int i = 0; i < nums.Length; i++)
            {
                for (int j = 0; j < guesss.Length; j++)
                {
                    if (nums[i] == guesss[j])
                    {
                        if (i == j)
                        {
                            a1++;
                        }
                        else
                        {
                            b1++;
                        }
                    }
                }
            }
            if (a == a1 && b == b1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                button1.PerformClick();
            }
        }
View Code

展现页面

原文地址:https://www.cnblogs.com/margin-gu/p/5354063.html