C# 奇怪的扫雷舰炸弹围绕着数字

C# 奇怪的扫雷舰炸弹围绕着数字,c#,uwp-xaml,C#,Uwp Xaml,所以我为UWP做了一个扫雷舰克隆。当然,我有数据显示周围有多少地雷。这就是问题所在(你可以在图片上看到): 左上角、右上角、右上角和左上角字段缺少数字。但是,当我第二次单击时(我没有实施第二次单击开阔场地的预防措施),他们添加了1(地雷的放置方式不同,因为我重新启动应用程序以拍摄另一个屏幕截图): 我试图通过添加2而不是1来解决这个问题,但问题仍然存在,但更为严重 第一次点击: 第二次点击: 所以,最后,我们来看看codez: using System; using Windows.UI

所以我为UWP做了一个扫雷舰克隆。当然,我有数据显示周围有多少地雷。这就是问题所在(你可以在图片上看到):

左上角、右上角、右上角和左上角字段缺少数字。但是,当我第二次单击时(我没有实施第二次单击开阔场地的预防措施),他们添加了1(地雷的放置方式不同,因为我重新启动应用程序以拍摄另一个屏幕截图):

我试图通过添加2而不是1来解决这个问题,但问题仍然存在,但更为严重

第一次点击:

第二次点击:

所以,最后,我们来看看codez:

using System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=234238

namespace Mynesweeper
{
    public class Cell
    {
        public enum CellType
        {
            Safe = 0,
            Bomb = 1,
            Flag = 2,
            FirstClick = 3
        }

        public CellType cellState;
        public bool covered = true;
    }
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MineField : Page
    {

        Cell[,] cells = new Cell[0, 0];
        bool isGenerated = false;
        Button[,] fieldBtns = new Button[0, 0];
        int maxMines = 0;
        int[,] nearbyBombs = new int[0, 0];

        public MineField()
        {
            this.InitializeComponent();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            string param = e.Parameter.ToString();
            if (param == "tutorial")
            {
                MinesAmount.Text = "4";
                GenerateLevel(25, 25, 4);
            }
            else
            {
                TextBlock errTxtBlck = new TextBlock() { Name = "ErrorTextBlock", Text = "Error: no or incorrect parameter was specified when navigating to MineField.xaml\r\n\r\nIf you're the player and've downloaded the game through Windows Store, then let me know about this error by contacting me at artyomisflash@mail.ru. If you're not a player then maybe you're me and you have to fix this error.", HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Stretch, Margin = new Windows.UI.Xaml.Thickness(16, 0, 16, 0), TextWrapping = Windows.UI.Xaml.TextWrapping.Wrap, VerticalAlignment = Windows.UI.Xaml.VerticalAlignment.Center, FontSize = 24, TextAlignment = Windows.UI.Xaml.TextAlignment.Center };
                MainGrid.Children.Add(errTxtBlck);
                SmileyFace.Text = "...";
            }
        }

        public static int Clamp(int val, int min, int max)
        {
            if (val < min)
            {
                return min;
            }
            else if (val > max)
            {
                return max;
            }
            else
            {
                return val;
            }
        }

        void GenerateLevel(int x, int y, int mines)
        {
            cells = new Cell[x, y];
            StackPanel[] fieldRows = new StackPanel[y];
            fieldBtns = new Button[x, y];
            maxMines = mines;
            nearbyBombs = new int[x, y];
            for (int i = 0; i < y; i++)
            {
                fieldRows[i] = new StackPanel() { Name = "MineFieldPanelRow" + i, Orientation = Orientation.Horizontal };
                MineFieldPanelRows.Children.Add(fieldRows[i]);
                for (int j = 0; j < x; j++)
                {
                    fieldBtns[j, i] = new Button() { Name = "MineFieldButton" + j + "_" + i, Width = 64, Height = 64, Margin = new Thickness(4), Padding = Margin = new Thickness(0) };
                    fieldBtns[j, i].Click += MineFieldButtonClick;
                    fieldBtns[j, i].Content = new FontIcon() { Glyph = "\uE890", FontSize = 32 };
                    fieldRows[i].Children.Add(fieldBtns[j, i]);
                    cells[j, i] = new Cell()
                    {
                        covered = true
                    };
                    nearbyBombs[j, i] = 0;
                }
            }
        }

        void PlaceMines()
        {
            Random rnd = new Random();
            for (int i = 0; i < maxMines; i++)
            {
                int x = rnd.Next(0, cells.GetLength(0));
                int y = rnd.Next(0, cells.GetLength(1));
                while (cells[x, y].cellState == Cell.CellType.Bomb || cells[x, y].cellState == Cell.CellType.FirstClick)
                {
                    x = rnd.Next(0, cells.GetLength(0));
                    y = rnd.Next(0, cells.GetLength(1));
                }
                cells[x, y].cellState = Cell.CellType.Bomb;
            }
        }

        private void MineFieldButtonClick(object sender, RoutedEventArgs e)
        {
            if (!isGenerated)
            {
                for (int i = 0; i < fieldBtns.GetLength(1); i++)
                {
                    for (int j = 0; j < fieldBtns.GetLength(0); j++)
                    {
                        if (fieldBtns[j, i] == sender)
                        {
                            cells[j, i].cellState = Cell.CellType.FirstClick;
                            break;
                        }
                    }
                }
                PlaceMines();
                isGenerated = true;
            }
            for (int i = 0; i < cells.GetLength(1); i++)
            {
                for (int j = 0; j < cells.GetLength(0); j++)
                {
                    if (cells[j, i].cellState == Cell.CellType.Bomb)
                    {
                        fieldBtns[j, i].Content = new FontIcon() { Glyph = "\uE783", FontSize = 32 };
                        nearbyBombs[j, i] = -1;
                        if (i - 1 > -1)
                        {
                            if (j - 1 > -1)
                            {
                                nearbyBombs[j - 1, i - 1]+=2;
                            }
                            nearbyBombs[j, i - 1]+=2;
                            if (j + 1 <= nearbyBombs.GetLength(0) - 1)
                            {
                                nearbyBombs[j + 1, i - 1]+=2;
                            }
                        }
                        if (j - 1 > -1)
                        {
                            nearbyBombs[j - 1, i]+=2;
                        }
                        if (j + 1 <= nearbyBombs.GetLength(0) - 1)
                        { 
                            nearbyBombs[j + 1, i]++;
                        }
                        if (i + 1 <= nearbyBombs.GetLength(1) - 1)
                        {
                            if (j - 1 >= 0)
                            {
                                nearbyBombs[j - 1, i + 1]++;
                            }
                            nearbyBombs[j, i + 1]++;
                            if (j + 1 <= nearbyBombs.GetLength(0) - 1)
                            {
                                nearbyBombs[j + 1, i + 1]++;
                            }
                        }
                    }
                    else if (nearbyBombs[j, i] == 0)
                    {
                        fieldBtns[j, i].Content = null;
                    }
                    else
                    {
                        int bombsNearby = nearbyBombs[j, i];
                        fieldBtns[j, i].Content = new TextBlock() { Text = bombsNearby.ToString(), FontSize = 32 };
                    }
                }
            }
        }
    }
}
使用系统;
使用Windows.UI.Xaml;
使用Windows.UI.Xaml.Controls;
使用Windows.UI.Xaml.Navigation;
//空白页项模板被记录在https://go.microsoft.com/fwlink/?LinkId=234238
名称空间Mynesweeper
{
公共类单元
{
公共枚举单元类型
{
安全=0,
炸弹=1,
Flag=2,
FirstClick=3
}
公共细胞型细胞状态;
公共布尔覆盖=真;
}
/// 
///可以单独使用或在框架内导航到的空页。
/// 
公共密封部分级雷场:第页
{
单元格[,]单元格=新单元格[0,0];
bool isGenerated=false;
按钮[,]fieldBtns=新按钮[0,0];
int max=0;
int[,]nearbyBombs=新int[0,0];
公共雷场()
{
this.InitializeComponent();
}
受保护的覆盖无效OnNavigatedTo(NavigationEventArgs e)
{
string param=e.Parameter.ToString();
如果(参数==“教程”)
{
MinesAmount.Text=“4”;
生成级(25,25,4);
}
其他的
{
TextBlock errTxtBlck=new TextBlock(){Name=“ErrorTextBlock”,Text=“错误:导航到雷区时未指定任何参数或参数不正确。xaml\r\n\r\n如果您是玩家,并且已通过Windows应用商店下载了游戏,请通过以下地址与我联系以了解此错误:artyomisflash@mail.ru.如果你不是一名球员,那么也许你就是我,你必须纠正这个错误。“,HorizontalAlignment=Windows.UI.Xaml.HorizontalAlignment.Stretch,Margin=new Windows.UI.Xaml.Thickness(16,0,16,0),TextWrapping=Windows.UI.Xaml.TextWrapping.Wrap,VerticalAlignment=Windows.UI.Xaml.VerticalAlignment.Center,FontSize=24,TextAlignment=Windows.UI.Xaml.TextAlignment.Center};
MainGrid.Children.Add(errTxtBlck);
SmileyFace.Text=“…”;
}
}
公共静态int钳位(int val、int min、int max)
{
如果(valmax)
{
返回最大值;
}
其他的
{
返回val;
}
}
无效生成级别(整数x、整数y、整数矿山)
{
单元=新单元[x,y];
StackPanel[]fieldRows=新的StackPanel[y];
fieldBtns=新按钮[x,y];
最大地雷=地雷;
nearbyBombs=新整数[x,y];
for(int i=0;i-1)
<Page
    x:Class="Mynesweeper.MineField"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Mynesweeper"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <Grid x:Name="MainGrid">
        <Grid.Transitions>
            <TransitionCollection>
                <EntranceThemeTransition IsStaggeringEnabled="True"/>
            </TransitionCollection>
        </Grid.Transitions>
        <StackPanel x:Name="Stats" Margin="16,16,16,0" Orientation="Horizontal" VerticalAlignment="Top" HorizontalAlignment="Center" RenderTransformOrigin="0.5,0.5">
            <TextBlock x:Name="TimeAmount" TextWrapping="Wrap" Text="0" VerticalAlignment="Center" FontSize="32" Width="64" TextAlignment="Right" Margin="0,0,16,0"/>
            <Button x:Name="Smiley" Padding="0" Width="64" Height="64" HorizontalAlignment="Center" VerticalAlignment="Stretch" FontSize="32" d:LayoutOverrides="LeftMargin, RightMargin, LeftPosition, RightPosition">
                <TextBlock x:Name="SmileyFace" Text=":)" TextLineBounds="Tight" TextAlignment="Center"/>
            </Button>
            <TextBlock x:Name="MinesAmount" TextWrapping="Wrap" Text="0" VerticalAlignment="Center" FontSize="32" Width="64" Margin="16,0,0,0"/>
        </StackPanel>
        <StackPanel x:Name="MineFieldPanelRows" Margin="0,16,0,0" VerticalAlignment="Center" HorizontalAlignment="Center" RenderTransformOrigin="0.5,0.5">
            <StackPanel.RenderTransform>
                <CompositeTransform ScaleX="0.25" ScaleY="0.25"/>
            </StackPanel.RenderTransform> <!-- specially downscaled to fit the whole field on the screen -->
        </StackPanel>

    </Grid>
</Page>
                    if (j - 1 > -1)
                    {
                        nearbyBombs[j - 1, i]+=2;
                    }
void updateBombCountsAroundBomb( int bombY, int bombX, int height, int width )
{
    for( y = bombY - 1;  y <= bombY + 1;  y++ )
        for( x = bombX - 1;  x <= bombX + 1;  x++ )
            if( y != bombY && x != bombX )
                if( y >= 0 && y < height && x >= 0 && x < width )
                     nearbyBombs[y, x]++;
}
else if (nearbyBombs[j, i] == 0)