Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 设计一个猜测卡号的算法_C#_Algorithm - Fatal编程技术网

C# 设计一个猜测卡号的算法

C# 设计一个猜测卡号的算法,c#,algorithm,C#,Algorithm,我正在做一个猜牌游戏。有100张牌,分为10行10列,每张牌上都有一个数字,用户必须找到他想的数字。我想设计一种算法,通过打开不到20张卡片来确定一个给定的数字是否写在其中一张卡片上。我已经动态创建了按钮,现在我很难通过逻辑来搜索它们。这是我的代码 namespace WindowsFormsApplication3 { public partial class Form1 : Form { int rememberlast = 0, move = 0;

我正在做一个猜牌游戏。有100张牌,分为10行10列,每张牌上都有一个数字,用户必须找到他想的数字。我想设计一种算法,通过打开不到20张卡片来确定一个给定的数字是否写在其中一张卡片上。我已经动态创建了按钮,现在我很难通过逻辑来搜索它们。这是我的代码

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        int rememberlast = 0, move = 0;
        object savelastobject;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            int sizee = 50, where = 0;
            this.Height = 0;
            this.Width = 0;
            var generatedNum = new List<int>();
            var random = new Random();

            while (generatedNum.Count < 100)
            {
                var tempo = random.Next(0, 100);

                if (generatedNum.Contains(tempo)) continue;
                generatedNum.Add(tempo);
            }
            char[] text2 = text.ToCharArray();
            for (int x = 0; x < 10; x++)
            {
                for (int y = 0; y < 10; y++)
                {
                    Button ta = new Button();
                    ta.Name = x.ToString()+y.ToString();
                    ta.Width = sizee;
                    ta.Height = sizee;
                    ta.Tag = generatedNum[where];

                    where++;
                    ta.BackColor = Color.Red;
                    ta.Location = new Point(70 * y, 70 * x);
                    Controls.Add(ta);
                    ta.Click += new System.EventHandler(this.button_Click);

                    this.Width += 90 * x / 13;
                    this.Height += 90 * x / 8;
                }
            }
        }
        private void button_Click(object sender, EventArgs e)
        {
            Control me = (Control)sender;

                rememberlast = int.Parse(me.Tag.ToString());
                savelastobject = me;
                me.Text = me.Tag.ToString();
                me.Enabled = true;
                me.BackColor = Color.Gray;
                move++;
                label2.Text = move.ToString();

                me.Enabled = true;
                me.Text = me.Tag.ToString();
                me.BackColor = Color.Gray;
                me.Refresh();
                Thread.Sleep(1000);
                if (move == 20)
                {
                    MessageBox.Show("Total Moves Consumed");
                    Application.Restart();
                }
                if (me.Tag.ToString() == textBox1.Text)
                {
                    //Control him = (Control)savelastobject;
                    //him.BackColor = Color.LightBlue;
                    me.BackColor = Color.LightBlue;
                    MessageBox.Show("You Win");
                    Application.Restart();
                }
         }

    }
}
命名空间窗口窗体应用程序3
{
公共部分类Form1:Form
{
int rememberlast=0,move=0;
对象savelastobject;
公共表格1()
{
初始化组件();
}
私有void Form1\u加载(对象发送方、事件参数e)
{
int-sizee=50,其中=0;
这个。高度=0;
这个。宽度=0;
var generatedNum=新列表();
var random=新的random();
while(generatedNum.Count<100)
{
var tempo=random.Next(01100);
如果(generatedNum.Contains(tempo))继续;
generatedNum.Add(节奏);
}
char[]text2=text.ToCharArray();
对于(int x=0;x<10;x++)
{
对于(int y=0;y<10;y++)
{
按钮ta=新按钮();
ta.Name=x.ToString()+y.ToString();
ta.宽度=尺寸;
ta.高度=尺寸;
ta.Tag=generatedNum[where];
where++;
ta.BackColor=Color.Red;
ta.位置=新点(70*y,70*x);
添加(ta);
ta.Click+=newsystem.EventHandler(此.button\u单击);
宽度+=90*x/13;
该高度+=90*x/8;
}
}
}
私有无效按钮\u单击(对象发送者,事件参数e)
{
控制me=(控制)发送方;
rememberlast=int.Parse(me.Tag.ToString());
savelastobject=me;
me.Text=me.Tag.ToString();
me.Enabled=true;
me.BackColor=Color.Gray;
move++;
label2.Text=move.ToString();
me.Enabled=true;
me.Text=me.Tag.ToString();
me.BackColor=Color.Gray;
我。刷新();
睡眠(1000);
如果(移动==20)
{
MessageBox.Show(“消耗的移动总数”);
Application.Restart();
}
if(me.Tag.ToString()==textBox1.Text)
{
//Control him=(Control)savelastobject;
//him.BackColor=Color.LightBlue;
me.BackColor=Color.LightBlue;
MessageBox.Show(“你赢了”);
Application.Restart();
}
}
}
}

到底是什么问题?如果您需要随机排列100个数字,则使用a将比选择一个随机数字并查看它是否已被使用更好。如果您随机排列100个数字,您可能需要检查99张卡以确定所选号码的位置。您需要某种方法来缩小选项的数量,只需问“这是您的卡吗?”,最多可以使用99张卡,预计需要大约50张卡。你需要像“更高或更低”、“上、下、左或右”这样的东西,以便更快地找到所需的卡。