C#小游戏--扫雷【全程注释,多处注释不当,请指正】

 

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
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 Form1 : Form
{
public Form1()
{
InitializeComponent();
// 初始化窗口控件
AddButton(); // 铺设BUTTON
CreateLable(); // 铺设lable

}
private const int Xcount = 20 ; // 设置雷区长度20
private const int Ycount = 20 ; // 设置雷区高度20
private Label[,] labels = new Label[Xcount, Ycount]; // 定义二维数组labels
private Button[,] buttons = new Button[Xcount, Ycount]; // 定义二维数组buttons
private void AddButton() // 铺设BUTTON,AddButton()方法
{
for ( int i = 0 ; i < Xcount; i ++ ) // 横坐标循环
{
for ( int j = 0 ; j < Ycount; j ++ ) // 纵坐标循环
{
Button btn
= new Button(); // 初始化新实例btn
btn.Click += new EventHandler(btn_Click); // 委托事件btn_Click
// btn.Click +=new EventHandler(btn_MouseDown);
btn.MouseUp += new MouseEventHandler(btn_MouseUp); // 委托事件btn_MouseUp
btn.Location = new Point(i * 30 , 30 * j); // 将btn定位在FROM上
btn.Size = new Size( 30 , 30 ); // 设置高度和宽度
this .Controls.Add(btn); // 将btn添加到控件集合中
// 利用Tag使按钮和坐标建立联系
Point pt = new Point(i, j); // 初始化新实例pt
btn.Tag = pt; // 将点的坐标的有序对(数据)赋值给btn.tag
buttons[i, j] = btn; // 将实例btn赋值给数组
}
}
}
void btn_MouseUp( object sender, MouseEventArgs e) // btn_MouseUp事件
{
if (e.Button == MouseButtons.Right) // 捕捉鼠标按键
{
Button btn
= (Button)sender; // 将委托传送的实例初始化新实例
if (btn.BackgroundImage == null ) // 如果btn背景图片是空
{
btn.BackgroundImage
= Properties.Resources.挖雷工兵; // 将图片替换成挖雷工兵
}
else // 如果btn背景图片不为空
{
btn.BackgroundImage
= null ; // 将图片背景替换为空
}
}
}


private Label[] labels2 = new Label[Xcount * Ycount]; // 初始化新一维数组label[]
int indexOfLable = 0 ; // 定义索引变量indexofLabel
private void CreateLable() // 铺设Lable
{
int [] ints = new int [ 400 ]; // 定义一维数组ints[]
Random rd = new Random(); // 初始化随机实例
for ( int i = 0 ; i < 400 ; i ++ ) // 400次循环
{

ints[i]
= rd.Next( 1 , 400 ); // 得到一个1~400的随机数赋值给ints数组
}
for ( int i = 0 ; i < Xcount; i ++ ) // 横坐标循环
{
for ( int j = 0 ; j < Ycount; j ++ ) // 纵坐标循环
{
Label lb
= new Label(); // 初始化新实例lb
lb.Location = new Point(i * 30 , 30 * j); // 将lb定位到FORM上
lb.Size = new Size( 30 , 30 ); // 设置lb的高度和宽度
lb.BorderStyle = BorderStyle.Fixed3D; // 设置lb的样式
this .Controls.Add(lb); // 将lb添加到控件集合中
labels[i, j] = lb; // 将lb赋值到labels二维数组中
labels2[indexOfLable] = lb; // 将lb赋值到labels2一维数组中
indexOfLable ++ ; // 索引加1
}
}
for ( int i = 0 ; i < ints.Length; i ++ ) // 循环400次
{
if (ints[i] < 40 ) // 如果数组中的元素小于40
{
labels2[i].Image
= Properties.Resources.标准地雷; // labels2一维数组中的lb对象图片为标准地雷的图片
}
}
}


void btn_Click( object sender, EventArgs e) // 委托事件btn_Click
{
Button btn
= (Button)sender; // 将委托传送的实例初始化新实例
this .Controls.Remove(btn); // 从控件集合中移除btn控件
Point pt = (Point)btn.Tag; // 将btn坐标的有序对初始化成pt实例
x = pt.X; // 将pt的横坐标赋值给x
y = pt.Y; // 将pt的纵坐标赋值给y
if (labels[x, y].Image != null ) // 如果二维数组labels中对应点击到的btn覆盖的lable的图片不为空
{
MessageBox.Show(
" 游戏结束,果然是踩雷高手 " ); // 弹出对话框,游戏结束
ReStart(); // 重新开始
return ; // 返回
}
CheckLable(x, y);
// 遍历周围8格
}



int x = 0 ; // 定义变量x=0
int y = 0 ; // 定义变量y=0
int num = 0 ; // 定义变量num=0
/// <summary>
/// 遍历周围8格
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
private void CheckLable( int x, int y) // 遍历周围8格
{
if (x < Xcount && x >= 0 && y >= 0 && y < Ycount) // 如果x和y在横坐标和纵坐标范围内
{
num
= 0 ; // 变量num=0
num = GetCount(x, y); // 变量num=某个格子周围的不是雷的个数
if (num == 8 ) // 如果周围不是雷的个数是8
{
num
= 0 ; // 变量num=0
string tag = buttons[x, y].Tag.ToString(); // 原点坐标数据转化为字符串赋值给tag
buttons[x, y].Tag = 0 ; // 原点数据赋值为0
if (labels[x, y].Image == null && tag != " 0 " ) // 如果原点图像为空或者坐标数据不为0
{
x
++ ; // 右移一格
RemoveButton(x, y); // 移除
y ++ ; // 下移一格
RemoveButton(x, y); // 移除
x -- ; // 左移一格
RemoveButton(x, y); // 移除
x -- ; // 左移一格
RemoveButton(x, y); // 移除
y -- ; // 上移一格
RemoveButton(x, y); // 移除
y -- ; // 上移一格
RemoveButton(x, y); // 移除
x ++ ; // 右移一格
RemoveButton(x, y); // 移除
x ++ ; // 右移一格
RemoveButton(x, y); // 移除
if (tag != " 0 " ) // 如果坐标数据不为0
{
num
= 0 ; // 变量num=0
Recursion(x, y); // 递归调用
}
}
}
else // 如果原点周围有地雷
{
if (x < Xcount && x >= 0 && y >= 0 && y < Ycount) // 如果原点坐标在横坐标和纵坐标的范围内
{
labels[x, y].Text
= ( 8 - num).ToString(); // 原点文本显示地雷数

num
= 0 ; // num赋值为0
return ; // 返回
}
}
}
}

/// <summary>
/// 递归
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
private void Recursion( int x, int y) // 递归检测周围的8个Lable
{
CheckLable(x, y);
// 调用CheckLable(x,y)递归
y ++ ; // 下移一格
CheckLable(x, y); // 调用CheckLable(x,y)递归
y ++ ; // 下移一格
CheckLable(x, y); // 调用CheckLable(x,y)递归
x -- ; // 左移一格
CheckLable(x, y); // 调用CheckLable(x,y)递归
x -- ; // 左移一格
CheckLable(x, y); // 调用CheckLable(x,y)递归
y -- ; // 上移一格
CheckLable(x, y); // 调用CheckLable(x,y)递归
y -- ; // 上移一格
CheckLable(x, y); // 调用CheckLable(x,y)递归
x ++ ; // 右移一格
CheckLable(x, y); // 调用CheckLable(x,y)递归

}

/// <summary>
/// 验证单格
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
private void RemoveButton( int x, int y) // 移除当前坐标的按钮
{
if (x < Xcount && x >= 0 && y >= 0 && y < Ycount) // 如果坐标在横坐标和纵坐标的范围内
{
if (labels[x, y].Image == null ) // 如果坐标label的图像为空
{
this .Controls.Remove(buttons[x, y]); // 移除该坐标上的btn

}
}
}


int temp2 = 0 ; // 定义变量temp2=0
/// <summary>
/// 得到某个格子周围的不是雷的个数
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
private int GetCount( int x, int y) // 方法GetCount(int x,int y)得到某个格子周围不是雷的个数
{

temp2
= 0 ; // 变量temp2=0
x ++ ; // 向右移动一格
if (x < Xcount && x >= 0 && y >= 0 && y < Ycount) // 如果新坐标仍在横坐标和纵坐标的范围内
{
if (labels[x, y].Image == null ) // 如果新坐标上label的图像为空
{
temp2
++ ; // temp2++
}
}
else // 如果新坐标不在横坐标和纵坐标的范围内
{ temp2 ++ ; } // temp2++

y
++ ; // 向下移动一格
if (x < Xcount && x >= 0 && y >= 0 && y < Ycount) // 如果新坐标仍在横坐标和纵坐标的范围内
{
if (labels[x, y].Image == null ) // 如果新坐标上label的图像为空
{
temp2
++ ; // temp2++
}
}
else // 如果新坐标不在横坐标和纵坐标的范围内
{ temp2 ++ ; } // temp2++

x
-- ; // 向左移动一格
if (x < Xcount && x >= 0 && y >= 0 && y < Ycount) // 如果新坐标在横坐标和纵坐标的范围内
{
if (labels[x, y].Image == null ) // 如果新坐标上label的图像为空
{
temp2
++ ; // temp2++
}
}
else // 如果新坐标不在横坐标和纵坐标的范围内
{ temp2 ++ ; } // temp2++
x -- ; // 向左移动一格
if (x < Xcount && x >= 0 && y >= 0 && y < Ycount) // 如果新坐标在横坐标和纵坐标的范围内
{
if (labels[x, y].Image == null ) // 如果新坐标label上的图像为空
{
temp2
++ ; // temp2++
}
}
else // 如果新坐标不在横坐标和纵坐标的范围内
{ temp2 ++ ; } // temp2++
y -- ; // 向上移动一格
if (x < Xcount && x >= 0 && y >= 0 && y < Ycount) // 如果新坐标在横坐标和纵坐标的范围内
{
if (labels[x, y].Image == null ) // 如果新坐标label的图像为空
{
temp2
++ ; // temp2++
}
}
else // 如果新坐标不在横坐标和纵坐标的范围内
{ temp2 ++ ; } // temp2++
y -- ; // 向上移动一格
if (x < Xcount && x >= 0 && y >= 0 && y < Ycount) // 如果新坐标在横坐标和纵坐标的范围内
{
if (labels[x, y].Image == null ) // 如果新坐标label的图像为空
{
temp2
++ ; // temp2++
}
}
else // 如果新坐标不在横坐标和纵坐标的范围内
{ temp2 ++ ; } // temp2++
x ++ ; // 向右移动一格
if (x < Xcount && x >= 0 && y >= 0 && y < Ycount) // 如果新坐标在横坐标和纵坐标的范围内
{
if (labels[x, y].Image == null ) // 如果新坐标的图像为空
{
temp2
++ ; // temp2++
}
}
else // 如果新坐标不在横坐标和纵坐标的范围内
{ temp2 ++ ; } // temp2++
x ++ ; // 向右移动一格
if (x < Xcount && x >= 0 && y >= 0 && y < Ycount) // 如果新坐标在横坐标和纵坐标的范围内
{
if (labels[x, y].Image == null ) // 如果新坐标的图像为空
{
temp2
++ ; // temp2++
}
}
else // 如果新坐标不在横坐标和纵坐标的范围内
{ temp2 ++ ; } // temp2++

return temp2; // 返回temp2(原坐标不是雷的个数)
}

public void ReStart() // 重新运行游戏
{
this .Controls.Clear(); // 移除所有控件
InitializeComponent(); // 初始化控件窗口
temp2 = 0 ; // 变量temp2=0
x = 0 ; // 坐标x=0
y = 0 ; // 坐标y=0
indexOfLable = 0 ; // 索引赋值为0
num = 0 ; // 变量num=0
AddButton(); // 铺设button
CreateLable(); // 铺设lable
}
private void btnReStart_Click( object sender, EventArgs e) // 单击重新开始事件
{
ReStart();
// 触发重启方法
}

private void Form1_Load( object sender, EventArgs e)
{

}

}
}

转载于:https://www.cnblogs.com/xulong/archive/2010/03/16/1687323.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值