扫雷-----C#窗体

 

using System;

using System.Collections.Generic;

using System.Text;

using System.Drawing;

using System.Windows;

using System.Windows.Forms;

namespace winmine

{

    public class Map

    {

        public static int Time = 0;

        public static bool GameOver = false;

        public static bool IsSuccess = false;

        public static int UserMineCount = 0;

        public static int Width = 17 * 10;

        public static int Height = 17 * 10;

        public static int BlockWidth = 16;

        public static int MineNumber = 10;

        public static int RowCount = 9;

        public static int ColumnCount = 9;

        public static int MainMarked = 0;

        public static int BlockOpen = 0;

        public static List<BlockData> BlockDataList = new List<BlockData>();

        static Map()

        {

            Init();

        }

        public static void Init()

        {

            GameOver = false;

            Time = 0;

            Height = RowCount * BlockWidth;

            Width = ColumnCount * BlockWidth;

        }

        static void OpenUnit(BlockData bd)

        {

            if (!bd.IsMine)

            {

                if (bd.State == BlockState.Open || bd.State == BlockState.UserMine)

                {

                    return;

                }

                bd.State = BlockState.Open;

                BlockGraphics.DrawBlock(bd);

                BlockOpen++;

                if (bd.MineCount == 0)

                {

                    foreach (BlockData bd 1 in bd.BlockList)

                    {

                        OpenUnit(bd1);

                    }

                }

            }

 

        }

        public static void Showall()

        {

            foreach (BlockData bd in Map.BlockDataList)

            {

                if (bd.State == BlockState.Closed)

                {

                    bd.State = BlockState.Open;

                }

 

            }

        }

        public static void DrawBlocks()

        {

            if (!Map.GameOver)

                foreach (BlockData bd in Map.BlockDataList)

                {

                    BlockGraphics.DrawBlock(bd);

                }

            else

            {

                foreach (BlockData bd in Map.BlockDataList)

                {

                    if (!bd.IsMine && bd.State == BlockState.UserMine)

                    {

                        BlockGraphics.DrawBlock(bd.x, bd.y, BlockGraphics.MineBlastMap);

                    }

                    else if (bd.IsMine)

                    {

                        BlockGraphics.DrawBlock(bd.x, bd.y, BlockGraphics.MineMap);

                    }

                    else

                    {

                        BlockGraphics.DrawBlock(bd);

                    }

                }

            }

        }

        public static bool IsSuccessful()

        {

            if (Map.UserMineCount == 0)

            {

                int i = 0;

                for (i = 0; i < Map.BlockDataList.Count; i++)

                {

                    if (!Map.BlockDataList[i].IsMine && Map.BlockDataList[i].State == BlockState.UserMine)

                    {

                        break;

                    }

 

                }

                if (i < Map.BlockDataList.Count)

                {

                    return false;

                }

                else

                {

                    return true;

                }

            }

            else

            {

                return false;

            }

        }

        public static void SingleClick(int x, int y)

        {

            BlockData bd = GetBlock(x, y);

            if (GameOver)

            {

                return;

            }

            if (bd != null && bd.IsMine)

            {

 

                GameOver = true;

                Showall();

                DrawBlocks();

            }

            else

            {

                OpenUnit(bd);

            }

            if (IsSuccessful())

            {

                Map.IsSuccess = true;

 

            }

        }

        public static BlockData GetBlock(int x, int y)

        {

            if (x >= Map.ColumnCount * Map.BlockWidth || y > Map.RowCount * Map.BlockWidth)

            {

                return null;

            }

            int index = (y / Map.BlockWidth) * Map.ColumnCount + (x / Map.BlockWidth) % Map.ColumnCount;

            if (index < BlockDataList.Count)

            {

                return BlockDataList[index];

            }

            else

            {

                return null;

            }

 

        }

        public static int GetBlockIndex(int x, int y)

        {

            if (x >= Map.ColumnCount * Map.BlockWidth || y > Map.RowCount * Map.BlockWidth)

            {

                return -1;

            }

            int index = (y / Map.BlockWidth) * Map.ColumnCount + (x / Map.BlockWidth) % Map.ColumnCount;

            if (index < BlockDataList.Count)

            {

                return index;

            }

            else

            {

                return -1;

            }

        }

    }

    public enum BlockState

    {

        Closed, Open, UserMine

    }

    public class BlockGraphics

    {

        public static Graphics G;

        public static Graphics GG;

        public static Bitmap MemImage;

        public static Font DrawFont = new Font("Arial", 12, FontStyle.Bold);

        public static Bitmap ClosedMap;//Map for the closed state

        public static Bitmap OpenMap;//Map for the open state

        public static Bitmap BlankOpenMap;//Map for the blank state

        public static Bitmap UserMineMap;//Map for the mine user marked

        public static Bitmap MineBlastMap;//Map for the BlastMine

        public static Bitmap MineMap;//Map for the Mine

 

        static BlockGraphics()

        {

 

            ClosedMap = new Bitmap((Image)new Bitmap("image/closed.gif"), new Size(Map.BlockWidth, Map.BlockWidth));

            OpenMap = new Bitmap((Image)new Bitmap("image/open.gif"), new Size(Map.BlockWidth, Map.BlockWidth));

            BlankOpenMap = new Bitmap((Image)new Bitmap("image/open.gif"), new Size(Map.BlockWidth, Map.BlockWidth));

            UserMineMap = new Bitmap((Image)new Bitmap("image/usermine.gif"), new Size(Map.BlockWidth, Map.BlockWidth));

            MineBlastMap = new Bitmap((Image)new Bitmap("image/MineBlast.gif"), new Size(Map.BlockWidth, Map.BlockWidth));

            MineMap = new Bitmap((Image)new Bitmap("image/Mine.gif"), new Size(Map.BlockWidth, Map.BlockWidth));

        }

 

        public static void DrawBlock(int x, int y, Bitmap b)

        {

            if (G != null)

            {

                G.DrawImage(b, x, y);

                GG.DrawImage(b, x, y);

            }

        }

        public static void DrawBlock(BlockData bd)

        {

            Bitmap b = BlockGraphics.BlankOpenMap;

            switch (bd.State)

            {

                case BlockState.Closed: { b = BlockGraphics.ClosedMap; break; }

                case BlockState.Open: { b = BlockGraphics.OpenMap; break; }

                case BlockState.UserMine: { b = BlockGraphics.UserMineMap; break; }

            }

            DrawBlock(bd.x, bd.y, b);

            if (bd.State == BlockState.Open && bd.MineCount != 0)

            {

                StringFormat drawFormat = new StringFormat();

                SolidBrush drawBrush = new SolidBrush(System.Drawing.Color.Blue);

                switch (bd.MineCount)

                {

                    case 1: { drawBrush = new SolidBrush(System.Drawing.Color.Blue); break; }

                    case 2: { drawBrush = new SolidBrush(System.Drawing.Color.Green); break; }

                    case 3: { drawBrush = new SolidBrush(System.Drawing.Color.Red); break; }

                    case 4: { drawBrush = new SolidBrush(System.Drawing.Color.Red); break; }

                    case 5: { drawBrush = new SolidBrush(System.Drawing.Color.Blue); break; }

                    case 6: { drawBrush = new SolidBrush(System.Drawing.Color.Blue); break; }

                    case 7: { drawBrush = new SolidBrush(System.Drawing.Color.Blue); break; }

                    case 8: { drawBrush = new SolidBrush(System.Drawing.Color.Blue); break; }

 

                }

                G.DrawString(bd.MineCount.ToString(), DrawFont, drawBrush, bd.x, bd.y, drawFormat);

                GG.DrawString(bd.MineCount.ToString(), DrawFont, drawBrush, bd.x, bd.y, drawFormat);

            }

        }

        public BlockGraphics()

        {

 

        }

    }

    public class BlockData

    {

        public int ID = 0;

        public int x;

        public int y;

        public int MineCount = 0;//The mine count around it

        public List<BlockData> MineList = new List<BlockData>();

        public List<BlockData> BlockList = new List<BlockData>();

        public int UserMineCount = 0;//The mine count around it which user marked

        public BlockState State = BlockState.Closed;

        public bool IsMine = false;

        public BlockData(int x, int y, int MineCount)

        {

            this.x = x;

            this.y = y;

            this.MineCount = MineCount;

        }

        public BlockData(int x, int y)

        {

            this.x = x;

            this.y = y;

        }

    }

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值