/// <summary> /// 返回周围雷数 /// </summary> /// <param name="mineCell"></param> /// <returns></returns> private int MineAroundNum(MineCell mineCell) { int num = 0; // 获取该控件的坐标和下标 Point point = mineCell.Location; int x = point.Y / 20; int y = point.X / 20; Point[] points = new Point[8]; points[0] = new Point(x - 1, y); points[1] = new Point(x, y - 1); points[2] = new Point(x + 1, y); points[3] = new Point(x, y + 1); points[4] = new Point(x - 1, y - 1); points[5] = new Point(x - 1, y + 1); points[6] = new Point(x + 1, y - 1); points[7] = new Point(x + 1, y + 1); for (int i = 0; i < 8; i++) { if (points[i].X < MineCells.GetLength(0) && points[i].X >= 0 && points[i].Y < MineCells.GetLength(1) && points[i].Y >= 0) { if (MineCells[points[i].X, points[i].Y].IsMine) { num++; } } else { continue; } } return(num); }
private void 中级ToolStripMenuItem_Click(object sender, EventArgs e) { this.Visible = false; timer1.Stop(); if (disposed) { foreach (MineCell item in MineCells) { item.Dispose(); } } 初级ToolStripMenuItem.Checked = false; 中级ToolStripMenuItem.Checked = true; 高级ToolStripMenuItem.Checked = false; FaceButton.ImageIndex = 0; SetRemainingMinesCount(40); GameTime = 0; SetGameTime(GameTime); RemainingMinesCount = 40; this.Width = 351; this.Height = 441; panel2.Width = 324; panel2.Height = 324; MineCells = new MineCell[16, 16]; for (int i = 0; i < 16; i++) { for (int j = 0; j < 16; j++) { MineCells[i, j] = new MineCell { Name = "", Open = false, IsMine = false, Size = new Size(20, 20), Image = MineImageList.Images[9], Location = new Point(j * 20, i * 20), }; // 绑定事件 MineCells[i, j].MouseDown += new MouseEventHandler(Mine_MouseDown); MineCells[i, j].MouseUp += new MouseEventHandler(Mine_MouseUp); panel2.Controls.Add(MineCells[i, j]); } } disposed = true; RandomSteBomb(MineCells, 40); this.Visible = true; }
/// <summary> /// 自动打开周围雷数为0的控件 /// </summary> /// <param name="mineCell"></param> private void AutoOpen(MineCell mineCell) { // 获取该控件的坐标和下标 Point point = mineCell.Location; int x = point.Y / 20; int y = point.X / 20; Point[] points = new Point[8]; points[0] = new Point(x - 1, y); points[1] = new Point(x, y - 1); points[2] = new Point(x + 1, y); points[3] = new Point(x, y + 1); points[4] = new Point(x - 1, y - 1); points[5] = new Point(x - 1, y + 1); points[6] = new Point(x + 1, y - 1); points[7] = new Point(x + 1, y + 1); for (int i = 0; i < 8; i++) { if (points[i].X < MineCells.GetLength(0) && points[i].X >= 0 && points[i].Y < MineCells.GetLength(1) && points[i].Y >= 0) { if (!MineCells[points[i].X, points[i].Y].Open) { MineCells[points[i].X, points[i].Y].Open = true; MineCells[points[i].X, points[i].Y].Image = MineImageList.Images[MineAroundNum(MineCells[points[i].X, points[i].Y])]; if (MineAroundNum(MineCells[points[i].X, points[i].Y]) == 0) { AutoOpen(MineCells[points[i].X, points[i].Y]); } } } else { continue; } } }
/// <summary> /// 鼠标按键抬起时 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Mine_MouseUp(object sender, MouseEventArgs e) { MineCell mineCell = (MineCell)sender; // 点击鼠标左键 if (e.Button == MouseButtons.Left) { FaceButton.ImageIndex = 0; // 控件没有被标记时 if (!mineCell.Marked) { // 踩到雷时 if (mineCell.IsMine) { timer1.Stop(); FaceButton.ImageIndex = 3; // 解绑事件 for (int i = 0; i < MineCells.GetLength(0); i++) { for (int j = 0; j < MineCells.GetLength(1); j++) { MineCells[i, j].MouseDown -= new MouseEventHandler(Mine_MouseDown); MineCells[i, j].MouseUp -= new MouseEventHandler(Mine_MouseUp); // 显示所有的雷 if (MineCells[i, j].IsMine) { MineCells[i, j].Image = MineImageList.Images[14]; } // 标记错误 if (!MineCells[i, j].IsMine && MineCells[i, j].Marked) { MineCells[i, j].Image = MineImageList.Images[13]; } } } mineCell.Open = true; mineCell.Image = MineImageList.Images[12]; } else { // 周围没有雷时将周围控件全部打开 if (MineAroundNum(mineCell) == 0) { mineCell.Open = true; mineCell.Image = MineImageList.Images[0]; AutoOpen(mineCell); } else { mineCell.Open = true; mineCell.Image = MineImageList.Images[MineAroundNum(mineCell)]; // 显示周围的雷数(未完成) } } } } // 点击鼠标右键 if (e.Button == MouseButtons.Right) { while (true) { if (mineCell.Open) { break; } if (mineCell.Name == "?") { mineCell.Image = MineImageList.Images[9]; break; } if (mineCell.Marked) { mineCell.Marked = false; // 取消标记 mineCell.Image = MineImageList.Images[11]; // 显示问号 mineCell.Name = "?"; RemainingMinesCount += 1; SetRemainingMinesCount(RemainingMinesCount); break; } if (!mineCell.Marked) { mineCell.Marked = true; // 标记 mineCell.Image = MineImageList.Images[10]; // 显示旗帜 mineCell.Name = "flag"; RemainingMinesCount -= 1; SetRemainingMinesCount(RemainingMinesCount); break; } } } bool win = true; // 结束时检查标记是否正确 if (RemainingMinesCount == 0) { timer1.Stop(); foreach (MineCell item in MineCells) { if (!item.IsMine && item.Marked) { win = false; item.Image = MineImageList.Images[13]; FaceButton.ImageIndex = 3; } } if (win) // 胜利 { FaceButton.ImageIndex = 4; if (初级ToolStripMenuItem.Checked && GameTime < TopTime[0]) { form3.ShowDialog(); File.Delete(path); using (StreamWriter sw = File.CreateText(path)) { sw.WriteLine($"{GameTime}|{Form3.HeroName}"); sw.WriteLine($"{str[1]}"); sw.WriteLine($"{str[2]}"); } } if (中级ToolStripMenuItem.Checked && GameTime < TopTime[1]) { form3.ShowDialog(); File.Delete(path); using (StreamWriter sw = File.CreateText(path)) { sw.WriteLine($"{str[0]}"); sw.WriteLine($"{GameTime}|{Form3.HeroName}"); sw.WriteLine($"{str[2]}"); } } if (高级ToolStripMenuItem.Checked && GameTime < TopTime[2]) { form3.ShowDialog(); File.Delete(path); using (StreamWriter sw = File.CreateText(path)) { sw.WriteLine($"{str[0]}"); sw.WriteLine($"{str[1]}"); sw.WriteLine($"{GameTime}|{Form3.HeroName}"); } } using (StreamReader sr = File.OpenText(path)) { string s; int i = 0; while ((s = sr.ReadLine()) != null) { str[i] = s; i++; } } string[] str1 = str[0].Split('|'); string[] str2 = str[1].Split('|'); string[] str3 = str[2].Split('|'); TopTime[0] = Convert.ToInt32(str1[0]); TopTime[1] = Convert.ToInt32(str2[0]); TopTime[2] = Convert.ToInt32(str3[0]); Form2 form2 = new Form2(); form2.ShowDialog(); } } }