• 原创:扫雷游戏


      运行效果如下:

       

    源码下载

    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 MineSweeper
    {
        
    public partial class Form1 : Form
        {
            
    public Form1()
            {
                InitializeComponent();
            }
            
    //
            List<Mine> list;
            
    //雷区
            int[] MineList;
            
    //图片尺寸
            int ImgSize = 18;
            
    //计时器
            int second = 0;
            
    //标记数
            int flagcount = 0;
            
    void timer1_Tick(object sender, EventArgs e)
            {
                second
    ++;
                labTime.Text 
    = second.ToString();
            }
            
    private void btnStart_Click(object sender, EventArgs e)
            {
                NewGame();
            }
            
    private void Mine_Leave(object sender, EventArgs e)
            {
                ((Mine)sender).BackgroundImage 
    = Properties.Resources.ico_normal;
            }
            
    private void Mine_Hover(object sender, EventArgs e)
            {
                ((Mine)sender).BackgroundImage 
    = Properties.Resources.ico_over;
            }
            
    /// <summary>
            
    /// 点击雷
            
    /// </summary>
            
    /// <param name="sender"></param>
            
    /// <param name="e"></param>
            private void Mine_Click(object sender, MouseEventArgs e)
            {
                Mine mine 
    = ((Mine)sender);
                mine.BackgroundImage 
    = Properties.Resources.ico_over;

                
    if (e.Button == MouseButtons.Left)
                {
                    OpenMine(mine);
                }
                
    else if (e.Button == MouseButtons.Right)
                {
                    
    //标记雷
                    switch (mine.State)
                    {
                        
    case MineState.normal:
                            mine.Image 
    = Properties.Resources.ico_flag;
                            mine.State 
    = MineState.flag;
                            flagcount
    ++;
                            
    break;
                        
    case MineState.flag:
                            mine.Image 
    = null;
                            mine.State 
    = MineState.normal;
                            flagcount
    --;
                            
    break;
                        
    case MineState.sweeped:
                            
    break;
                    }
                    labFlag.Text 
    = flagcount.ToString();
                }
            }
            
    /// <summary>
            
    /// 是否能赢
            
    /// </summary>
            bool IsWin
            {
                
    get
                { 
                    
    return list.Where(m => m.State == MineState.normal || m.State == MineState.flag).Count() == 10;
                }
            }
            
    /// <summary>
            
    /// 挂了
            
    /// </summary>
            private void Dead()
            {
                timer1.Stop();
                
    foreach (Mine mine in list)
                {
                    
    if (mine.IsMine)
                    {
                        mine.Image 
    = Properties.Resources.ico_mine;
                    }
                }
                GameOver();
            }
            
    /// <summary>
            
    /// 赢了
            
    /// </summary>
            void Win()
            {
                timer1.Stop();
                MessageBox.Show(
    "OY!你胜利啦!~~");
                GameOver();
            }
            
    /// <summary>
            
    /// 开始游戏
            
    /// </summary>
            void NewGame()
            {
                
    //newGameToolStripMenuItem.Enabled = false;
                second = 0;
                flagcount 
    = 0;
                timer1.Interval 
    = 1000;
                timer1.Start();
                panel1.Enabled 
    = true;
                panel1.Controls.Clear();
                
    //初始化雷区
                list = new List<Mine>(81);

                
    //随机决定9个雷
                MineList = GetRandMine(109 * 9);
                
    //布雷
                for (int i = 0; i < 81; i++)
                {
                    Mine mine 
    = new Mine();
                    
    if (MineList.Contains(i))
                        mine.IsMine 
    = true;
                    
    else
                        mine.IsMine 
    = false;
                    mine.X 
    = i / 9;
                    mine.Y 
    = i % 9;
                    mine.Left 
    = mine.X * ImgSize;
                    mine.Top 
    = mine.Y * ImgSize;
                    mine.MouseClick 
    += new MouseEventHandler(Mine_Click);
                    mine.MouseHover 
    += new EventHandler(Mine_Hover);
                    mine.MouseLeave 
    += new EventHandler(Mine_Leave);

                    list.Add(mine);
                    panel1.Controls.Add(mine);
                }
                
    #region 设置雷区所有雷的数目
                
    foreach (Mine mine in list)
                {
                    
    if (mine.IsMine)
                    {
                        
    foreach (Mine mine1 in list.Where(m => m.State == MineState.normal && Math.Abs(m.X - mine.X) < 2 && Math.Abs(m.Y - mine.Y) < 2).ToList())
                        {
                            mine1.MineCount
    ++;
                        }

                    }
                }
                
    #endregion
                
    //调整panel大小
                panel1.Width = 9 * ImgSize;
                panel1.Height 
    = 9 * ImgSize;
            }
            
    /// <summary>
            
    /// 结束
            
    /// </summary>
            void GameOver()
            {
                panel1.Enabled 
    = false;
                newGameToolStripMenuItem.Enabled 
    = true;
                timer1.Stop();
                
            }
            
    /// <summary>
            
    /// 探雷
            
    /// </summary>
            
    /// <param name="mine"></param>
            void OpenMine(Mine mine)
            {
                
    if (mine.State == MineState.flag)
                    
    return;
                
    //是雷 GameOver
                if (mine.IsMine)
                {
                    mine.BackgroundImage 
    = Properties.Resources.ico_boom;
                    Dead();
                    
    return;
                }
                
    //不是雷
                mine.State = MineState.sweeped;
                mine.Image 
    = Properties.Resources.ico_bg;
                
    if (IsWin)
                {
                    Win();
                    
    return;
                }
                
    if (mine.MineCount > 0)
                {
                    mine.Controls.Add(
    new Label()
                    {
                        Text 
    = mine.MineCount.ToString(),
                        ForeColor 
    = GetColor(mine.MineCount),
                        BackColor 
    = Color.Transparent,
                        Font 
    = new System.Drawing.Font("Microsoft YaHei", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)))
                    });
                }
                
    else
                {
                    
    //递归探雷
                    foreach (Mine mine1 in list.Where(m => m.IsMine == false && m.State == MineState.normal && Math.Abs(m.X - mine.X) < 2 && Math.Abs(m.Y - mine.Y) < 2).ToList())
                    {
                        OpenMine(mine1);
                    }
                }
            }
            
    /// <summary>
            
    /// 设字体颜色
            
    /// </summary>
            
    /// <param name="count"></param>
            
    /// <returns></returns>
            private Color GetColor(int count)
            {
                
    switch (count)
                {
                    
    default:
                        
    return Color.Blue;
                    
    case 2:
                        
    return Color.Green;
                    
    case 3:
                        
    return Color.OrangeRed;
                    
    case 4:
                        
    return Color.DeepPink;
                    
    case 5:
                        
    return Color.Red;
                    
    case 6:
                        
    return Color.Gold;
                }
            }
            
    /// <summary>
            
    /// 随机获取雷位置
            
    /// </summary>
            
    /// <param name="Count"></param>
            
    /// <param name="Max"></param>
            
    /// <returns></returns>
            int[] GetRandMine(int Count, int Max)
            {
                Random rand 
    = new Random();
                
    int[] result = new int[Count];
                
    for (int i = 0; i != Count; i++)
                {
                    
    int tmp = rand.Next(Max);
                    
    if (tmp > 0 && !result.Contains(tmp))
                        result[i] 
    = tmp;
                }
                
    return result;
            }

        }
        
    /// <summary>
        
    /// 雷
        
    /// </summary>
        public class Mine : System.Windows.Forms.PictureBox
        {
            
    public Mine()
            {
                
    base.BackgroundImage = Properties.Resources.ico_normal;
                
    base.Width = 18;
                
    base.Height = 18;
                State 
    = MineState.normal;
            }

            
    public int X { getset; }
            
    public int Y { getset; }
            
    public bool IsMine { getset; }
            
    public int MineCount { getset; }
            
    public MineState State { getset; }
        }
        
    /// <summary>
        
    /// 雷状态
        
    /// </summary>
        public enum MineState
        {
            sweeped,
            normal,
            flag
        }
    }
  • 相关阅读:
    选择高性能NoSQL数据库的5个步骤
    如何将 Redis 用于微服务通信的事件存储
    让你的AI模型尽可能的靠近数据源
    Collections.sort 给集合排序
    Bootstrap 文件上传插件File Input的使用
    select2 api参数的文档
    textarea 标签换行及靠左
    JSON
    JDK的get请求方式
    通讯录作业
  • 原文地址:https://www.cnblogs.com/mad/p/1467281.html
Copyright © 2020-2023  润新知