C#
编扫雷源码
08-5-02
C#编扫雷源码 一2008年03月23日 星期日 21:53
源代码:
[Form1.CS]
using
System;
using
System.Drawing;
using
System.Collections;
using
System.ComponentModel;
using
System.Windows.Forms;
using
System.Data;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
namespace
MineClearance
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuItem2;
private System.Windows.Forms.MenuItem menuItem3;
private System.Windows.Forms.MenuItem menuItem4;
private System.Windows.Forms.MenuItem menuItem5;
private System.Windows.Forms.MenuItem menuItem6;
private System.Windows.Forms.MenuItem menuItem7;
private System.Windows.Forms.MenuItem menuItem9;
private System.Windows.Forms.MenuItem menuItem12;
private System.Windows.Forms.MenuItem menuItem13;
private System.Windows.Forms.MenuItem menuItem14;
private System.ComponentModel.IContainer components;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
private FaceControl faceControl1;
private MineCountControl mineCountControl1;
private TimerControl timerControl1;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
private int [ , ] Mine;//==-2时,表示这个地雷已经爆炸;==-1时,代表这个位置是地雷;否则代表周围的地雷的数量
private int [ , ] Turn;//==-1 表示这个位置已经翻开;==0 表示这个位置没有翻开;==1 表示这个位置插上红旗;
private int BoardWidth;//宽度
private int BoardHeight;//高度
private uint MineCount;
private Point MP = new Point(0, 0);//用来记录鼠标点击的方格
private bool GameStartMark;//游戏开始标志
private uint Count
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
get
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
return MineCount;
}
set
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
MineCount = value;
mineCountControl1.mineCount = (int)value;
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
public Form1()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
//
// Required for Windows Form Designer support
//
InitializeComponent();
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
//
// TODO: Add any constructor code after InitializeComponent call
//
mineCountControl1 = new MineCountControl();
faceControl1 = new FaceControl();
faceControl1.Click += new EventHandler(ClickFace);
timerControl1 = new TimerControl();
timerControl1.Rouse += new EventHandler(RouseTime);
timerControl1.rouseTime = 999;
Reset(9, 9, 10);
this.CenterToScreen();
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
this.Controls.Add(faceControl1);
this.Controls.Add(mineCountControl1);
this.Controls.Add(timerControl1);
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
if( disposing )
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
if (components != null)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
components.Dispose();
}
}
base.Dispose( disposing );
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/7ff8d92cded7e0ce15e7ca1acc870052.gif)
Windows Form Designer generated code#region Windows Form Designer generated code
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this.menuItem9 = new System.Windows.Forms.MenuItem();
this.menuItem4 = new System.Windows.Forms.MenuItem();
this.menuItem5 = new System.Windows.Forms.MenuItem();
this.menuItem6 = new System.Windows.Forms.MenuItem();
this.menuItem7 = new System.Windows.Forms.MenuItem();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItem3 = new System.Windows.Forms.MenuItem();
this.menuItem12 = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.menuItem13 = new System.Windows.Forms.MenuItem();
this.menuItem14 = new System.Windows.Forms.MenuItem();
this.mainMenu1 = new System.Windows.Forms.MainMenu();
//
// menuItem9
//
this.menuItem9.Index = 5;
this.menuItem9.Text = "-";
//
// menuItem4
//
this.menuItem4.Index = 2;
this.menuItem4.Text = "初级 (&B)";
this.menuItem4.Click += new System.EventHandler(this.menuItem4_Click);
//
// menuItem5
//
this.menuItem5.Index = 1;
this.menuItem5.Text = "-";
//
// menuItem6
//
this.menuItem6.Index = 3;
this.menuItem6.Text = "中级 (&I)";
this.menuItem6.Click += new System.EventHandler(this.menuItem6_Click);
//
// menuItem7
//
this.menuItem7.Index = 4;
this.menuItem7.Text = "高级 (&E)";
this.menuItem7.Click += new System.EventHandler(this.menuItem7_Click);
//
// menuItem1
//
this.menuItem1.Index = 0;
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] ...{
this.menuItem3,
this.menuItem5,
this.menuItem4,
this.menuItem6,
this.menuItem7,
this.menuItem9,
this.menuItem12});
this.menuItem1.Text = "游戏(&G)";
//
// menuItem3
//
this.menuItem3.Index = 0;
this.menuItem3.Shortcut = System.Windows.Forms.Shortcut.F2;
this.menuItem3.Text = "开局 (&N)";
this.menuItem3.Click += new System.EventHandler(this.menuItem3_Click);
//
// menuItem12
//
this.menuItem12.Index = 6;
this.menuItem12.Shortcut = System.Windows.Forms.Shortcut.CtrlX;
this.menuItem12.Text = "退出 (&X)";
this.menuItem12.Click += new System.EventHandler(this.menuItem12_Click);
//
// menuItem2
//
this.menuItem2.Index = 1;
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
this.menuItem2.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] ...{
this.menuItem13,
this.menuItem14});
this.menuItem2.Text = "帮助(&H)";
//
// menuItem13
//
this.menuItem13.Index = 0;
this.menuItem13.Shortcut = System.Windows.Forms.Shortcut.F1;
this.menuItem13.Text = "使用帮助 (&F)";
this.menuItem13.Click += new System.EventHandler(this.menuItem13_Click);
//
// menuItem14
//
this.menuItem14.Index = 1;
this.menuItem14.Text = "关于扫雷… (&A)";
this.menuItem14.Click += new System.EventHandler(this.menuItem14_Click);
//
// mainMenu1
//
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] ...{
this.menuItem1,
this.menuItem2});
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(10, 23);
this.BackColor = System.Drawing.Color.Gainsboro;
this.ClientSize = new System.Drawing.Size(355, 277);
this.Font = new System.Drawing.Font("Microsoft Sans Serif", 15F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MaximizeBox = false;
this.Menu = this.mainMenu1;
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "扫雷";
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
}
#endregion
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
Application.Run(new Form1());
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
//鼠标坐标转换成雷区方格的坐标
private Point MToA(int x, int y)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
x -= 13; y -= 53;
if(x < 0 || x > BoardWidth * 20 || y < 0 || y > BoardWidth * 20 )
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
throw new Exception();
}
x = x / 20;
y = y / 20;
return new Point(x , y);
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
//鼠标坐标转换成雷区绝对坐标,进行坐标越界处理
private Point MToAT(int x, int y)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
x = (x - 13) / 20;
y = (y - 53) / 20;
if(x < 0) x = 0;
if(x > BoardWidth - 1) x = BoardWidth - 1;
if(y < 0) y = 0;
if(y > BoardHeight -1) y = BoardHeight -1;
return new Point(x , y);
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
//判断一点是否在一区域内
private bool InBox(System.Drawing.Rectangle rec1, System.Drawing.Point p1)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
if( (rec1.Left <= p1.X && rec1.Top <= p1.Y) && (p1.X <= rec1.Right && p1.Y <= rec1.Bottom) ) return true;
else return false;
}
//判断两区域是否交叉(有覆盖之处)
private bool CrossBox(System.Drawing.Rectangle rec1, System.Drawing.Rectangle rec2)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
//如果rec2有任何一个角在rec1中则视为交叉
if( InBox(rec1, new Point(rec2.Left, rec2.Top)) == true) return true;
if( InBox(rec1, new Point(rec2.Right, rec2.Top)) == true) return true;
if( InBox(rec1, new Point(rec2.Left, rec2.Bottom)) == true) return true;
if( InBox(rec1, new Point(rec2.Right, rec2.Bottom)) == true) return true;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
//如果rec1有任何一个角在rec2中则视为交叉
if( InBox(rec2, new Point(rec1.Left, rec1.Top)) == true) return true;
if( InBox(rec2, new Point(rec1.Right, rec1.Top)) == true) return true;
if( InBox(rec2, new Point(rec1.Left, rec1.Bottom)) == true) return true;
if( InBox(rec2, new Point(rec1.Right, rec1.Bottom)) == true) return true;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
if( (rec1.Left <= rec2.Left && rec2.Right <= rec1.Right) && (rec2.Top <= rec1.Top && rec1.Bottom <= rec2.Bottom) ) return true;
if( (rec2.Left <= rec1.Left && rec1.Right <= rec2.Right) && (rec1.Top <= rec2.Top && rec2.Bottom <= rec1.Bottom) ) return true;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
return false;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
//画立体方框
private void ColorBox(int x1, int y1, int x2, int y2, int PenWidth, bool Down, bool Fill, Color FillColor)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
System.Drawing.Graphics g = this.CreateGraphics();
System.Drawing.Pen p = new Pen(Color.White, 1);//确定画笔的颜色和粗细
for(; PenWidth > 0; PenWidth --)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
p.Color = (Down == false)? Color.White: Color.Gray;
g.DrawLine( p, x1, y1, x2, y1);
g.DrawLine( p, x1, y1, x1, y2);
p.Color = (Down == false)? Color.Gray: Color.White;
g.DrawLine( p , x2, y2, x2, y1);
g.DrawLine( p , x2, y2, x1, y2);
x1 ++; y1 ++;
x2 --; y2 --;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
if( Fill == true ) g.FillRectangle(new SolidBrush(FillColor), x1, y1, x2 - x1 + 1, y2 - y1 + 1);
}
//自动清除无雷区
private void ClearBoardMine(int nx, int ny)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
if(InBox(new Rectangle(0,0,BoardWidth-1,BoardHeight-1), new Point(nx, ny)) == false) return;
if( Mine[ny, nx] == -1 || Turn[ny, nx] == -1) return;
Turn[ny, nx] = -1;
ReDraw(new Point(nx, ny), new Point(nx, ny) );
if( Mine[ny, nx] == 0)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
for(int i=ny-1; i<= ny+1; i++)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
for(int j=nx-1; j<=nx+1; j++)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
ClearBoardMine(j, i);
}
}
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
//单击笑脸处理
public void ClickFace(object sender, System.EventArgs e)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
ReStart();
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
//超时处理
public void RouseTime(object sender, System.EventArgs e)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
timerControl1.Stop();
System.Windows.Forms.MessageBox.Show("时间到了,这次你输了! 没关系,别恢心,请继续努力!","游戏结束");
ReStart();
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
protected override void OnPaint(PaintEventArgs e)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
System.Drawing.Graphics g = e.Graphics;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
ColorBox(0, 0,this.ClientSize.Width, this.ClientSize.Height, 3, false, false, Color.Gainsboro);
g.FillRectangle(new SolidBrush(Color.Gainsboro), 3, 3, this.ClientSize.Width - 5, 7);
g.FillRectangle(new SolidBrush(Color.Gainsboro), 3, 3, 7, this.ClientSize.Height - 5);
g.FillRectangle(new SolidBrush(Color.Gainsboro), this.ClientSize.Width - 9 , 3, 7, this.ClientSize.Height - 5);
g.FillRectangle(new SolidBrush(Color.Gainsboro), 3, 41, this.ClientSize.Width - 5, 9);
g.FillRectangle(new SolidBrush(Color.Gainsboro), 3, this.ClientSize.Height - 9, this.ClientSize.Width - 5, 7);
ColorBox(10, 10, this.ClientSize.Width-10, 40, 2, true, false, Color.Gainsboro);
g.FillRectangle(new SolidBrush(Color.Gainsboro), mineCountControl1.Right, 12, faceControl1.Left - mineCountControl1.Right, 27);
g.FillRectangle(new SolidBrush(Color.Gainsboro), faceControl1.Right, 12, timerControl1.Left - faceControl1.Right , 27);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
ColorBox(10, 50, this.ClientSize.Width-10, this.ClientSize.Height-10, 3, true, false, Color.Gainsboro);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
ReDraw( this.MToAT(e.ClipRectangle.Left, e.ClipRectangle.Top), this.MToAT(e.ClipRectangle.Right, e.ClipRectangle.Bottom) );
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
protected override void OnMouseDown(MouseEventArgs e)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
Point p;
try
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
p = MToA(e.X, e.Y);
}
catch
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
return;
}
if(Turn[p.Y, p.X] == -1 || GameStartMark == false)//方格被翻开或游戏结束
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
return;
}
timerControl1.Start();
switch(e.Button)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
case MouseButtons.Left:
faceControl1.faceState = FaceControl.FaceState.Startle;
if(Turn[p.Y, p.X] != 1 )//未插红旗
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
DrawMine( p.X, p.Y, -4);
//记录鼠标单击时按下的方格
MP.X = p.X; MP.Y = p.Y;
}
break;
case MouseButtons.Right:
if(Turn[p.Y, p.X] == 0)//未插红旗则插入红旗
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
Turn[p.Y, p.X] = 1;
mineCountControl1.mineCount --;
}
else
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
Turn[p.Y, p.X] = 0;
mineCountControl1.mineCount ++;
}
ReDraw(p, p);
break;
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
protected override void OnMouseUp(MouseEventArgs e)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
Point p;
try
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
p = MToA(e.X, e.Y);
}
catch
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
if(InBox(new System.Drawing.Rectangle(0, 0, BoardWidth - 1, BoardHeight - 1), MP) == true) ReDraw(MP, MP);
faceControl1.faceState = FaceControl.FaceState.Up;
return;
}
if(GameStartMark == false)//如果游戏结束
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
return;
}
switch(e.Button)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
case MouseButtons.Left:
if(MP.X == p.X && MP.Y == p.Y && Turn[p.Y, p.X] != 1)//未插红旗
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
if(Turn[p.Y, p.X] != -1 )//未被翻开
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
if(Mine[p.Y, p.X] == 0)//如果这个位置周围没有地雷
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
ClearBoardMine(p.X, p.Y);//自动清除无雷区
}
else
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
Turn[p.Y, p.X] = -1;
if(Mine[p.Y, p.X] == -1)//碰到地雷
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
timerControl1.Stop();
Mine[p.Y, p.X] = -2;
//游戏结束
for(int i=0; i< BoardHeight; i++)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
for(int j=0; j< BoardWidth; j++)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
Turn[i, j] = -1;
}
}
ReDraw(new Point(0, 0), new Point(BoardWidth-1, BoardHeight-1) );
faceControl1.faceState = FaceControl.FaceState.Die;
GameStartMark = false;
return;
}
ReDraw( p, p );
}
faceControl1.faceState = FaceControl.FaceState.Up;
//统计游戏结果
int tn=0;
for(int i=0; i< BoardHeight; i++)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
for(int j=0;j< BoardWidth; j++)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
if(Turn[i,j] == -1) tn++;
}
}
if(tn == (BoardWidth * BoardHeight - MineCount) )//胜利
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
timerControl1.Stop();
System.Windows.Forms.MessageBox.Show("恭喜您凯旋归来!","游戏结束");
ReStart();
}
}
}
else
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
if(InBox(new System.Drawing.Rectangle(0, 0, BoardWidth - 1, BoardHeight - 1), MP) == true) ReDraw(MP, MP);
faceControl1.faceState = FaceControl.FaceState.Up;
}
break;
}
}
//重画
private void ReDraw(Point p1, Point p2)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
if (InBox(new Rectangle(0, 0, BoardWidth - 1, BoardHeight - 1), p1) == false || InBox(new Rectangle(0, 0, BoardWidth - 1, BoardHeight - 1), p2) == false) return;
System.Drawing.Graphics g = this.CreateGraphics();
for (int i = p1.Y; i <= p2.Y; i++)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
for (int j = p1.X; j <= p2.X; j++)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
switch (Turn[i, j])
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
case 0://未动过
DrawMine(j, i, -3);
break;
case 1://插入红旗
DrawMine(j, i, -5);
break;
default:
DrawMine(j, i, Mine[i, j]);
break;
}
}
}
}
//重画
private void ReDraw()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
System.Drawing.Graphics g = this.CreateGraphics();
System.Drawing.Rectangle rec = new Rectangle(0, 0, this.ClientRectangle.Right, this.ClientRectangle.Bottom);
OnPaint(new PaintEventArgs(g, rec));
}
//重新开始
private void ReStart()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
Reset(BoardWidth, BoardHeight, MineCount);
}
//以指定的行、列和地雷数量初始化,重新开始
private void Reset(int NWidth, int NHeight, uint NCount)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
System.Random ran = new Random(System.DateTime.Now.Second + (int)System.DateTime.Now.Ticks);
BoardWidth = NWidth;
BoardHeight = NHeight;
Count = NCount;
Mine = new int[NHeight, NWidth];
Turn = new int[NHeight, NWidth];
//初始化地雷
for (int i = 0; i < NHeight; i++)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
for (int j = 0; j < NWidth; j++)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
Mine[i, j] = 0; Turn[i, j] = 0;
}
}
//生成 N 颗地雷
for (int i = 0; i < NCount; i++)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
int nx, ny; do
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
nx = ran.Next(0, NWidth); ny = ran.Next(0, NHeight);
}
while (Mine[ny, nx] == -1); Mine[ny, nx] = -1;
//描述雷区
for (int j = nx - 1; j <= nx + 1; j++)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
for (int k = ny - 1; k <= ny + 1; k++)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
if ((j >= 0 && j < NWidth) && (k >= 0 && k < NHeight))
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
if (Mine[k, j] != -1) Mine[k, j]++;
}
}
}
}
this.ClientSize = new System.Drawing.Size(26 + BoardWidth * 20, 66 + BoardHeight * 20);
//设置窗体大小
GameStartMark = true; mineCountControl1.Location = new Point(20, 13);
faceControl1.Location = new Point(this.ClientSize.Width / 2 - 10, 15);
timerControl1.Location = new Point(this.ClientSize.Width - 70, 13);
timerControl1.Clear();
ReDraw();
faceControl1.faceState = FaceControl.FaceState.Up;
}
//在指定位置画物件
private void DrawMine(int x, int y, int Value)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
System.Drawing.Graphics g = this.CreateGraphics();
System.Drawing.Pen p = new Pen(Color.Black, 1);
//确定画笔的颜色和粗细
System.Drawing.Color textColor = new Color();
x = 13 + x * 20; y = 53 + y * 20;
switch (Value)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
case -5://画红旗
ColorBox(x, y, x + 19, y + 19, 2, false, true, Color.Gainsboro);
g.DrawLine(p, x + 8, y + 14, x + 12, y + 14);
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
g.FillPolygon(new SolidBrush(Color.Red), new Point[] ...{ new Point(x + 12, y + 11), new Point(x + 12, y + 2), new Point(x + 5, y + 7) });
p.Width = 2; g.DrawLine(p, x + 5, y + 16, x + 15, y + 16);
g.DrawLine(p, x + 11, y + 11, x + 11, y + 14);
return;
case -4://画按下的区域
ColorBox(x, y, x + 19, y + 19, 2, true, true, Color.Gainsboro);
return;
case -3://画未按下的区域
ColorBox(x, y, x + 19, y + 19, 2, false, true, Color.Gainsboro);
return;
case -2://画爆炸的地雷
case -1://画未爆炸的地雷
g.DrawRectangle(new Pen(System.Drawing.Color.Gray, 2), x + 1, y + 1, 19, 19);
g.FillRectangle(new SolidBrush((Value == -2) ? Color.Red : Color.Gainsboro), x + 2, y + 2, 17, 17);
g.FillEllipse(new SolidBrush(Color.Black), x + 4, y + 4, 12, 12);
g.DrawLine(p, x + 4, y + 4, x + 16, y + 16); g.DrawLine(p, x + 10, y + 3, x + 10, y + 17);
g.DrawLine(p, x + 15, y + 4, x + 4, y + 15);
g.DrawLine(p, x + 3, y + 10, x + 18, y + 10);
g.FillEllipse(new SolidBrush(Color.White), x + 7, y + 7, 3, 3);
return;
default:
//确定字体颜色
switch (Value)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
case 0: textColor = Color.Gainsboro; break;
case 1: textColor = Color.Blue; break;
case 2: textColor = Color.Green; break;
case 3: textColor = Color.Red; break;
case 4: textColor = Color.DarkBlue; break;
case 5: textColor = Color.DarkGreen; break;
case 6: textColor = Color.DarkRed; break;
case 7: textColor = Color.Fuchsia; break;
case 8: textColor = Color.Black; break;
}
//显示周围的雷数
g.DrawRectangle(new Pen(System.Drawing.Color.Gray, 2), x + 1, y + 1, 19, 19);
g.FillRectangle(new SolidBrush(Color.Gainsboro), x + 2, y + 2, 17, 17);
g.DrawString(Value.ToString(), this.Font, new System.Drawing.SolidBrush(textColor), x + 2, y - 2);
return;
}
}
private void menuItem3_Click(object sender, System.EventArgs e)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
ReStart();
}
private void menuItem4_Click(object sender, System.EventArgs e)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
Reset(9, 9, 10);
}
private void menuItem6_Click(object sender, System.EventArgs e)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
Reset(16, 16, 40);
}
private void menuItem7_Click(object sender, System.EventArgs e)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
Reset(30, 16, 99);
}
private void menuItem13_Click(object sender, System.EventArgs e)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
new Help().ShowDialog();
}
private void menuItem14_Click(object sender, System.EventArgs e)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
new About().ShowDialog();
}
private void menuItem12_Click(object sender, System.EventArgs e)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
Application.Exit();
}
}
扫雷这个游戏我以前用Turb C做过,Dos界面,图像效果很差。而且我整整做了三个星期。这回用VS.Net重做感觉不同了,窗口编程容易了许多,做出来的效果也好了很多,几乎和Windows的扫雷差不了多少。直到现在我才明白为什么C#被称为用于编写"下一代窗口服务",由于平易近人的图形界面极大的减轻了源代码编程的负担,VS.Net可以在较短周期内开发应用程序,所开发的程序又具有良好的可移植性。我想C#至关重要的各种要素就是下面这些吧:简单
C#中,没有C++中流行的指针。在C++中,有::、.、和->操作符,它们用于名字空间、成员和引用。对于新手来说,操作符至今仍是学习的一道难关。C#弃用其它操作符,仅使用单个操作符 "."。现在一个程序员所需要理解的就是嵌套名字的注解了。C#使用统一的类型系统,屏弃了C++多变的类型系统。这种系统充许您把各种类型作为一个对象查看,它是一个原始类型还是一个full-blown 类。和其它编程语言相比,由于加框(boxing)和消框(unboxing)的机制,把简单类型当作对象处理并不能获得性能的改善。