论坛首页 编程语言技术论坛

自编扫雷游戏

浏览 3115 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-11-05   最后修改:2010-04-30
    有段时间没有更新自己的博客了,一是学习紧,二是忙着工作的事。好了,现在拿出在学习C#过程中所做的扫雷小程序,在代码优化方面还有很多不足之处,希望大家多多拍砖,小弟还等着回家盖房呢,呵呵...

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() + "秒";
        }
       
    }
}
论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics