我怎么知道点击了哪张卡?

时间:2018-10-16 18:59:22

标签: c# wpf xaml

我正在用WPF和C#制作一个记忆游戏。到现在为止一切都很好。当我单击(翻转)两张卡片时,我希望我的代码注册该卡片,并且当图像不匹配时,我希望back.png图像回来。

现在,我的代码计算点击次数,但是我不知道如何在两张图像匹配时使卡片再次“转”并使其消失。我有16张图像,1和9是对,2和10是对,依此类推。

我的计划是制作一个称为resetCards()的方法。

这是我的MainWindow.cs:

    public partial class MainWindow : Window
{
    private MemoryGrid grid;

    public MainWindow()
    {
        InitializeComponent();
    }

    private void start_Click(object sender, RoutedEventArgs e)
    {
        grid = new MemoryGrid(GameGrid, 4, 4);
        start.Visibility = Visibility.Collapsed;
    }

这是我的MemoryGrid.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace SpellenScherm
{
public class MemoryGrid
{
    private Grid grid;
    private int rows, cols;

    public MemoryGrid(Grid grid, int rows, int cols)
    {
        this.grid = grid;
        this.rows = rows;
        this.cols = cols;

        InitializeGrid();
        AddImages();
    }


    private void InitializeGrid()
    {
        for (int i = 0; i < rows; i++)
        {
            grid.RowDefinitions.Add(new RowDefinition());
        }
        for (int i = 0; i < cols; i++)
        {
            grid.ColumnDefinitions.Add(new ColumnDefinition());
        }
    }

    private void AddImages()
    {
        List<ImageSource> images = GetImagesList();
        for (int row = 0; row < rows; row++)
        {
            for (int col = 0; col < cols; col++)
            {
                Image back = new Image();
                back.Source = new BitmapImage(new Uri("/images/back.png", UriKind.Relative));

                back.MouseDown += new System.Windows.Input.MouseButtonEventHandler(CardClick);

                back.Tag = images.First();
                images.RemoveAt(0);                                 
                Grid.SetColumn(back, col);
                Grid.SetRow(back, row);
                grid.Children.Add(back);
            }
        }
    }

    static int numberOfClicks = 0;
    private void resetCards()
    {

    }

    private void CardClick(object sender, MouseButtonEventArgs e)
    {

        if (numberOfClicks < 2)
        {
            Image card = (Image)sender;
            ImageSource front = (ImageSource)card.Tag;
            card.Source = front;
            numberOfClicks++;

        }
        if (numberOfClicks == 2)
        {

            resetCards();

            numberOfClicks = numberOfClicks -2;
        }
    }

    public List<ImageSource> GetImagesList()
    {
        List<ImageSource> images = new List<ImageSource>();
        List<string> random = new List<string>();

        for (int i = 0; i < 16; i++)
        {

            int imageNR = 0;

            Random rnd = new Random();
            imageNR = rnd.Next(1, 17);
            if (random.Contains(Convert.ToString(imageNR)))
            {
                i--;
            }
            else
            {
                random.Add(Convert.ToString(imageNR));
                ImageSource source = new BitmapImage(new Uri("images/" + imageNR + ".png", UriKind.Relative));
                images.Add(source);
            }              
        }
        return images;
    }
}
}

1 个答案:

答案 0 :(得分:0)

您可以尝试这种方法-在MemoryGrid类中为每个显示其正面的图像保留两个字段。 (我们称它们为Image1和Image2)。然后,您可以跟踪哪些卡片在整个网格中被翻转,并将它们作为参数传递给resetCards方法,如下所示:

private void CardClick(object sender, MouseButtonEventArgs e)
{

    if (numberOfClicks < 2)
    {
        Image card = (Image)sender;
        ImageSource front = (ImageSource)card.Tag;
        card.Source = front;
        if(this.Image1 == null){
           Image1 = card;
        }
        else if(this.Image2 == null){
           Image2 = card;
        }
        numberOfClicks++;

    }
    if (numberOfClicks == 2)
    {

        resetCards(Image1, Image2);

        numberOfClicks = numberOfClicks -2;
    }
}