WPF扫雷程序分享

  1. 实现效果:

    WPF扫雷程序分享_第1张图片
    WPF扫雷程序分享_第2张图片
    WPF扫雷程序分享_第3张图片
    WPF扫雷程序分享_第4张图片

  2. 前台代码:

<Window x:Class="扫雷.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:扫雷"
        mc:Ignorable="d"
        Title="扫雷V0.0.1" Height="620" Width="700" MaxHeight="620" MinHeight="620" MinWidth="700" MaxWidth="700">
    <Grid >
        <Grid.Background>
            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                <GradientStop Color="#FF3B41CF" Offset="0"/>
                <GradientStop Color="#FFB82274" Offset="0.992"/>
                <GradientStop Color="#FF4A4A4A" Offset="0.229"/>
                <GradientStop Color="#FF7AD65E" Offset="0.562"/>
            </LinearGradientBrush>
        </Grid.Background>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition Height="500"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="500"/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Grid Grid.Row="1" Grid.Column="0"  Name="grid_table"  Margin="5" Background="#7FD68585"/>
        <Grid Grid.Row="1" Grid.Column="1">
            <Grid.RowDefinitions>
                
                <RowDefinition Height="1*"/>
                <RowDefinition Height="4*"/>
                <RowDefinition Height="5*"/>
            </Grid.RowDefinitions>
            <StackPanel Grid.Row="0" Orientation="Vertical" VerticalAlignment="Center">
                <Label Content="难度选择:" Foreground="Azure" VerticalAlignment="Center"/>
                <ComboBox x:Name="cb_mode_set" SelectionChanged="cb_mode_set_SelectionChanged">
                    <ComboBoxItem Content="简单" IsSelected="True"/>
                    <ComboBoxItem Content="一般"/>
                    <ComboBoxItem Content="困难"/>
                </ComboBox>
            </StackPanel>
            <StackPanel Grid.Row="1" Orientation="Vertical" VerticalAlignment="Center">
                <Label Content="棋盘大小:" Foreground="Azure"/>
                <Button Width="150" Content="10*10" Height="50" Name="bt10" Click="bt10_Click"/>
                <Button Width="150" Content="20*20" Height="50" Name="bt20" Margin="0 1 0 0" Click="bt20_Click"/>
                <Button Width="150" Content="30*30" Height="50" Name="bt30" Margin="0 1 0 0" Click="bt30_Click"/>
            </StackPanel>
            <StackPanel Grid.Row="2" Orientation="Vertical" VerticalAlignment="Center" HorizontalAlignment="Center">
                <Image Width="100"   Source="钟.jpeg"/>
                <Label x:Name="lb_time" Content="00:00:00" Height="30" HorizontalAlignment="Center"/>
                <!--<Button Height="50" Content="暂停" x:Name="bt_stop" Click="bt_stop_Click"/>-->
                <Button Height="50" Content="放弃" x:Name="bt_cal" Click="bt_cal_Click" Margin="0 5 0 0"/>
                <TextBlock Text="Power By Bilibili-火狐鞥
2020-12-31" HorizontalAlignment="Center" VerticalAlignment="Center" TextAlignment="Center" FontSize="15"  Foreground="Gray"/>
            </StackPanel>
  
           
        </Grid>
        <Grid Grid.Row="0" Grid.Column="0">
            <Grid.RowDefinitions >
                <RowDefinition/>
                <RowDefinition/>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition/>
                <ColumnDefinition Width="0.1*"/>
                <ColumnDefinition/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>
            <Label Grid.Row="0" Grid.Column="0" Content="炸弹总数:" Foreground="Azure"/>
            <Label Grid.Row="1" Grid.Column="0" Content="已标记总数:" Foreground="Azure"/>
            <Label Grid.Row="2" Grid.Column="0" Content="当前步数:" Foreground="Azure"/>
            <Label x:Name="lb_minecount" Grid.Row="0" Grid.Column="1" Content="-" Foreground="Azure"/>
            <Label x:Name="lb_flagcount" Grid.Row="1" Grid.Column="1" Content="-" Foreground="Azure"/>
            <Label x:Name="lb_runstpe" Grid.Row="2" Grid.Column="1" Content="-" Foreground="Azure"/>

            <Label Grid.Row="0" Grid.Column="3" Content="成功最少步数:" Foreground="Azure"/>
            <Label Grid.Row="1" Grid.Column="3" Content="成功最少用时:" Foreground="Azure"/>
            <Label Grid.Row="2" Grid.Column="3" Content="-" Foreground="Azure"/>
            <Label x:Name="lb_minstpe" Grid.Row="0" Grid.Column="4" Content="-" Foreground="Azure"/>
            <Label x:Name="lb_mintime" Grid.Row="1" Grid.Column="4" Content="-" Foreground="Azure"/>
            <Label Grid.Row="2" Grid.Column="4" Content="-" Foreground="Azure"/>

        </Grid>
        <TextBox x:Name="tb_info" Grid.Row="0" Grid.Column="1" TextWrapping="Wrap" Visibility="Hidden"/>
        <TextBlock Grid.Row="0" Grid.Column="1" VerticalAlignment="Center"  TextWrapping="Wrap"  Text="第①步:选择难度
第②步:选择棋盘大小
第③步:开始扫雷
第④步:扫雷结束后(重新选择期盼大小启动新游戏)" Foreground="Red"/>

    </Grid>
</Window>

  1. 后台代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace 扫雷
{
     
    /// 
    /// MainWindow.xaml 的交互逻辑
    /// 
    public partial class MainWindow : Window
    {
     
        public MainWindow()
        {
     
            InitializeComponent();
            timer_init();
        }



        #region //变量定义
        private DispatcherTimer timer;
        private ProcessCount processCount;
        private ProcessCount MinTimeCount; //计数

        private int MineSIZE = 0; //棋盘大小 正方形期盼 行数列数

        private int MineCount = 0;//总共含有多少地雷
        private int MineTyep = 2; //难度系数  1正常  2 中等  4非常困难
        private int RedFlagCount = 0;//红旗总数
        private bool Can_Click = true; //全部暴雷后禁止点击
        /// 
        /// 每个区域的信息
        /// 
        class addr
        {
     
            private int _x = 0;
            private int _y = 0;
            private int mini_count = 0;  //附近地雷总数
            private bool is_mini = false; //是否是地雷
            private int stat = 0;       //状态 0未触发  1旗帜标志 2炸弹炸了  3清空该区域 4显示数字
            private int use = 0;//使用次数
            public int X
            {
     
                set {
      _x = value; }
                get {
      return _x; }

            }
            public int Y
            {
     
                set {
      _y = value; }
                get {
      return _y; }
            }

            public int U
            {
     
                set {
      use = value; }
                get {
      return use; }

            }
            public int Count
            {
     
                set {
      mini_count = value; }
                get {
      return mini_count; }

            }

            public bool ISMine
            {
     
                set {
      is_mini = value; }
                get {
      return is_mini; }

            }

            public int Stat
            {
     
                set {
      stat = value; }
                get {
      return stat; }

            }

        }


        /// 
        /// 区域信息列表
        /// 
        private List<addr> MineInfo;




        #endregion

        #region //棋盘按键  
        private void bt30_Click(object sender, RoutedEventArgs e)
        {
     
            show_table(30);
            timer.Start();
        }

        private void bt20_Click(object sender, RoutedEventArgs e)
        {
     
            show_table(20);
            timer.Start();
        }

        private void bt10_Click(object sender, RoutedEventArgs e)
        {
     
            show_table(10);
            timer.Start();
        }

        private void bt_stop_Click(object sender, RoutedEventArgs e)
        {
     
            if (timer != null)
            {
     
                timer.Stop();
            }
            
        }
        #endregion

        #region //功能函数

        [DllImport("kernel32.dll")]
        private static extern bool SetProcessWorkingSetSize(IntPtr proc, int min, int max);

        /// 
        /// 内存回收
        /// 
        private void ClearMemory()
        {
     
            GC.Collect();
            GC.WaitForPendingFinalizers();
            if (Environment.OSVersion.Platform == PlatformID.Win32NT)
            {
     
                SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1);
            }
        }


        /// 
        /// 产生指定随机数 且不重复
        /// 
        /// 产生随机数个数
        /// 最小值
        /// 最大值
        /// 
        public int[] GetRandomArray(int Number, int minNum, int maxNum)
        {
     
            int j;
            int[] b = new int[Number];
            Random r = new Random();
            for (j = 0; j < Number; j++)
            {
     
                int i = r.Next(minNum, maxNum + 1);
                int num = 0;
                for (int k = 0; k < j; k++)
                {
     
                    if (b[k] == i)
                    {
     
                        num = num + 1;
                    }
                }
                if (num == 0)
                {
     
                    b[j] = i;
                }
                else
                {
     
                    j = j - 1;
                }
            }
            return b;
        }

        /// 
        /// WPF控件查找
        /// 
        /// 空间类型
        /// 父控件
        /// 查找空间名称
        /// 
        public static T FindChild<T>(DependencyObject parent, string childName) where T : DependencyObject
        {
     
            if (parent == null) return null;

            T foundChild = null;

            int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
            for (int i = 0; i < childrenCount; i++)
            {
     
                var child = VisualTreeHelper.GetChild(parent, i);
                // 如果bai子控件不是需查找的控件类型
                T childType = child as T;
                if (childType == null)
                {
     
                    // 在下一级控件中递归查找
                    foundChild = FindChild<T>(child, childName);

                    // 找到控件就可以中断递归操作 
                    if (foundChild != null) break;
                }
                else if (!string.IsNullOrEmpty(childName))
                {
     
                    var frameworkElement = child as FrameworkElement;
                    // 如果控件名称符合参数条件
                    if (frameworkElement != null && frameworkElement.Name == childName)
                    {
     
                        foundChild = (T)child;
                        break;
                    }
                }
                else
                {
     
                    // 查找到了控件
                    foundChild = (T)child;
                    break;
                }
            }

            return foundChild;
        }

        #endregion

        #region //内部函数

        /// 
        /// 界面初始化
        /// 
        /// 
        private void show_table(int row)
        {
     
            grid_table.Children.Clear();
            grid_table.RowDefinitions.Clear();
            grid_table.ColumnDefinitions.Clear();
            for (int i = 0; i < row; i++)
            {
     

                grid_table.RowDefinitions.Add(new RowDefinition());
            }
            for (int i = 0; i < row; i++)
            {
     
                grid_table.ColumnDefinitions.Add(new ColumnDefinition());
            }
            MineCount = row * MineTyep;
            
            lb_minecount.Content = MineCount.ToString();
            init_mine(MineCount, row);//初始化地雷
            int count = 0;
            for (int i = 0; i < row; i++)
            {
     

                for (int j = 0; j < row; j++)
                {
     

                    int mine_count = 0;
                    if (count - row > 0 && i > 0)
                    {
     
                        if (MineInfo[count - row].ISMine)
                        {
     
                            mine_count++;
                        }
                    }


                    if (count - row + 1 > 0 && i > 0 && j < row - 1)
                    {
     
                        if (MineInfo[count - row + 1].ISMine)
                        {
     
                            mine_count++;
                        }
                    }

                    if (count - row - 1 > 0 && i > 0 && j > 0)
                    {
     
                        if (MineInfo[count - row - 1].ISMine)
                        {
     
                            mine_count++;
                        }
                    }

                    if (count + row < row * row - 1 && i < row - 1)
                    {
     
                        if (MineInfo[count + row].ISMine)
                        {
     
                            mine_count++;
                        }
                    }
                    if (count + row + 1 < row * row - 1 && i < row - 1 && j < row - 1)
                    {
     
                        if (MineInfo[count + row + 1].ISMine)
                        {
     
                            mine_count++;
                        }
                    }
                    if (count + row - 1 < row * row - 1 && i < row - 1 && j > 0)
                    {
     
                        if (MineInfo[count + row - 1].ISMine)
                        {
     
                            mine_count++;
                        }
                    }

                    if (count - 1 > 0 && j > 0)
                    {
     
                        if (MineInfo[count - 1].ISMine)
                        {
     
                            mine_count++;
                        }
                    }
                    if (count + 1 < row * row - 1 && j < row - 1)
                    {
     
                        if (MineInfo[count + 1].ISMine)
                        {
     
                            mine_count++;
                        }
                    }
                    MineInfo[count].Count = mine_count;

                    BrushConverter conv = new BrushConverter();
                    Brush bru;

                    bru = conv.ConvertFromInvariantString("#FFB6BFBF") as Brush;
                    Button tb = new Button() {
      FontFamily = new FontFamily("/Fonts/toopfont.ttf"), Name = "tbmine_" + count.ToString(), Background=bru, Content = "" };
                    tb.SetValue(Grid.RowProperty, i);
                    tb.SetValue(Grid.ColumnProperty, j);
                    tb.MouseRightButtonDown += new MouseButtonEventHandler(Right_Click);
                    //tb.MouseLeftButtonDown += new MouseButtonEventHandler(chelik_Click);
                    tb.Click += new RoutedEventHandler(Left_Click);
                    grid_table.Children.Add(tb);

                    count++;
                }
            }
            MineSIZE = row;
            RedFlagCount = 0;
            ClearMemory();
            processCount.TotalSecond = 0;
            processCount.Stpe = 0;
            Can_Click = true;
            lb_flagcount.Content = RedFlagCount.ToString();
            lb_runstpe.Content = processCount.Stpe.ToString();

        }

        /// 
        /// 地雷数量初始化
        /// 
        /// 地雷数量
        /// 雷区长度
        private void init_mine(int num, int area)
        {
     
            MineInfo = new List<addr>();
            MineInfo.Clear();
            int[] mine_addr = GetRandomArray(num, 0, area * area - 1);
            tb_info.Text = "";
            //for (int i = 0; i < num; i++)
            //{
     
            //    tb_info.Text += mine_addr[i].ToString()+",";
            //}

            int count = 0;
            for (int i = 0; i < area; i++)
            {
     
                
                for (int j = 0; j < area; j++)
                {
     
                    addr tp = new addr();
                    tp.Y = i;
                    tp.X = j;
                    tp.U = 0;
                    for (int k = 0; k < num; k++)
                    {
     
                        if (mine_addr[k] == count)
                        {
     
                            tb_info.Text += mine_addr[k].ToString() + ",";
                            tp.ISMine = true;
                            tp.Stat = 0;
                            tp.Count = 0;
                            break;
                        }
                    }
                    count++;
                    MineInfo.Add(tp);

                }
            }
        }

        /// 
        /// 定时器初始化
        /// 
        private void timer_init()
        {
     
            timer = new DispatcherTimer();
            timer.Interval = new TimeSpan(10000000);   //时间间隔为一秒
            timer.Tick += new EventHandler(timer_Tick);
            processCount = new ProcessCount(0);
            MinTimeCount = new ProcessCount(9999999);
        }
        /// 
        /// Timer触发的事件
        /// 
        /// 
        /// 
        private void timer_Tick(object sender, EventArgs e)
        {
     
            processCount.ProcessCountUp();
            lb_time.Content = processCount.GetHour() + ":" + processCount.GetMinute() + ":" + processCount.GetSecond();
            //timer.Stop();
        }


        /// 
        /// 显示所有信息
        /// 
        private void show_all_info()
        {
     
            timer.Stop();
            this.Dispatcher.Invoke(new Action(delegate
            {
     
                /*代码写在这里*/
                int count = 0;
                for (int i = 0; i < MineSIZE* MineSIZE; i++)
                {
     
                    string rb_name = "tbmine_X".Replace("X", count.ToString());
                    Button tb = null;
                    //this.GetChild
                    tb = FindChild<Button>(grid_table, rb_name);
                    //tb = (Button)grid_table.cFindName(rb_name);
                    if (tb != null)
                    {
     
                        if (MineInfo[count].ISMine)
                        {
     
                            if (MineInfo[count].Stat == 0) //为表示   //状态 0未触发  1旗帜标志 2炸弹炸了  3清空该区域
                            {
     
                                Image img = new Image();

                                BrushConverter conv = new BrushConverter();
                                Brush bru;

                                bru = conv.ConvertFromInvariantString("#000000FF") as Brush;
                                tb.Background = bru;
                                img.Source = new BitmapImage(new Uri("/Resources/炸弹.png", UriKind.Relative));
                                tb.Content = img;
                            }
                            else if (MineInfo[count].Stat == 1) //
                            {
     
                                Image img = new Image();
                                BrushConverter conv = new BrushConverter();
                                Brush bru;

                                bru = conv.ConvertFromInvariantString("#00FFFFFF") as Brush;
                                tb.Background = bru;
                                img.Source = new BitmapImage(new Uri("/Resources/排雷成功.png", UriKind.Relative));
                                tb.Content = img;
                            }
                            else if (MineInfo[count].Stat == 2) //
                            {
     
                                Image img = new Image();

                                BrushConverter conv = new BrushConverter();
                                Brush bru;

                                bru = conv.ConvertFromInvariantString("#FF0000FF") as Brush;
                                tb.Background = bru;
                                img.Source = new BitmapImage(new Uri("/Resources/炸了.png", UriKind.Relative));
                                tb.Content = img;
                            }
                            else if (MineInfo[count].Stat == 3)  // 不可能存在
                            {
     
                                //Image img = new Image();
                                //img.Source = new BitmapImage(new Uri("/Resources/炸弹.png", UriKind.Relative));
                                BrushConverter conv = new BrushConverter();
                                Brush bru;

                                bru = conv.ConvertFromInvariantString("#000000FF") as Brush;
                                tb.Background = bru;
                                tb.Content = "";

                            }
                        }
                        else
                        {
     
                            if (MineInfo[count].Stat == 0) //为表示   //状态 0未触发  1旗帜标志 2炸弹炸了  3清空该区域
                            {
     
                                //Image img = new Image();
                                //img.Source = new BitmapImage(new Uri("/Resources/炸弹.png", UriKind.Relative));
                                BrushConverter conv = new BrushConverter();
                                Brush bru;

                                bru = conv.ConvertFromInvariantString("#000000FF") as Brush;
                                tb.Background = bru;
                                tb.Content = "";
                            }
                            else if (MineInfo[count].Stat == 1) //
                            {
     
                                //Image img = new Image();
                                //img.Source = new BitmapImage(new Uri("/Resources/炸弹.png", UriKind.Relative));
                                BrushConverter conv = new BrushConverter();
                                Brush bru;
                                //tb.Content = img;
                                bru = conv.ConvertFromInvariantString("#FF0000FF") as Brush;
                                tb.Background = bru;
                            }
                            else if (MineInfo[count].Stat == 2) // 不可能存在
                            {
     
                                //Image img = new Image();
                                //img.Source = new BitmapImage(new Uri("/Resources/炸弹.png", UriKind.Relative));
                                //BrushConverter conv = new BrushConverter();
                                //Brush bru;
                                //tb.Content = img;
                                //bru = conv.ConvertFromInvariantString("#FF0000FF") as Brush;
                                //tb.Background = bru;
                            }
                            else if (MineInfo[count].Stat == 3) // 
                            {
     
                                //Image img = new Image();
                                //img.Source = new BitmapImage(new Uri("/Resources/炸弹.png", UriKind.Relative));
                                BrushConverter conv = new BrushConverter();
                                Brush bru;

                                bru = conv.ConvertFromInvariantString("#000000FF") as Brush;
                                tb.Background = bru;
                                tb.Content = "";
                            }
                        }
                    }
                    count++;
                }
                ClearMemory();
            }));
        }

        /// 
        /// 检查边界  
        /// 递归方式,要确定传递方向,防止震荡
        /// 
        ///  地址
        ///  左方向
        ///  右方方向
        ///  上方向
        ///  下方向
        private void MineCheck(int addr,bool left=true, bool right = true, bool top = true, bool bottom = true)
        {
     
            this.Dispatcher.Invoke(new Action(delegate
            {
     
                MineInfo[addr].U++;
                if (MineInfo[addr].ISMine)//如果是炸,直接爆炸  逻辑上不会出现此种情况
                {
     
                    MineInfo[addr].Stat = 2;
                    show_all_info();
                    Can_Click = false;
                    return;
                }
                else
                {
     

                    Button tb = null;
                    BrushConverter conv = new BrushConverter();
                    Brush bru;
                    if (MineInfo[addr].Count != 0) //判断当前是否为雷区边界
                    {
     

                        string rb_name = "tbmine_X".Replace("X", (addr).ToString());
                        tb = FindChild<Button>(grid_table, rb_name);
                        bru = conv.ConvertFromInvariantString("#FFF0F5F5") as Brush;
                        tb.Background = bru;
                        tb.Content = MineInfo[addr].Count.ToString();
                        MineInfo[addr].Stat = 4;
                        return;
                    }
                    else //
                    {
     
                        string rb_name = "tbmine_X".Replace("X", (addr).ToString());
                        tb = FindChild<Button>(grid_table, rb_name);
                        bru = conv.ConvertFromInvariantString("#FFE5E5E5") as Brush;
                        tb.Background = bru;
                        tb.Content = "";
                        MineInfo[addr].Stat = 3;  //状态 0未触发  1旗帜标志 2炸弹炸了  3清空该区域 4显示数字
                    }

                    //往左判断
                    if ((MineInfo[addr].X > 0) && (MineInfo[addr].X < MineSIZE) && left)
                    {
     
                        if (MineInfo[addr - 1].Stat != 3)
                        {
     
                            MineCheck(addr - 1);
                        }

                    }

                    //往右判断
                    if ((MineInfo[addr].X >= 0) && (MineInfo[addr].X < MineSIZE - 1) && right)
                    {
     
                        if (MineInfo[addr + 1].Stat != 3)
                        {
     
                            MineCheck(addr + 1);

                        }
                    }

                    //往上判断
                    if ((MineInfo[addr].Y > 0) && (MineInfo[addr].Y < MineSIZE) && top)
                    {
     
                        if (MineInfo[addr - MineSIZE].Stat != 3)
                        {
     
                            MineCheck(addr - MineSIZE);
                        }

                    }

                    //往下判断
                    if ((MineInfo[addr].Y >= 0) && (MineInfo[addr].Y < MineSIZE - 1) && bottom)
                    {
     
                        if (MineInfo[addr + MineSIZE].Stat != 3)
                        {
     
                            MineCheck(addr + MineSIZE);
                        }

                    }
 
                    //往左上判断
                    if ((MineInfo[addr].X > 0) && (MineInfo[addr].X < MineSIZE) && left && (MineInfo[addr].Y > 0) && (MineInfo[addr].Y < MineSIZE) && top)
                    {
     
                        if (MineInfo[addr - 1 - MineSIZE].Stat != 3)
                        {
     
                       
                            if (MineInfo[addr - 1 - MineSIZE].Count != 0) //此处值不为零 边界
                            {
     
                              
                                //显示值,并且背景色改变
                                MineCheck(addr - 1 - MineSIZE);
 

                            }
                            //else //为零,可以继续扩展
                            //{
     
                            //    MineCheck(addr - 1 - MineSIZE);
                            //}
                        }

                    }
                    //往右上判断
                    if ((MineInfo[addr].X >= 0) && (MineInfo[addr].X < MineSIZE - 1) && right && (MineInfo[addr].Y > 0) && (MineInfo[addr].Y < MineSIZE) && top)
                    {
     
                        if (MineInfo[addr + 1 - MineSIZE].Stat != 3)
                        {
     
                             
                            if (MineInfo[addr + 1 - MineSIZE].Count != 0) //此处值不为零 边界
                            {
     
                                MineCheck(addr + 1 - MineSIZE);
                            }
                            //else //为零,可以继续扩展
                            //{
     
                            //    MineCheck(addr + 1 - MineSIZE);
                            //}
                        }
                    }
                    //往左下判断
                    if ((MineInfo[addr].X > 0) && (MineInfo[addr].X < MineSIZE) && left && (MineInfo[addr].Y >= 0) && (MineInfo[addr].Y < MineSIZE - 1) && bottom)
                    {
     
                        if (MineInfo[addr - 1 + MineSIZE].Stat != 3)
                        {
     
                           
                            if (MineInfo[addr - 1 + MineSIZE].Count != 0) //此处值不为零 边界
                            {
     
                               // string rb_name = "tbmine_X".Replace("X", (addr - 1 + MineSIZE).ToString());
                                MineCheck(addr - 1 + MineSIZE);

                            }
                            //else //为零,可以继续扩展
                            //{
     
                            //    MineCheck(addr - 1 + MineSIZE);
                            //}
                        }

                    }

                    //往右下判断
                    if ((MineInfo[addr].X >= 0) && (MineInfo[addr].X < MineSIZE - 1) && right && (MineInfo[addr].Y >= 0) && (MineInfo[addr].Y < MineSIZE - 1) && bottom)
                    {
     
                        if (MineInfo[addr + 1 + MineSIZE].Stat != 3)
                        {
     
                      
                            if (MineInfo[addr + 1 + MineSIZE].Count != 0) //此处值不为零 边界
                            {
     
                                //显示值,并且背景色改变
                                MineCheck(addr + 1 + MineSIZE);
                            }
                            //else //为零,可以继续扩展
                            //{
     
                            //    MineCheck(addr + 1 + MineSIZE);
                            //}
                        }

                    }
 
                }

            }));
        }


        /// 
        /// 判断是否胜利
        /// 
        private bool check_scu()
        {
     
            int mine_count = 0;
            for (int i=0; i < MineSIZE * MineSIZE; i++)
            {
     
                if (MineInfo[i].Stat != 0)//状态 0未触发  1旗帜标志 2炸弹炸了  3清空该区域 4显示数字
                {
     
                    if (MineInfo[i].Stat == 1 &&MineInfo[i].ISMine)
                    {
     
                        mine_count++;
                    }
                }
                else
                {
     
                    break;
                }
            }
            if (mine_count == MineCount && MineCount == RedFlagCount)
            {
     
                return true;
            }
            return false;
        }
      
        /// 
        /// 左键按下
        /// 
        /// 
        /// 
        private void Left_Click(object sender, RoutedEventArgs e)
        {
     
            Button button = (Button)sender;
            string name = button.Name; //获取按钮名
            int addr = int.Parse(name.Split('_')[1]);
             
            if (MineInfo[addr].Stat != 4 && Can_Click) //判断是否可以操作
            {
     
                processCount.Stpe ++;
                lb_runstpe.Content = processCount.Stpe;
                if (MineInfo[addr].ISMine)
                {
     
                    show_all_info();
                    Can_Click = false;
                    MessageBox.Show("很遗憾,你被炸死了,祖国会感谢你做出的贡献!\r\n存活时间:"+lb_time.Content);
                }
                else
                {
     
                    MineCheck(addr);
                    ClearMemory();
                    if (check_scu())
                    {
     
                        show_all_info();
                        if (MinTimeCount.Stpe > processCount.Stpe && processCount.Stpe != 0)
                        {
     
                            MinTimeCount.Stpe = processCount.Stpe;
                            lb_minstpe.Content = MinTimeCount.Stpe.ToString();
                        }
                        if (MinTimeCount.TotalSecond > processCount.TotalSecond && processCount.TotalSecond != 0)
                        {
     
                            MinTimeCount.TotalSecond = processCount.TotalSecond;
                            lb_mintime.Content = MinTimeCount.GetHour() + ":" + MinTimeCount.GetMinute() + ":" + MinTimeCount.GetSecond();
                        }
                        Can_Click = false;
                        MessageBox.Show("恭喜,所有的地雷都被排除,你的功绩将会被永远铭记!\r\n使用时间:"+lb_time.Content);
                    }
                }
       

            }
           
        }
     
        /// 
        /// 鼠标点击操作
        /// 
        /// 
        /// 
        private void Right_Click(object sender, MouseButtonEventArgs e)
        {
     
            Button button = (Button)sender;
            string name = button.Name; //获取按钮名
            int addr = int.Parse(name.Split('_')[1]);
            
            if (e.RightButton == MouseButtonState.Pressed && Can_Click)  //鼠标右键  判断是否可操作
            {
     

                processCount.Stpe++;
                lb_runstpe.Content = processCount.Stpe;
                if (MineInfo[addr].Stat == 0)  //状态 0未触发  1旗帜标志 2炸弹炸了  3清空该区域 4显示数字
                {
     
                    Image img = new Image();
                    img.Source = new BitmapImage(new Uri("/Resources/红旗.png", UriKind.Relative));
                    RedFlagCount++;
                    lb_flagcount.Content = RedFlagCount.ToString();
                    button.Content = img;
                    MineInfo[addr].Stat = 1;
                }
                else if (MineInfo[addr].Stat == 1)
                {
     
                    MineInfo[addr].Stat = 0;
                    RedFlagCount--;
                    lb_flagcount.Content = RedFlagCount.ToString();
                    button.Content = "";
                }
                else if (MineInfo[addr].Stat == 3)
                {
     

                }
                else if (MineInfo[addr].Stat == 4) //禁止操作
                {
     
                    //MineInfo[addr].Stat = 0;

                    //button.Content = MineInfo[addr].Count;
                }
                if (check_scu())
                {
     
                    show_all_info();
                    if (MinTimeCount.Stpe > processCount.Stpe && processCount.Stpe!=0)
                    {
     
                        MinTimeCount.Stpe = processCount.Stpe;
                        lb_minstpe.Content = MinTimeCount.Stpe.ToString();
                    }
                    if (MinTimeCount.TotalSecond > processCount.TotalSecond && processCount.TotalSecond != 0)
                    {
     
                        MinTimeCount.TotalSecond = processCount.TotalSecond;
                        lb_mintime.Content = MinTimeCount.GetHour() + ":" + MinTimeCount.GetMinute() + ":" + MinTimeCount.GetSecond();
                    }
                    Can_Click = false;
                    MessageBox.Show("恭喜,所有的地雷都被排除,你的功绩将会被永远铭记!\r\n使用时间:" + lb_time.Content);
                }
            }
        }

        private void cb_mode_set_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
     
            if (cb_mode_set.SelectedIndex == 0)
            {
     
                MineTyep = 2;
            }
            else if (cb_mode_set.SelectedIndex == 1)
            {
     
                MineTyep = 4;
            }
            else if (cb_mode_set.SelectedIndex == 2)
            {
     
                MineTyep = 5;
            }
        }
        #endregion








        private void bt_cal_Click(object sender, RoutedEventArgs e)
        {
     
            show_all_info();
        }

       
    }

    /// 
    /// 实现倒计时功能的类
    /// 
    public class ProcessCount
    {
     
        private Int32 _TotalSecond;

        private int _stpe = 0;

        public Int32 TotalSecond
        {
     
            get {
      return _TotalSecond; }
            set {
      _TotalSecond = value; }
        }
        public int Stpe
        {
     
            get {
      return _stpe; }
            set {
      _stpe = value; }
        }

        /// 
        /// 构造函数
        /// 
        public ProcessCount(Int32 totalSecond,int s=0)
        {
     
            this._TotalSecond = totalSecond;
            this.Stpe = s;
        }


        /// 
        /// 减秒
        /// 
        /// 
        public bool ProcessCountDown()
        {
     
            if (_TotalSecond == 0)
                return false;
            else
            {
     
                _TotalSecond--;
                return true;
            }
        }


        /// 
        /// 加秒
        /// 
        /// 
        public bool ProcessCountUp()
        {
     
            _TotalSecond++;
            return true;
        }


        /// 
        /// 获取小时显示值
        /// 
        /// 
        public string GetHour()
        {
     
            return String.Format("{0:D2}", (_TotalSecond / 3600));
        }


        /// 
        /// 获取分钟显示值
        /// 
        /// 
        public string GetMinute()
        {
     
            return String.Format("{0:D2}", (_TotalSecond % 3600) / 60);
        }


        /// 
        /// 获取秒显示值
        /// 
        /// 
        public string GetSecond()
        {
     
            return String.Format("{0:D2}", _TotalSecond % 60);
        }
    }
}

4、代码下载:
WPF扫雷程序V0.0.1
试用程序:
链接:https://pan.baidu.com/s/132QZctP2zgLRfOJtLJ3v7Q
提取码:2qua

你可能感兴趣的:(c#,wpf,游戏)