/// <summary> /// 绘制带有地雷格子 /// </summary> private void CreatLoadMine() { for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { loadMind = new LoadMind(); loadMind.X = i; loadMind.Y = j; backborder[i, j] = new Border(); backborder[i, j].MouseLeftButtonDown += MainWindow_MouseLeftButtonDown; backborder[i, j].MouseRightButtonDown += MainWindow_MouseRightButtonDown; backborder[i, j].Background = Brushes.White; backborder[i, j].BorderThickness = new Thickness(1); backborder[i, j].BorderBrush = Brushes.Black; backborder[i, j].Tag = loadMind; backborder[i, j].Width = 60; backborder[i, j].Height = 50; Canvas.SetLeft(backborder[i, j], i * backborder[i, j].Width); Canvas.SetTop(backborder[i, j], j * backborder[i, j].Height); Map.Children.Add(backborder[i, j]); Label label = new Label(); label.Width = backborder[i, j].Width; label.Height = backborder[i, j].Height; label.Content = ""; backborder[i, j].Child = label; } } }
private void JudgeMind(Border border) { LoadMind lm = ((LoadMind)border.Tag); //如果是1就是雷 if (lm.Ismind == true) { for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { if (((LoadMind)backborder[i, j].Tag).Ismind == true) { ImageBrush image = new ImageBrush(); image.ImageSource = new BitmapImage(new Uri(@"..\..\img\Map\mind.jpg", UriKind.Relative)); backborder[i, j].Background = image; } } } timer.Stop(); MessageBox.Show("Game Over!很遗憾,再接再厉!"); Map.Children.Clear(); Map.Background = Brushes.White; return; } else { border.Background = Brushes.Gray; if (lm.Y - 1 >= 0 && ((LoadMind)backborder[lm.X, lm.Y - 1].Tag).Ismind == true) { //有雷 计数 //return; lm.Count++; } // if (lm.X - 1 >= 0 && lm.Y - 1 >= 0 && ((LoadMind)backborder[lm.X - 1, lm.Y - 1].Tag).Ismind == true) { //有雷 计数 //return; lm.Count++; } if (lm.X - 1 >= 0 && ((LoadMind)backborder[lm.X - 1, lm.Y].Tag).Ismind == true) { //有雷 计数 //return; lm.Count++; } if (lm.X - 1 >= 0 && lm.Y + 1 < 10 && ((LoadMind)backborder[lm.X - 1, lm.Y + 1].Tag).Ismind == true) { //有雷 计数 //return; lm.Count++; } if (lm.Y + 1 < 10 && ((LoadMind)backborder[lm.X, lm.Y + 1].Tag).Ismind == true) { //有雷 计数 //return; lm.Count++; } if (lm.X + 1 < 10 && lm.Y + 1 < 10 && ((LoadMind)backborder[lm.X + 1, lm.Y + 1].Tag).Ismind == true) { //有雷 计数 //return; lm.Count++; } if (lm.X + 1 < 10 && ((LoadMind)backborder[lm.X + 1, lm.Y].Tag).Ismind == true) { //有雷 计数 //return; lm.Count++; } if (lm.X + 1 < 10 && lm.Y - 1 >= 0 && ((LoadMind)backborder[lm.X + 1, lm.Y - 1].Tag).Ismind == true) { //有雷 计数 //return; lm.Count++; } //左边 if (lm.Y > 0 && ((LoadMind)backborder[lm.X, lm.Y - 1].Tag).Ismind == false && backborder[lm.X, lm.Y - 1].Background == Brushes.White) { if (lm.Count == 0) { JudgeMind(backborder[lm.X, lm.Y - 1]); } } //左上 if (lm.X > 0 && lm.Y > 0 && ((LoadMind)backborder[lm.X - 1, lm.Y - 1].Tag).Ismind == false && backborder[lm.X - 1, lm.Y - 1].Background == Brushes.White) { if (lm.Count == 0) { JudgeMind(backborder[lm.X - 1, lm.Y - 1]); } } //上 if (lm.X > 0 && ((LoadMind)backborder[lm.X - 1, lm.Y].Tag).Ismind == false && backborder[lm.X - 1, lm.Y].Background == Brushes.White) { if (lm.Count == 0) { JudgeMind(backborder[lm.X - 1, lm.Y]); } } //右上 if (lm.X > 0 && lm.Y < 9 && ((LoadMind)backborder[lm.X - 1, lm.Y + 1].Tag).Ismind == false && backborder[lm.X - 1, lm.Y + 1].Background == Brushes.White) { if (lm.Count == 0) { JudgeMind(backborder[lm.X - 1, lm.Y + 1]); } } //右边 if (lm.Y < 9 && ((LoadMind)backborder[lm.X, lm.Y + 1].Tag).Ismind == false && backborder[lm.X, lm.Y + 1].Background == Brushes.White) { if (lm.Count == 0) { JudgeMind(backborder[lm.X, lm.Y + 1]); } } //右下 if (lm.X < 9 && lm.Y < 9 && ((LoadMind)backborder[lm.X + 1, lm.Y + 1].Tag).Ismind == false && backborder[lm.X + 1, lm.Y + 1].Background == Brushes.White) { if (lm.Count == 0) { JudgeMind(backborder[lm.X + 1, lm.Y + 1]); } } //下 if (lm.X < 9 && ((LoadMind)backborder[lm.X + 1, lm.Y].Tag).Ismind == false && backborder[lm.X + 1, lm.Y].Background == Brushes.White) { if (lm.Count == 0) { JudgeMind(backborder[lm.X + 1, lm.Y]); } } //左边下 if (lm.X < 9 && lm.Y > 0 && ((LoadMind)backborder[lm.X + 1, lm.Y - 1].Tag).Ismind == false && backborder[lm.X + 1, lm.Y - 1].Background == Brushes.White) { if (lm.Count == 0) { JudgeMind(backborder[lm.X + 1, lm.Y - 1]); } } } //总共的雷数量显示 if (lm.Count > 0) { if (lm.Iskey == false) { Label lb = (Label)border.Child; lb.Content = lm.Count; lb.HorizontalContentAlignment = HorizontalAlignment.Center; lb.VerticalContentAlignment = VerticalAlignment.Center; lb.FontSize = 28; if (lb.Content.ToString() == "1") { lb.Foreground = Brushes.Red; } else if (lb.Content.ToString() == "2") { lb.Foreground = Brushes.Blue; } else if (lb.Content.ToString() == "3") { lb.Foreground = Brushes.Green; } else { lb.Foreground = Brushes.Purple; } lm.Iskey = true; } else if (lm.Iskey == true) { return; } } }