`
fatedgar
  • 浏览: 132283 次
  • 性别: Icon_minigender_1
  • 来自: 安徽
社区版块
存档分类
最新评论

扫雷-----C#窗体

    博客分类:
  • 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 bd1 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;

        }

    }

}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics