/// <summary> /// NormalButton的Click响应,本质是一个委托 /// </summary> /// <param name="listid"></param> public void NormalClick(int listid) { //if (isGameOver_flag) //{ // GameOver(); // return; //} //rightClickCount是否已标记 if (NBS[listid].rightClickCount != 0) { return; } //是否已挖开 if (NBS[listid].isdigged_flag == true) { return; } //是否为地雷 if (Landmines.Contains(listid)) { //NBS[listid].Background = Brushes.DarkRed; //NBS[listid].isdigged_flag = true; foreach (var i in Landmines) { ImageBrush imageBrush = new ImageBrush(); imageBrush.ImageSource = new BitmapImage(new Uri("pack://application:,,,/images/boom.bmp", UriKind.Absolute)); NBS[i].Background = imageBrush; } //Thread.Sleep(100); //isGameOver_flag = true; MessageBox.Show("你输了"); //VisualBrush vb = new VisualBrush(gameWindow.GD); //Rectangle rectangle = new Rectangle(); //rectangle.Width = gameWindow.GD.ActualWidth; //rectangle.Height = gameWindow.GD.ActualHeight; //rectangle.Opacity = 0.75; //rectangle.Fill = vb; //gameWindow.Content = null; //Grid grid = new Grid(); //grid.Background = Brushes.DarkGray; //grid.Children.Add(rectangle); //gameWindow.Content = grid; GameOver();//必须放在1messagebox后面,原因未知 //中止游戏 return; } //扩散挖开 List <int> normalButtons = new List <int>();//扩散表 normalButtons.Add(listid); while (normalButtons.Count > 0) { int lid = normalButtons[0]; //周围是否有地雷 if (NBS[lid].numberoflandminesaround != 0) { NBS[lid].Content = NBS[lid].numberoflandminesaround.ToString();// //return; } else { if (lid - ((CBItem)CB_MapSize.SelectedItem).SelectItem >= 0 && //判断土块序号是否存在 !normalButtons.Contains(lid - ((CBItem)CB_MapSize.SelectedItem).SelectItem) && //判断是否在扩散表中 NBS[lid - ((CBItem)CB_MapSize.SelectedItem).SelectItem].isdigged_flag == false && //判断是否已挖开 NBS[lid - ((CBItem)CB_MapSize.SelectedItem).SelectItem].islandmine_flag == false) //判断是否为地雷 { normalButtons.Add(lid - ((CBItem)CB_MapSize.SelectedItem).SelectItem); } if (lid + ((CBItem)CB_MapSize.SelectedItem).SelectItem < NBS.Count && !normalButtons.Contains(lid + ((CBItem)CB_MapSize.SelectedItem).SelectItem) && NBS[lid + ((CBItem)CB_MapSize.SelectedItem).SelectItem].isdigged_flag == false && NBS[lid + ((CBItem)CB_MapSize.SelectedItem).SelectItem].islandmine_flag == false) { normalButtons.Add(lid + ((CBItem)CB_MapSize.SelectedItem).SelectItem); } if ((lid) % ((CBItem)CB_MapSize.SelectedItem).SelectItem != 0 && !normalButtons.Contains(lid - 1) && NBS[lid - 1].isdigged_flag == false && NBS[lid - 1].islandmine_flag == false) { normalButtons.Add(lid - 1); } if ((lid + 1) % ((CBItem)CB_MapSize.SelectedItem).SelectItem != 0 && !normalButtons.Contains(lid + 1) && NBS[lid + 1].isdigged_flag == false && NBS[lid + 1].islandmine_flag == false) { normalButtons.Add(lid + 1); }// } //挖开此块 NormalButton normalButton = NBS[lid] as NormalButton; normalButton.isdigged_flag = true; normalButton.Background = Brushes.White; switch (normalButton.numberoflandminesaround) { case 1: normalButton.Foreground = Brushes.LightSteelBlue; break; case 2: normalButton.Foreground = Brushes.Black; break; case 3: normalButton.Foreground = Brushes.DarkGoldenrod; break; case 4: normalButton.Foreground = Brushes.YellowGreen; break; case 5: normalButton.Foreground = Brushes.Aqua; break; case 6: normalButton.Foreground = Brushes.Fuchsia; break; case 7: normalButton.Foreground = Brushes.Orange; break; case 8: normalButton.Foreground = Brushes.Red; break; } //normalButton.Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FF0000")); normalButtons.Remove(normalButtons[0]); } }
void NewGame() { //判空 if (CB_MapSize.SelectedItem == null) { MessageBox.Show("请选择地图大小!"); return; } if (TBX_LandmineNumber.Text == null || Convert.ToInt32(TBX_LandmineNumber.Text) == 0) { Random random = new Random(); NumberOfLandmine = random.Next() % (Convert.ToInt32(Math.Pow(((CBItem)CB_MapSize.SelectedItem).SelectItem, 2)) - 5) + 5; } else { NumberOfLandmine = Convert.ToInt32(TBX_LandmineNumber.Text); } //new GameWindow GameWindow.rowNumberOfLandmine = ((CBItem)CB_MapSize.SelectedItem).SelectItem; gameWindow = new GameWindow(); //随机地雷表 Random random1 = new Random(); while (Landmines.Count < NumberOfLandmine) { int site = random1.Next() % (Convert.ToInt32(Math.Pow(((CBItem)CB_MapSize.SelectedItem).SelectItem, 2) - 1)); if (!Landmines.Contains(site)) { Landmines.Add(site); } } //绘制窗体框架 gameWindow.WP.Width = ((CBItem)CB_MapSize.SelectedItem).SelectItem * 30 + 2; gameWindow.WP.Height = ((CBItem)CB_MapSize.SelectedItem).SelectItem * 30 + 2; gameWindow.Width = ((CBItem)CB_MapSize.SelectedItem).SelectItem * 30 + 25; gameWindow.Height = ((CBItem)CB_MapSize.SelectedItem).SelectItem * 30 + 72; gameWindow.gameWindowDelegate += ShowMainWindow; gameWindow.gameWindowDelegate1 += RemakeGame; gameWindow.gameWindowDelegate2 += QuitGame; //gameWindow.MN.Width= for (int i = 0; i < ((CBItem)CB_MapSize.SelectedItem).SelectItem * ((CBItem)CB_MapSize.SelectedItem).SelectItem; i++) { NormalButton normalButton = new NormalButton() { Width = 30, Height = 30, listid = i, Background = Brushes.DarkGray, FontWeight = FontWeights.Black }; if (Landmines.Contains(i)) { normalButton.islandmine_flag = true; } normalButton.clickhandle = NormalClick;//将NormalButton的clickhandle委托与函数绑定 normalButton.rightClickHandle += NormalRightClick; gameWindow.WP.Children.Add(normalButton); NBS.Add(normalButton); } gameWindow.Show(); foreach (int i in Landmines) { //为周围8个土块添加雷区标记 numberoflandminesaround++ if (i - ((CBItem)CB_MapSize.SelectedItem).SelectItem >= 0 && //判断土块序号是否存在 NBS[i - ((CBItem)CB_MapSize.SelectedItem).SelectItem].islandmine_flag == false) //判断是否为地雷 { NBS[i - ((CBItem)CB_MapSize.SelectedItem).SelectItem].numberoflandminesaround++; } if (i - ((CBItem)CB_MapSize.SelectedItem).SelectItem >= 0 && (i - ((CBItem)CB_MapSize.SelectedItem).SelectItem + 1) % ((CBItem)CB_MapSize.SelectedItem).SelectItem != 0 && //判断土块序号是否存在 NBS[i - ((CBItem)CB_MapSize.SelectedItem).SelectItem + 1].islandmine_flag == false) //判断是否为地雷 { NBS[i - ((CBItem)CB_MapSize.SelectedItem).SelectItem + 1].numberoflandminesaround++; } if (i + ((CBItem)CB_MapSize.SelectedItem).SelectItem < NBS.Count && NBS[i + ((CBItem)CB_MapSize.SelectedItem).SelectItem].islandmine_flag == false) { NBS[i + ((CBItem)CB_MapSize.SelectedItem).SelectItem].numberoflandminesaround++; } if (i + ((CBItem)CB_MapSize.SelectedItem).SelectItem < NBS.Count && (i + ((CBItem)CB_MapSize.SelectedItem).SelectItem) % ((CBItem)CB_MapSize.SelectedItem).SelectItem != 0 && //判断土块序号是否存在 NBS[i + ((CBItem)CB_MapSize.SelectedItem).SelectItem - 1].islandmine_flag == false) //判断是否为地雷 { NBS[i + ((CBItem)CB_MapSize.SelectedItem).SelectItem - 1].numberoflandminesaround++; } if ((i) % ((CBItem)CB_MapSize.SelectedItem).SelectItem != 0 && NBS[i - 1].islandmine_flag == false) { NBS[i - 1].numberoflandminesaround++; } if ((i) % ((CBItem)CB_MapSize.SelectedItem).SelectItem != 0 && i - ((CBItem)CB_MapSize.SelectedItem).SelectItem - 1 >= 0 && NBS[i - ((CBItem)CB_MapSize.SelectedItem).SelectItem - 1].islandmine_flag == false) { NBS[i - ((CBItem)CB_MapSize.SelectedItem).SelectItem - 1].numberoflandminesaround++; } if ((i + 1) % ((CBItem)CB_MapSize.SelectedItem).SelectItem != 0 && NBS[i + 1].islandmine_flag == false) { NBS[i + 1].numberoflandminesaround++; } if ((i + 1) % ((CBItem)CB_MapSize.SelectedItem).SelectItem != 0 && (i + 1) + ((CBItem)CB_MapSize.SelectedItem).SelectItem < NBS.Count && NBS[(i + 1) + ((CBItem)CB_MapSize.SelectedItem).SelectItem].islandmine_flag == false) { NBS[(i + 1) + ((CBItem)CB_MapSize.SelectedItem).SelectItem].numberoflandminesaround++; }// } this.Hide(); }