C# 如何搜索两个相似的图像(记忆游戏)

C# 如何搜索两个相似的图像(记忆游戏),c#,xaml,compare,shuffle,C#,Xaml,Compare,Shuffle,我试着搜索两张相似的图片(比如在记忆游戏中) 我知道怎么做,但我想这肯定不是最好的方法。。。 谁有更好的解决方案 下面是我如何做到这一点的代码 private void first_button (object sender, System.Windows.Input.GestureEventArgs e) //so button get pressed { field_one = true; StoryboardBack11.Begin();

我试着搜索两张相似的图片(比如在记忆游戏中) 我知道怎么做,但我想这肯定不是最好的方法。。。 谁有更好的解决方案

下面是我如何做到这一点的代码

private void first_button (object sender, System.Windows.Input.GestureEventArgs e)  //so button get pressed
    {
        field_one = true;

        StoryboardBack11.Begin();
        StoryboardBack11.Completed += new EventHandler(AfterAnimation);
        i = i + 1;
    }

// same for the other buttons

private void AfterAnimation(object sender, EventArgs e)    //check here after storyboard completed
    {if (i == 2)     // check after 2 clicks
        {
            i = 0;

            if (field_one == true && field_two == true)
            {
                if (imageUri_one.Equals(imageUri_two))
                    {
                        Fade_one.Begin();    //Storyboard starts
                        Fade_two.Begin();
                        field_one = false;
                        field_two = false;

                    }
            }

//and this for every possible combination 
//-> here is my problem because it is a lot to copy paste and thus not optimal i guess
            if (field_one == true && field_three == true)     
            {
                if (imageUri_one.Equals(imageUri_three))
                {
                    Fade_one.Begin();
                    Fade_three.Begin();
                    field_one = false;
                    field_three = false;

                }
            }
// here is the shuffeling of the images
private void test1(object sender, System.Windows.Input.GestureEventArgs e)
    {

        List<string> Test = new List<string>();   //list of the images
        Test.Add("Art1.png");
        Test.Add("Art1.png");
        Test.Add("Art2.png");
        Test.Add("Art2.png");
        Test.Add("Art3.png");
        Test.Add("Art3.png");

        Test.Shuffle();   //they get randomly shuffled here and then realigned


        imageUri_one = Test[0];
        imageUri_two = Test[1];
        imageUri_three = Test[2];
            // ..... and so on


        // put the new image Uri to a button -> the same 6 time for everey button
        BitmapImage bm_one = new BitmapImage(new Uri(@imageUri_one, UriKind.RelativeOrAbsolute));
        image11.Source = bm_one; //but it in xaml Source




}
private void first_按钮(对象发送者,System.Windows.Input.GestureEventArgs e)//因此按钮被按下
{
字段1=真;
故事板背景11.Begin();
StoryboardBack11.Completed+=新事件处理程序(AfterAnimation);
i=i+1;
}
//其他按钮也一样
私有void AfterAnimation(对象发送者,EventArgs e)//在故事板完成后检查此处
{if(i==2)//单击两次后检查
{
i=0;
if(field_one==true&&field_two==true)
{
如果(imageUri_1.等于(imageUri_2))
{
Fade_one.Begin();//故事板开始
淡出2.开始;
字段1=假;
字段2=假;
}
}
//这是每一种可能的组合
//->这是我的问题,因为它是很多复制粘贴,因此不是最佳的,我想
if(field_one==true&&field_three==true)
{
如果(imageUri_1.等于(imageUri_3))
{
淡出一个。开始();
第三步,开始;
字段1=假;
字段_三=假;
}
}
//下面是这些图像的处理
私有void test1(对象发送方,System.Windows.Input.GestureEventArgs e)
{
列表测试=新建列表();//图像列表
添加测试(“Art1.png”);
添加测试(“Art1.png”);
添加测试(“Art2.png”);
添加测试(“Art2.png”);
添加测试(“Art3.png”);
添加测试(“Art3.png”);
Test.Shuffle();//它们在这里被随机洗牌,然后重新排列
imageUri_one=测试[0];
imageUri_two=测试[1];
imageUri_three=测试[2];
//……等等
//将新图像Uri放入一个按钮->相同的6次Every按钮
BitmapImage bm_one=新的BitmapImage(新Uri(@imageUri_one,UriKind.RelativeOrAbsolute));
image11.Source=bm_one;//但它在xaml源中
}
所以问题是我想让Uri变得灵活,这样我可以在使用另一个列表时洗牌不同的Uri,这应该已经起作用了? 希望问题清楚:)

伪代码:

public class MyElement
{
    public bool IsPressed { get; set; }
    public string Uri { get; set; }
    ... anything else you want to know about that one tile
}
然后构建逻辑板

MyElement[,] tiles = new MyElement[8, 8];
for(int i = 0; i < 8; i++)
{
    for(j = 0; j < 8; j++)
    {
        tiles[i, j] = new MyElement { IsPressed = false; Uri = whatever; ... };
    }
}
MyElement[,]tiles=新的MyElement[8,8];
对于(int i=0;i<8;i++)
{
对于(j=0;j<8;j++)
{
tiles[i,j]=newmyelement{IsPressed=false;Uri=whatever;…};
}
}
现在用按钮创建“物理”板,并为包含其坐标的所有按钮指定自定义值


创建一个单击处理程序,在其中检查该坐标的平铺数组并检查其MyElement对象。将此单击处理程序分配给所有按钮单击事件。

不要对所有按钮和imageUri使用命名变量。使用自定义对象(包含按钮和imageUri)的二维数组(表示板)之类的东西.这可能更好..所以一个数组表示是否按下按钮,然后另一个数组中有Uri?然后比较Uri数组的数组位置,在真数组或假数组中是真的?或者类似的?类似的。但是使用一个数组,其中包含包含所有需要信息的元素。因此n数组,其中每个元素都有一个IsPressed值、一个URI值以及您需要的所有其他内容。