我有一个记忆游戏,在那个游戏中,我必须检查2张卡是否相等。 我做了一个if语句,检查两个图像源是否相等,但是即使它们相等,由于某种原因它也看不到它。我尝试了“ ==”运算符和.Equal,但是它们都不起作用。我有一个名为images的目录,在该文件夹中有8张图像,其名称分别为1.png,2.png等。
这是我的代码:
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 Image card;
static int score;
private Image Image1;
private Image Image2;
private void CardClick(object sender, MouseButtonEventArgs e)
{
if (hasDelay) return;
Image card = (Image)sender;
ImageSource front = (ImageSource)card.Tag;
card.Source = front;
numberOfClicks++;
checkCards(card);
}
private void checkCards(Image card)
{
this.card = card;
if (numberOfClicks < 2 || numberOfClicks == 2)
{
if (this.Image1 == null)
{
Image1 = card;
}
else if (this.Image2 == null)
{
Image2 = card;
}
}
if (numberOfClicks == 2)
{
checkPair(Image1, Image2);
numberOfClicks = 0;
Image1 = null;
Image2 = null;
}
}
public void checkPair(Image card1, Image card2)
{
this.Image1 = card1;
this.Image2 = card2;
if (card1.Source.Equals(card2.Source))
{
score++;
}
else
{
resetCards(Image1, Image2);
}
}
private bool hasDelay;
private async void resetCards(Image card1, Image card2)
{
this.Image1 = card1;
this.Image2 = card2;
hasDelay = true;
await Task.Delay(1000);
card1.Source = new BitmapImage(new Uri("/images/back.png", UriKind.Relative));
card2.Source = new BitmapImage(new Uri("/images/back.png", UriKind.Relative));
hasDelay = false;
}
public List<ImageSource> GetImagesList()
{
List<ImageSource> images = new List<ImageSource>();
List<string> random1 = new List<string>();
List<string> random2 = new List<string>();
for (int i = 0; i < 16; i++)
{
if (i < 8)
{
int imageNR = 0;
Random rnd = new Random();
imageNR = rnd.Next(1, 9);
if (random1.Contains(Convert.ToString(imageNR)))
{
i--;
}
else
{
random1.Add(Convert.ToString(imageNR));
ImageSource source = new BitmapImage(new Uri("images/" + imageNR + ".png", UriKind.Relative));
images.Add(source);
}
}
if (i >= 8)
{
int imageNR = 0;
Random rnd = new Random();
imageNR = rnd.Next(1, 9);
if (random2.Contains(Convert.ToString(imageNR)))
{
i--;
}
else
{
random2.Add(Convert.ToString(imageNR));
ImageSource source = new BitmapImage(new Uri("images/" + imageNR + ".png", UriKind.Relative));
images.Add(source);
}
}
}
return images;
}
}