码迷,mamicode.com
首页 > 其他好文 > 详细

扫雷代码

时间:2014-11-29 15:54:17      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:io   ar   os   使用   sp   for   on   bs   cti   


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("按enter键挖雷,按Q键标记雷,当雷区都标记正确,就胜利");
            Console.WriteLine("输入地雷区域的宽");
            int n = Convert.ToInt32(Console.ReadLine());//n标记雷区的宽和高
            Console.WriteLine("输入地雷的个数");
            int q = Convert.ToInt32(Console.ReadLine());//q标记雷的个数
            int q1 = q; //标记下.
            Console.Clear();
            int x = 0, y = 0;
            Console.SetCursorPosition(x, y);   //接受用户输入后的初始化值后,清屏
            string bianjie = "#";
            string[,] b = new string[n, n];//用二维数组b来标记显示的界面.
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    b[i, j] = "■";   //给每个元素赋这个标记
                    Console.Write(b[i, j]);//并显示界面
                    if (j == n - 1)//标记右边边界
                    {
                        Console.Write(bianjie);
                    }
                }
                Console.WriteLine();
            }
            for (int i2 = 0; i2 < n + 1; i2++)//标记下面边界
            {
                Console.Write(bianjie);
            }

            //初始化地雷
            int[,] a = new int[n, n];   //用数组a对应 显示数组b的坐标 来标记
            Random r = new Random(); //随机数的种子
            Program c = new Program(); //使用函数,申请类的实例C

            while (true)//填充地雷
            {
                int x1 = r.Next(0, n);
                int y1 = r.Next(0, n);
                if (a[x1, y1] != 10)
                {
                    a[x1, y1] = 10;//随机选择地雷的坐标,用数字10在a数组中表示为地雷
                    c.Bj(a, x1, y1);//调用BJ函数,对雷区周围的格子,进行加1
                    q--;         //如果布置了一个雷,就将总数减一,直到q==0的时候退出
                    if (q == 0)//如果地雷埋完了,就退出
                    {
                        break;
                    }
                }

            }

            do//上下左右移动光标
            {

                if (Console.KeyAvailable)
                {
                    #region  接受键盘,和上下左右移动
                    ConsoleKeyInfo cki = Console.ReadKey(true);
                    ConsoleKey ck = cki.Key;

                    if (ck == (ConsoleKey)37 && x != 0)//左
                    {
                        x--;
                    }
                    if (ck == (ConsoleKey)38 && y != 0)//上
                    {
                        y--;
                    }
                    if (ck == (ConsoleKey)39 && x != n - 1)//右
                    {
                        x++;
                        // Console.WriteLine("+{0}+",x);
                    }

                    if (ck == (ConsoleKey)40 && y != n - 1)//下
                    {
                        y++;
                    }

                    #endregion


                    if (ck == (ConsoleKey)13)
                    {
                        if (a[y, x] == 0)//如果该坐标没有雷区,和数字
                        {
                            c.Qk(a, y, x, b);//调用函数QK进行,清空周围的格子!!
                            Console.Clear();
                            for (int i = 0; i < n; i++)
                            {
                                for (int j = 0; j < n; j++)
                                {
                                    Console.Write(b[i, j]);
                                    if (j == n - 1)//标记右边边界
                                    {
                                        Console.Write(bianjie);
                                    }
                                }
                                Console.WriteLine();
                            }
                            for (int i2 = 0; i2 < n + 1; i2++)//标记下面边界
                            {
                                Console.Write(bianjie);
                            }
                        }
                        else if (a[y, x] == 10)//如果为雷区
                        {
                            Console.Clear();
                            b[y, x] = " *";
                            //如果为雷区,就将剩下的雷都显示出来
                            for (int i = 0; i < n; i++)
                            {
                                for (int j = 0; j < n; j++)
                                {
                                    if (b[i, j] == " *")
                                    {
                                        b[i, j] = "■";
                                    }
                                    if (a[i, j] == 10)
                                    {
                                        b[i, j] = " *";
                                    }
                                    Console.Write(b[i, j]);
                                    if (j == n - 1)//标记右边边界
                                    {
                                        Console.Write(bianjie);
                                    }
                                }
                                Console.WriteLine();
                            }
                            for (int i2 = 0; i2 < n + 1; i2++)//标记下面边界
                            {
                                Console.Write(bianjie);
                            }

                            Console.WriteLine("\n踩到地雷,GameOver.....再接再厉哦");
                            Console.ReadKey();
                            break;
                        }
                        else if (a[y, x] >= 0 && a[y, x] <= 9)//如果是雷的周围数字,就将数字显示出来
                        {
                            Console.Clear();
                            b[y, x] = " " + a[y, x].ToString();
                            for (int i = 0; i < n; i++)
                            {
                                for (int j = 0; j < n; j++)
                                {
                                    Console.Write(b[i, j]);
                                    if (j == n - 1)//标记右边边界
                                    {
                                        Console.Write(bianjie);
                                    }
                                }
                                Console.WriteLine();
                            }
                            for (int i2 = 0; i2 < n + 1; i2++)//标记下面边界
                            {
                                Console.Write(bianjie);
                            }
                        }

                    }
                    if (ck == (ConsoleKey)81)//这是标记雷区的按键
                    {
                        Console.Clear();
                        if (b[y, x] == "⊙")
                        {
                            b[y, x] = "■";
                        }
                        else if (b[y, x] == "■")
                        {
                            b[y, x] = "⊙";
                        }
                        int sum = 0;
                        int sumx = 0;
                        for (int i = 0; i < n; i++)
                        {
                            for (int j = 0; j < n; j++)
                            {
                                //sum统计雷的位置和标记的对应的个数
                                if (b[i, j] == "⊙" && a[i, j] == 10)
                                {
                                    sum++;
                                }
                                //sumx统计标记了多少个
                                if (b[i, j] == "⊙")
                                {
                                    sumx++;
                                }
                                Console.Write(b[i, j]);
                                if (j == n - 1)//标记右边边界
                                {
                                    Console.Write(bianjie);
                                }
                            }
                            Console.WriteLine();
                        }
                        for (int i2 = 0; i2 < n + 1; i2++)//标记下面边界
                        {
                            Console.Write(bianjie);
                        }
                        //sum=sumx=雷 的个数的时候,就胜利
                        if (sum == q1 && sumx == q1)
                        {
                            Console.WriteLine();
                            Console.WriteLine("恭喜您游戏胜利哦!!");

                            Console.ReadKey();
                            break;
                        }
                    }

                }
                Console.SetCursorPosition(2 * x, y);
            } while (true);

        }
        public void Qk(int[,] a, int x, int y, string[,] b)//选中为0的情况下,清空
        {
            b[x, y] = "  ";
            if (y - 1 >= 0)//上
            {
                if (a[x, y - 1] == 0)
                {
                    a[x, y - 1] = -1; //标记为-1,防止递归重复死循环
                    Qk(a, x, y - 1, b);
                    b[x, y - 1] = "  ";

                }
                else
                {
                    if (b[x, y - 1] != "  ")
                    {
                        b[x, y - 1] = " " + a[x, y - 1].ToString();
                    }
                }
            }
            if (x - 1 >= 0)//左
            {
                if (a[x - 1, y] == 0)
                {
                    a[x - 1, y] = -1;
                    Qk(a, x - 1, y, b);
                    b[x - 1, y] = "  ";
                }
                else
                {
                    if (b[x - 1, y] != "  ")
                    {
                        b[x - 1, y] = " " + a[x - 1, y].ToString();
                    }
                }
            }
            if (x + 1 <= a.GetUpperBound(0))//右
            {
                if (a[x + 1, y] == 0)
                {

                    a[x + 1, y] = -1;
                    Qk(a, x + 1, y, b);
                    b[x + 1, y] = "  ";

                }
                else
                {
                    if (b[x + 1, y] != "  ")
                    {
                        b[x + 1, y] = " " + a[x + 1, y].ToString();
                    }
                }

            }
            if (y + 1 <= a.GetUpperBound(1))//下
            {
                if (a[x, y + 1] == 0)
                {
                    a[x, y + 1] = -1;
                    Qk(a, x, y + 1, b);
                    b[x, y + 1] = "  ";

                }
                else
                {
                    if (b[x, y + 1] != "  ")
                    {
                        b[x, y + 1] = " " + a[x, y + 1].ToString();
                    }
                }


            }

        }

        public void Bj(int[,] a, int x, int y)  //对雷区周围的数字进行加1
        {
            if (x - 1 >= 0 && y - 1 >= 0)
            {
                a[x - 1, y - 1] = (a[x - 1, y - 1] == 10) ? 10 : ++a[x - 1, y - 1];//左上角
            }
            if (y - 1 >= 0)
            {
                a[x, y - 1] = (a[x, y - 1] == 10) ? 10 : ++a[x, y - 1];//上
            }
            if (x + 1 <= a.GetUpperBound(0) && y + 1 <= a.GetUpperBound(1))
            {
                a[x + 1, y + 1] = (a[x + 1, y + 1] == 10) ? 10 : ++a[x + 1, y + 1];//右上角
            }
            if (x - 1 >= 0)
            {
                a[x - 1, y] = (a[x - 1, y] == 10) ? 10 : ++a[x - 1, y];//左
            }
            if (x + 1 <= a.GetUpperBound(0))
            {
                a[x + 1, y] = (a[x + 1, y] == 10) ? 10 : ++a[x + 1, y];//右
            }
            if (x - 1 >= 0 && y + 1 <= a.GetUpperBound(1))
            {
                a[x - 1, y + 1] = (a[x - 1, y + 1] == 10) ? 10 : ++a[x - 1, y + 1];//左下角
            }
            if (y + 1 <= a.GetUpperBound(1))
            {
                a[x, y + 1] = (a[x, y + 1] == 10) ? 10 : ++a[x, y + 1];//下
            }
            if (x + 1 <= a.GetUpperBound(0) && y - 1 >= 0)
            {
                a[x + 1, y - 1] = (a[x + 1, y - 1] == 10) ? 10 : ++a[x + 1, y - 1];//右下角
            }

        }

    }
}

扫雷代码

标签:io   ar   os   使用   sp   for   on   bs   cti   

原文地址:http://www.cnblogs.com/zxhome/p/4130986.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!