/// <summary> /// 初始化雷区 /// </summary> /// <param name="panLineNum"> 方格边长</param> /// <param name="mineCount"> 地雷数量</param> public void Init(int panLineNum, int mineCount) { //放置方格,布局方格位置 for (int i = 0; i < panLineNum; i++) { for (int j = 0; j < panLineNum; j++) { UserButton userButton = new UserButton(); userButton.Click += new RoutedEventHandler(mouse_Press); //userButton.Style = (Style)Resources["ButtonStyle1"]; userButton.Height = 65; userButton.IsShow = false; userButton.HasMine = false; userButton.AroundMineCount = 0; userButton.Background = new SolidColorBrush(Colors.Yellow); MineFieldGrid.Children.Add(userButton); Grid.SetRow(userButton, i); Grid.SetColumn(userButton, j); userButton.IsEnabled = true; userButton.x = i; userButton.y = j; showNum = 0; } } //放置地雷 this.LayMine(mineCount); //设置方格点击后显示四周的地雷数 foreach (UserButton button in MineFieldGrid.Children) { button.AroundMineCount = this.GetAroundMineCount(button); } }
/// <summary> /// 初始化雷区 /// </summary> /// <param name="panLineNum"> 方格边长</param> /// <param name="mineCount"> 地雷数量</param> public void Init(int panLineNum, int mineCount) { //放置方格,布局方格位置 for (int i = 0; i <panLineNum ; i++) { for (int j = 0; j < panLineNum ; j++) { UserButton userButton = new UserButton(); userButton.Click += new RoutedEventHandler(mouse_Press ); //userButton.Style = (Style)Resources["ButtonStyle1"]; userButton.Height = 65; userButton.IsShow = false ; userButton.HasMine = false; userButton.AroundMineCount = 0; userButton.Background = new SolidColorBrush(Colors.Yellow); MineFieldGrid.Children.Add(userButton); Grid.SetRow(userButton, i); Grid.SetColumn(userButton, j); userButton.IsEnabled = true; userButton.x = i; userButton.y = j; showNum = 0; } } //放置地雷 this.LayMine(mineCount ); //设置方格点击后显示四周的地雷数 foreach (UserButton button in MineFieldGrid.Children) { button.AroundMineCount = this.GetAroundMineCount(button ); } }
//得到userButton周围雷数 private int GetAroundMineCount(UserButton userButton) { int minecount = 0; foreach (UserButton p in MineFieldGrid.Children) { if ((Math.Abs(p.x - userButton.x) == 1 && p.y == userButton.y) || (Math.Abs(p.y - userButton.y) == 1 && p.x == userButton.x) || (Math.Abs(p.x - userButton.x) == 1 && Math.Abs(p.y - userButton.y) == 1)) { if (p.HasMine) { minecount++; } } } return(minecount); }
/// <summary> /// 点击鼠标事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void mouse_Press(object sender, RoutedEventArgs e) { UserButton userButton = sender as UserButton; //userButton.IsEnabled = false; userButton.IsShow = true; if (showNum == 0) { foreach (UserButton showButton in MineFieldGrid.Children) { if (showButton.IsShow == true) { showNum++; } } if (showNum == 1) { this.showTime(); } } if (this.IsAllMineSweeped()) { this.DisplayAll(); this.MineField_MineSweeppedSuccessfully(); } else { if (userButton.HasMine) { this.DisplayAll(); userButton.Open(userButton); timer.Stop(); this.MineField_MineSweeppedFaid(); } else { this.DisplayAround(userButton); } } }
/// <summary> /// 递归调用显示周围没雷的方块 /// </summary> /// <param name="currentUserButton"></param> /// public void DisplayAround(UserButton userbutton) { if (userbutton.AroundMineCount != 0) { userbutton.Open(userbutton); return; } else { userbutton.Open(userbutton); foreach (UserButton button in MineFieldGrid.Children) { if (button.IsShow == true) { continue; } else { if (Math.Abs(button.x - userbutton.x) == 1 && Math.Abs(button.y - userbutton.y) == 0 && button.IsShow == false) { DisplayAround(button); } if (Math.Abs(button.y - userbutton.y) == 1 && Math.Abs(button.x - userbutton.x) == 0 && button.IsShow == false) { DisplayAround(button); } if (Math.Abs(button.x - userbutton.x) == 1 && Math.Abs(button.y - userbutton.y) == 1 && button.IsShow == false) { DisplayAround(button); } } } } }
//方格打开后的样子 public void Open(UserButton u) { //u.IsEnabled = false; u.IsShow = true; if (u.HasMine) { //u.Background = new SolidColorBrush(Colors.Red); //Image imgItem = new Image(); //imgItem.Source = (new BitmapImage(new Uri("ms-appx:///Assest/1.jpg",UriKind.RelativeOrAbsolute))); //u.Content = imgItem; u.IsEnabled = false; } else { switch (u.AroundMineCount) { case 0: u.Background = new SolidColorBrush(Colors.PapayaWhip); break; case 1: u.Content = "1 111"; u.FontSize = 30; u.Foreground = new SolidColorBrush(Color.FromArgb(255, 0, 0, 253)); u.Background = new SolidColorBrush(Color.FromArgb(255, 0, 255, 255)); break; case 2: u.Content = "2 222"; u.FontSize = 30; u.Foreground = new SolidColorBrush(Color.FromArgb(255, 0, 128, 0)); u.Background = new SolidColorBrush(Color.FromArgb(255, 0, 255, 255)); break; case 3: u.Content = "3 333"; u.FontSize = 30; u.Foreground = new SolidColorBrush(Colors.Black); u.Background = new SolidColorBrush(Color.FromArgb(255, 0, 255, 255)); break; case 4: this.Content = "4 444"; this.FontSize = 30; this.Foreground = new SolidColorBrush(Color.FromArgb(255, 1, 1, 127)); u.Background = new SolidColorBrush(Color.FromArgb(255, 0, 255, 255)); break; case 5: this.Content = "5 555"; this.FontSize = 30; u.Background = new SolidColorBrush(Color.FromArgb(255, 0, 255, 255)); break; case 6: this.Content = "6 666"; this.FontSize = 30; u.Background = new SolidColorBrush(Color.FromArgb(255, 0, 255, 255)); break; case 7: this.Content = "7 777"; this.FontSize = 30; u.Background = new SolidColorBrush(Color.FromArgb(255, 0, 255, 255)); break; case 8: this.Content = "8 888"; this.FontSize = 30; u.Background = new SolidColorBrush(Colors.LightGreen); break; } } }
//方格打开后的样子 public void Open(UserButton u) { //u.IsEnabled = false; u.IsShow =true ; if (u.HasMine) { //u.Background = new SolidColorBrush(Colors.Red); //Image imgItem = new Image(); //imgItem.Source = (new BitmapImage(new Uri("ms-appx:///Assest/1.jpg",UriKind.RelativeOrAbsolute))); //u.Content = imgItem; u.IsEnabled = false; } else { switch (u.AroundMineCount) { case 0: u.Background = new SolidColorBrush(Colors.PapayaWhip); break; case 1: u.Content = "1 111"; u.FontSize = 30; u.Foreground = new SolidColorBrush(Color.FromArgb(255,0,0,253)); u.Background = new SolidColorBrush(Color.FromArgb(255,0,255,255)); break; case 2: u.Content = "2 222"; u.FontSize = 30; u.Foreground = new SolidColorBrush(Color.FromArgb (255,0,128,0) ); u.Background = new SolidColorBrush(Color.FromArgb(255, 0, 255, 255)); break; case 3: u.Content = "3 333"; u.FontSize = 30; u.Foreground = new SolidColorBrush(Colors.Black ); u.Background = new SolidColorBrush(Color.FromArgb(255, 0, 255, 255)); break; case 4: this.Content = "4 444" ; this.FontSize = 30; this.Foreground = new SolidColorBrush(Color.FromArgb(255,1,1,127)); u.Background = new SolidColorBrush(Color.FromArgb(255, 0, 255, 255)); break; case 5: this.Content = "5 555"; this.FontSize = 30; u.Background = new SolidColorBrush(Color.FromArgb(255, 0, 255, 255)); break; case 6: this.Content = "6 666"; this.FontSize = 30; u.Background = new SolidColorBrush(Color.FromArgb(255, 0, 255, 255)); break; case 7: this.Content = "7 777"; this.FontSize = 30; u.Background = new SolidColorBrush(Color.FromArgb(255, 0, 255, 255)); break; case 8: this.Content = "8 888"; this.FontSize = 30; u.Background = new SolidColorBrush(Colors.LightGreen); break; } } }
//得到userButton周围雷数 private int GetAroundMineCount(UserButton userButton ) { int minecount = 0; foreach (UserButton p in MineFieldGrid.Children) { if ((Math.Abs(p.x - userButton.x) == 1 && p.y == userButton.y) || (Math.Abs(p.y - userButton.y) == 1 && p.x == userButton.x) || (Math.Abs(p.x - userButton.x) == 1 && Math.Abs(p.y - userButton.y) == 1)) { if (p.HasMine) minecount++; } } return minecount; }
/// <summary> /// 递归调用显示周围没雷的方块 /// </summary> /// <param name="currentUserButton"></param> /// public void DisplayAround(UserButton userbutton) { if (userbutton.AroundMineCount != 0 ) { userbutton.Open(userbutton); return; } else { userbutton.Open(userbutton); foreach (UserButton button in MineFieldGrid.Children) { if (button.IsShow ==true ) { continue; } else { if (Math.Abs(button.x - userbutton.x) == 1 && Math.Abs(button.y - userbutton.y) == 0 && button.IsShow==false ) DisplayAround(button); if (Math.Abs(button.y - userbutton.y) == 1 && Math.Abs(button.x - userbutton.x) == 0 && button.IsShow==false ) DisplayAround(button); if (Math.Abs(button.x - userbutton.x) == 1 && Math.Abs(button.y - userbutton.y) == 1 && button.IsShow==false ) DisplayAround(button); } } } }