自编扫雷游戏

有段时间没有更新自己的博客了,一是学习紧,二是忙着工作的事。好了,现在拿出在学习C#过程中所做的扫雷小程序,在代码优化方面还有很多不足之处,希望大家多多拍砖,小弟还等着回家盖房呢,呵呵... :wink:

BlockView部分:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QueryMainSample1
{
public partial class BlockView : UserControl
{
public BlockView()
{
InitializeComponent();
this.isMarked = false;
this.isMine = false;
}
//属性
public const int Block_Width = 13;
public const int Block_Height = 13;
public int row;//行
public int col;//列
public bool isOpen { set; get; }//是否被打开
public bool isMine { set; get; }//是否有雷
public bool isMarked { set; get; }//是否被标记
//事件
public event MouseEventHandler Open;
//方法
private void BlockView_MouseClick(object sender, MouseEventArgs e)
{
if (this.Open!=null)
{
this.Open.Invoke(this,e);
}
}
}
}
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
扫雷form部分:

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 QueryMainSample1
{
public partial class SaoLei : Form
{
//Propertity
public const int ROWS = 5;//行
public const int COLS = 5;//列
public const int ALLMAINS = 2;//雷数
public int Width1;//宽度
public int Height1;//高度
public bool GameOver = false;//游戏结束与否
public bool Success = false;//成功
public int MarkedCount = ALLMAINS;//雷显示数
//public int MineCount { set; get; }
public int StartTime;//开始时间
List<BlockView> Lblock = new List<BlockView>();
//初始化
public SaoLei()
{
InitializeComponent();
this.initializeBlockValue();
}
//小块信息初始化方法
public void initializeBlockValue()
{
//随机排列小块
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLS; j++)
{
BlockView block = new BlockView();
block.row = i;
block.col = j;
block.label.Text = string.Empty;
block.Left = 10 + i * (BlockView.Block_Width + 2);
block.Top = 50 + j * (BlockView.Block_Height + 2);
block.Open += new MouseEventHandler(block_Open);
Lblock.Add(block);
this.Controls.Add(block);
}
}

//初始化版面
this.Width1 = 25 + ROWS * (BlockView.Block_Width +2);
this.Height1 = 85 + COLS * (BlockView.Block_Height +2);
if(this.Width1>325 || this.Height1>385){
this.Width = this.Width1;
this.Height = this.Height1;
}

//显示雷数目
this.textBoxMineCount.Text = "雷数:"+MarkedCount.ToString();

//随机排列雷
this.Mine();
//开始时timer1停止运行
this.timer1.Stop();
}

//随机排列雷
public void Mine()
{
Random r = new Random(DateTime.Now.Millisecond);
for (int i = 0; i < ALLMAINS; i++)
{
int n = r.Next(0, ROWS * COLS);
if (!Lblock.ElementAt(n).isMine)
{
Lblock.ElementAt(n).isMine = true;
}
else
{
i--;
}
}
}

//小块的Open事件
void block_Open(object sender, MouseEventArgs e)
{
if (this.Success || this.GameOver)
{
timer1.Stop();
return;
}
timer1.Start();
this.buttonReset.Text = "Reset";
BlockView block = sender as BlockView;
if (block.isOpen)
{
return;
}
if (e.Button == MouseButtons.Left)
{
if (this.Success || this.GameOver)
{
timer1.Stop();
this.buttonReset.Text = "Reset";
return;
}
//如果已被用户标记,则不可再点击
if (block.isMarked)
{
return;
}
if (block.isMine)
{
this.GameOver = true;
Lblock.Where(s => s.isMine).ToList().ForEach(s => s.BackColor = Color.Red);
timer1.Stop();
this.buttonReset.Text = "Failed";
MessageBox.Show("游戏失败!");
this.buttonReset.Text = "Reset";
return;
}
CheckMine(block);
if (Lblock.Count(s => s.isMarked && s.isMine) == ALLMAINS)
{
if (MarkedCount == 0)
{
this.textBoxMineCount.Text = "雷数:"+this.MarkedCount.ToString();
this.Success = true;
timer1.Stop();
this.buttonReset.Text = "Ok";
MessageBox.Show("恭喜您过关,你共" + this.textBoxTime.Text + "!");
this.buttonReset.Text = "Reset";
}
}
}
else if (e.Button == MouseButtons.Right)
{
if (block.isMarked)
{
this.MarkedCount++;
this.textBoxMineCount.Text = "雷数:" + this.MarkedCount.ToString();
block.isMarked = false;
block.BackColor = Color.DarkGray;
return;
}
else
{
block.isMarked = true;
this.MarkedCount--;
block.BackColor = Color.Black;
this.textBoxMineCount.Text = "雷数:"+this.MarkedCount.ToString();
if (this.MarkedCount == 0)
{
if (Lblock.Count(s => s.isMarked && s.isMine) == ALLMAINS)
{
this.Success = true;
timer1.Stop();
this.buttonReset.Text = "Ok";
MessageBox.Show("恭喜您过关,你共" + this.textBoxTime.Text +"!");
this.buttonReset.Text = "Reset";
}
}
}
}
}
//检查小块周围的块(雷)
public void CheckMine(BlockView block)
{
List<BlockView> Lblock2 = new List<BlockView>();
block.BackColor = Color.White;
block.isOpen = true;
foreach (BlockView s in Lblock)
{
if (((Math.Abs(s.row - block.row) == 1 && Math.Abs(s.col - block.col) == 1) || (Math.Abs(s.row - block.row) == 0 && Math.Abs(s.col - block.col) == 1) || (Math.Abs(s.row- block.row) == 1 && Math.Abs(s.col - block.col) == 0)) && s.BackColor != Color.White)
{
if (!s.isMarked)
{
Lblock2.Add(s);
}
}
}
if (Lblock2.Any(s => s.isMine))
{
string mineNumber = Lblock2.Count(s => s.isMine).ToString();
block.BackColor = Color.LightCyan;
block.label.Text = mineNumber;
return;
}
foreach (BlockView ss in Lblock2)
{
CheckMine(ss);
}
}
//ButtonResult的点击事件
private void buttonReset_Click(object sender, EventArgs e)
{
if (this.buttonReset.Text == "Reset")
{
Lblock.ForEach(s =>
{
s.isOpen = false;
s.isMine = false;
s.isMarked = false;
s.label.Text = string.Empty;
s.BackColor = Color.DarkGray;
});
this.Mine();
this.buttonReset.Text = "Welcome";
this.StartTime = 0;
this.textBoxTime.Text = "用时:0秒";
timer1.Stop();
this.textBoxMineCount.Text = "雷数:"+ALLMAINS.ToString();
this.MarkedCount = ALLMAINS;

if (this.Success)
{
this.Success = false;
}
if (this.GameOver)
{
this.GameOver = false;
}
}
}

//timer的tick事件,在textbox中显示时间
private void timer1_Tick(object sender, EventArgs e)
{
this.StartTime++;
this.textBoxTime.Text = "用时:" + StartTime.ToString() + "秒";
}

}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值