zoukankan      html  css  js  c++  java
  • GDI绘图写的简单扫雷

    由于没话多少时间,这个扫雷我只实现了主要功能(扫雷功能,递归实现)

    废话不多说,直接上代码

    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 saolei
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            Graphics gs;
            Random rand;
            List<Point> leiPoint;//存放雷的坐标
            List<Point> isSearch;//存放已扫描过得坐标
            private void Form1_Load(object sender, EventArgs e)
            {
                leiPoint = new List<Point>();
                rand = new Random();
            }
    
    
            private void btnStart_Click(object sender, EventArgs e)
            {
                setLei();
                isSearch = new List<Point>();//实例化扫描过得雷区
                gs = pnlLei.CreateGraphics();//创建画图对象
                gs.Clear(this.pnlLei.BackColor);//清除所有
                for (int i = 0; i < 400; i+=20)//循环绘制地图
                    for (int j = 0; j < 400; j+=20)
                        gs.FillRectangle(new SolidBrush(Color.Gray), i - 1, j-1, 19, 19);
    
                foreach (Point p in leiPoint)//额外绘制地雷(可以注释掉)
                    gs.FillRectangle(new SolidBrush(Color.Purple), p.X-2, p.Y-2, 19, 19);
    
    
            }
    
            //设置雷的方法
            private void setLei()
            {
                for (int i = 0; i < 20; i++)//循环生成20的地雷
                {
                    int x = rand.Next(0, 20);
                    int y = rand.Next(0, 20);
                    foreach (Point p in leiPoint)
                    {
                        if (p == new Point(x, y))
                        {
                            i--;
                        } continue;
                    }
                    leiPoint.Add(new Point(x * 20 + 1, y * 20 + 1));
                }
            }
    
            
            //private Color getColor()
            //{
            //    return Color.FromArgb(rand.Next(1, 255), rand.Next(1, 255), rand.Next(1, 255));
            //}
    
            private void pnlLei_MouseClick(object sender, MouseEventArgs e)
            {
                if (e.Button == MouseButtons.Left)//判断鼠标是否使用左键单击的panel控件
                {
                    if (!isOk(e.Location.X, e.Location.Y))//判断是否是扫描过得雷区
                    {
                        isSearch.Add(new Point(e.Location.X, e.Location.Y));//加入扫描雷区
                        int x = e.Location.X / 20 * 20 + 1;//将鼠标单击的坐标X调成最接近的哪一个雷区的坐标
                        int y = e.Location.Y / 20 * 20 + 1;//将鼠标单击的坐标Y调成最接近的哪一个雷区的坐标
                        if (x > 390 || x < -2) return;//判断是否超出X轴界限(结束递归)
                        if (y > 390 || y < -2) return;//判断是否超出Y轴界限(结束递归)
                        int count = getLeiCount(x, y);//获取周围雷的个数
                        if (count > 0)//判断是否有雷(结束递归)
                        {
                            gs.DrawString(count.ToString(), new Font("楷体", 10),//绘制雷的个数
                                new SolidBrush(Color.Red), x + 4, y + 4);
                            return;
                        }
                        gs.FillRectangle(new SolidBrush(Color.Blue), x - 2, y - 2, 19, 19);
                        pnlLei_MouseClick(sender, new MouseEventArgs(e.Button, 1, x + 20, y, 0));//递归调用本次单击事件(向右扫描雷区)
                        pnlLei_MouseClick(sender, new MouseEventArgs(e.Button, 1, x - 20, y, 0));//递归调用本次单击事件(向左扫描雷区)
                        pnlLei_MouseClick(sender, new MouseEventArgs(e.Button, 1, x, y + 20, 0));//递归调用本次单击事件(向下扫描雷区)
                        pnlLei_MouseClick(sender, new MouseEventArgs(e.Button, 1, x, y - 20, 0));//递归调用本次单击事件(向上扫描雷区)
    
                    }
    
                }
            }
    
          //后去周围8个方向雷的个数
            private int getLeiCount(int x, int y)
            {
                int leiCount = 0;
    
                foreach (Point p in leiPoint)
                    if (p.X >= x - 20 && p.Y >= y - 20 && p.X <= x + 20 && p.Y <= y + 20)//循环判断周边是否有雷
                        leiCount++;
    
                return leiCount;
            }
    
            //判断是否是已经扫描过得雷区
            private bool isOk(int x, int y)
            {
                foreach (Point p in isSearch)
                    if (p == new Point(x, y))
                        return true;
                return false;
            }
        }
    }

    很多不足之处,望各位博友多指教

  • 相关阅读:
    WEB测试用例(十五)
    WEB测试用例(十二)
    WEB测试用例(九)
    WEB测试用例(六)
    WEB测试用例(四)
    WEB测试用例(一)
    WEB测试方法(十一)
    WEB测试方法(十)
    Python 知识要点:对象的 init 和 del 方法
    Python 知识要点:类 和 对象
  • 原文地址:https://www.cnblogs.com/caoxianbing/p/3746555.html
Copyright © 2011-2022 走看看