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

时间:2014-09-14 14:19:47

标签: c# xaml compare shuffle

我尝试搜索2个相似的图像(比如在记忆游戏中) 我知道该怎么做,但它肯定不是我想的最好的方式...... 谁有更好的解决方案?

这是代码我是如何做到的

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




}

所以问题是我想让Uri变得灵活,这样我就可以在使用另一个列表时改变不同的Uri,这应该已经有效了。但有没有更优雅的方式来检查类似的图像? 希望问题很清楚:)

1 个答案:

答案 0 :(得分:0)

的伪代码:

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; ... };
    }
}

现在创建&#34; physical&#34;使用按钮登录并为包含其坐标的所有按钮分配自定义值。

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