c#数组/随机图像帮助记忆游戏

本文关键字:帮助 记忆游戏 图像 随机 数组 | 更新日期: 2023-09-27 18:02:26

我正在尝试创建一个游戏,用户将看到一个图像,然后转到下一页,需要点击相同的图像。一种记忆游戏。我遇到的问题是随机排序数组,并能够确定他们点击了哪个图像。在我的页面上,我有五个随机填充的图像和一个提交按钮。理想情况下,我希望它检查,以确保他们选择了一个,然后比较它是什么程序选择一个(在这个例子中,我没有使用post或简单的任何东西。我可以很容易地编码,所以我只需要弄清楚为什么这不起作用)现在我只是随机选择一个值,然后尝试确定用户是否点击了同一个。但我得到混合结果有时我得到相同的数字在我的selvalue和programvalue变量。有人能帮我吗?我用c#写代码

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
            {
                Random random = new Random();
                List<string> urls = new List<string> { };
                List<int> nums = new List<int> { };
                List<string> iconnames = new List<string> 
                    {
                        "images/verimage1.png", 
                        "images/verimage2.png", 
                        "images/verimage3.png", 
                        "images/verimage4.png", 
                        "images/verimage5.png" 
                    };
                int noiconlabel = random.Next(iconnames.Count);
                switch (noiconlabel)
                {
                    case 0:
                        {
                            iconnamelabel.Text = "Newspaper";
                            HiddenField2.Value = "1";
                            break;
                        }
                    case 1:
                        {
                            iconnamelabel.Text = "iPod";
                            HiddenField2.Value = "2";
                            break;
                        }
                    case 2:
                        {
                            iconnamelabel.Text = "Star";
                            HiddenField2.Value = "3";
                            break;
                        }
                    case 3:
                        {
                            iconnamelabel.Text = "Lock";
                            HiddenField2.Value = "4";
                            break;
                        }
                    case 4:
                        {
                            iconnamelabel.Text = "Die";
                            HiddenField2.Value = "5";
                            break;
                        }
                }
                for (int a = 0; a < 5; a++)
                {
                    int index = random.Next(iconnames.Count);
                    var name = iconnames[index];
                    urls.Add(name);
                    nums.Add(index + 1);
                    iconnames.RemoveAt(index);
                }
                Image1.ImageUrl = urls[0];
                Image1.CommandArgument = nums[0].ToString();
                Image2.ImageUrl = urls[1];
                Image2.CommandArgument = nums[1].ToString();
                Image3.ImageUrl = urls[2];
                Image3.CommandArgument = nums[2].ToString();
                Image4.ImageUrl = urls[3];
                Image4.CommandArgument = nums[3].ToString();
                Image5.ImageUrl = urls[4];
                Image5.CommandArgument = nums[4].ToString();
            }
    }
    protected void Image1_Click(object sender, ImageClickEventArgs e)
    {
        ImageButton img = (ImageButton)(sender);
        HiddenField1.Value = img.CommandArgument;
    }
    protected void Image2_Click(object sender, ImageClickEventArgs e)
    {
        ImageButton img = (ImageButton)(sender);
        HiddenField1.Value = img.CommandArgument ;
    }
    protected void Image3_Click(object sender, ImageClickEventArgs e)
    {
        ImageButton img = (ImageButton)(sender);
        HiddenField1.Value = img.CommandArgument;
    }
    protected void Image4_Click(object sender, ImageClickEventArgs e)
    {
        ImageButton img = (ImageButton)(sender);
        HiddenField1.Value = img.CommandArgument;
    }
    protected void Image5_Click(object sender, ImageClickEventArgs e)
    {
        ImageButton img = (ImageButton)(sender);
        HiddenField1.Value = img.CommandArgument;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string selvalue = HiddenField1.Value;
        string programvalue = HiddenField2.Value;
        if (selvalue == null | selvalue == "")
        {
            errorverlabel.Text = "<span style='color: red; font-size: 8pt;'>No Verification Image Selected, Please try again.</span>";
        }
        else
        {
            if (selvalue != programvalue)
            {
                errorverlabel.Text = "<span style='color: red; font-size: 8pt;'>Wrong Verification Image Selected, Please try again.</span>";
            }
        }
    }
}

c#数组/随机图像帮助记忆游戏

您需要使用逻辑运算符|| (OR),而不是按位的| (OR)运算符。

改变:

if (selvalue == null | selvalue == "")

if (selvalue == null || selvalue == "")