using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace 扫雷
{
public partial class frmMine : Form
{
public frmMine()
{
InitializeComponent();
}
bool bRun = false;
int numMine;
const int Row = 16;
const int Column = 14;
int[,] mine = new int[Row, Column];
bool[,] bLPress = new bool[Row, Column];
bool[,] bRPress = new bool[Row, Column];
private void frmMine_Load(object sender, EventArgs e)
{
comboBox.SelectedIndex = 0;
}
private void frmMine_Paint(object sender, PaintEventArgs e)
{
DrawGrids(e.Graphics);
Font font = new Font(FontFamily.Families[1], 11, FontStyle.Regular);
for (int r = 0; r < Row; r++)
{
for (int c = 0; c < Column; c++)
{
if (bLPress[r, c] )
{
if (mine[r,c]<9)
if (mine[r, c] == 0)
{
e.Graphics.DrawImage(Properties.Resources.zero, 21 + 20 * c, 21 + 20 * r);
}
else
{
e.Graphics.DrawString(mine[r, c].ToString(),
font, Brushes.Green, 24 + 20 * c, 21 + 20 * r);
}
if (mine[r, c] == 9)
e.Graphics.DrawImage(Properties.Resources.mine, 21 + 20 * c, 21 + 20 * r);
}
if (bRPress[r, c])
{
e.Graphics.DrawString("*", font, Brushes.Black, 24 + 20 * c, 24 + 20 * r);
}
}
}
}
private void btnStart_Click(object sender, EventArgs e)
{
bRun = true;
numMine = int.Parse(comboBox.Text);
comboBox.Enabled = false;
InitGame();
}
/// 写游戏开始后,鼠标按下去的效果!
private void frmMine_MouseDown(object sender, MouseEventArgs e)
{
if (bRun == false)
return;
if (e.X < 20 || e.X > 300 || e.Y < 20 || e.Y > 340)
return;
int c = (e.X - 20) / 20;
int r = (e.Y - 20) / 20;
if (bLPress[r, c])
return;
Graphics g = this.CreateGraphics();
Font font = new Font(FontFamily.Families[1],11, FontStyle.Regular);
if (e.Button == MouseButtons.Left)
{
if (bRPress[r,c])
return;
if (mine[r, c] == 9)
{
bLPress[r, c] = true;
g.DrawImage(Properties.Resources.mine, 21 + 20 * c, 21 + 20 * r);
comboBox.Enabled = true;
MessageBox.Show("失败!游戏结束!");
bRun = false;
}
else if (mine[r, c] == 0)
{
Zero(g, font, r, c);
}
else
{
bLPress[r, c] = true;
g.DrawString(mine[r, c].ToString(), font, Brushes.Green, 24 + 20 * c, 21 + 20 * r);
}
}
else if (e.Button == MouseButtons.Right)
{
if (bRPress[r, c] == true)
{
bRPress[r, c] = false;
g.FillRectangle(new SolidBrush(BackColor), 21 + 20 * c, 21 + 20 * r, 19, 19);
}
else
{
bRPress[r, c] = true;
g.DrawString("*", font, Brushes.Black, 24 + 20 * c, 24 + 20 * r);
if (GameOver(r, c))
{
MessageBox.Show("恭喜您,赢得了本局!");
bRun = false;
}
}
}
g.Dispose();
}
/// 画雷区!
private void DrawGrids(Graphics g)
{
for (int i = 20; i <= 340; i += 20)
{
g.DrawLine(Pens.BlueViolet, 20, i, 300, i);
}
for (int i = 20; i <= 300; i += 20)
{
g.DrawLine(Pens.BlueViolet, i, 20, i, 340);
}
}
/// 初始化游戏!
private void InitGame()
{
for (int i = 0; i < Row; i++)
{
for (int j = 0; j < Column; j++)
{
bLPress[i, j] = false;
bRPress[i, j] = false;
mine[i, j] = 0;
}
}
Random rand = new Random();
for (int i = 0; i < numMine; i++)
{
int nRow = rand.Next(Row);
int nColumn = rand.Next(Column);
if (mine[nRow, nColumn] == 9)
{
i--;
continue;
}
mine[nRow, nColumn] = 9;
}
Graphics g = this.CreateGraphics();
g.FillRectangle(new SolidBrush(BackColor), 20, 20, Column * 20, Row * 20);
DrawGrids(g);
g.Dispose();
#region 显示各个位置各有几个雷!
for (int i = 0; i < Row; i++)
{
for (int j=0; j < Column; j++)
{
if (mine[i, j] == 9)
{
if (i - 1 >= 0)
{
if (j - 1 >= 0)
{
if (mine[i - 1, j - 1] < 9)
mine[i - 1, j - 1]++;
}
if (j + 1 < Column)
{
if (mine[i - 1, j + 1] < 9)
mine[i - 1, j + 1]++;
}
if (mine[i - 1, j] < 9)
mine[i - 1, j]++;
}
if (i + 1 < Row)
{
if (j - 1 >= 0)
{
if (mine[i + 1, j - 1] < 9)
mine[i + 1, j - 1]++;
}
if (j + 1 < Column)
{
if (mine[i + 1, j + 1] < 9)
mine[i + 1, j + 1]++;
}
if (mine[i + 1, j] < 9)
mine[i + 1, j]++;
}
if (j - 1 >= 0)
{
if (mine[i, j - 1] < 9)
mine[i, j - 1]++;
}
if (j + 1 < Column)
{
if (mine[i, j + 1] < 9)
mine[i, j + 1]++;
}
}
}
}
#endregion
}
///判断游戏结束!
private bool GameOver(int r, int c)
{
for (r = 0; r < Row; r++)
{
for (c = 0; c < Column; c++)
{
if (mine[r, c] == 9 && bRPress[
- 1
- 2
- 3
前往页